Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

..."while" i do perl...

by john1987 (Acolyte)
on Jan 29, 2001 at 05:09 UTC ( [id://54923]=perlquestion: print w/replies, xml ) Need Help??

john1987 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
As you might have seen me lurking around the monastery, I got a question about the while statements. It's a fairly sipmle one but I still need help...

I have seen examples that use numbers in the while statements, I want to know if it is possible to use words. It might sound like a stuipid question, but in every example I've seen, it always had the > >= < <= eq = in them. I havent seen one example with words in the while (blah)  {. Please have mercy on my stuipid question, I really need help. Thanks for taking your time out to read this. Later.

One person can change the corse of history. One person can destroy the human race. That one person is out there, I intend to find him.

Replies are listed 'Best First'.
Re: ..."while" i do perl...
by damian1301 (Curate) on Jan 29, 2001 at 05:12 UTC
    You can use
    while ($var ne 'blah'){ do stuff }
    or
    while($var){ read it }
    But I have never seen a bareword in there which leads me to believe that you can't have it in there. When you think about it though, what would a word in there do? Monks?

    Wanna be perl hacker.
    Dave AKA damian

    I encourage you to email me
Re: ..."while" i do perl...
by kschwab (Vicar) on Jan 29, 2001 at 05:43 UTC
    It's just an expression. The while loop executes while the expression is true. Expressions can be many things, including $variables, "words", /regexps/, etc.

    From the perl faq:

    Perl doesn't have a boolean type. 0, "0", "", undef and () are false, all else is true.

    So, fill it with an expression, and while the expression is "true", the loop runs.

Re: ..."while" i do perl...
by Anonymous Monk on Jan 29, 2001 at 06:03 UTC
    You can do
    while(blah)
    with blah being an unquoted bareword, but I'm not sure that you'd want to - an unquoted word, as long as it isn't 0, will always evaluate as true, as shown by
    while(blah) { print "Bareword works\n"; }
    (produces an infinite loop)
      sub blah { return } while( blah ) { print "Barewords aren't always true.\n"; }

      Just an exception. (:

              - tye (but my friends call me "Tye")
Re: ..."while" i do perl...
by Maclir (Curate) on Jan 29, 2001 at 07:39 UTC
    You could have a subroutine called "blah" that returns true or false:
    sub blah { # Do some thinking here and give a value to $return_code return $return_code; } while (blah) { # do something really useful here }
    I would categorise that as bad programming style. It will fail under "use Strict". I would prefer to show that "blah" was really a subroutine in the following way:
    while (&blah) { }
    or even use &blah() to show this is really a subroutine call.

    A question to the true perl gurus - is there any advantage in putting in the () if there are no arguments being passed to a sub?

      This comes up periodically. The answer is in perlsub, if you omit the parens there is an implicit set of arguments passed which might not be what you want. I consider this a dangerous style because even if you know about the gotcha, the person after you might not.

      Two other relevant notes. There is a promise that future Perl built-in functions will be lower case, so some people like using mixed-case function names. Also using the & is (IIRC) considerably slower than not using it.

      Personally I used to use & all of the time, but now I never do. I don't consider the arguments either way particularly convincing on that. But I always put in the parens.

      is there any advantage in putting in the () if there are no arguments being passed to a sub?

      The answer depends on if you prepend the sub name with & or not.

      If you do something like &blah, the sub gets the current contents of @_ as arguments (it's equivalent to blah(@_)).

      If you just do blah, then if the sub's been predeclared, it'll be called. Otherwise, it's interpreted as a bareword string. Which is probably not what you want.

      I avoid the controversy altogether by using the parenthesis as often as possible, and the ampersand only when dealing with references. I haven't found a good use for &blah, passing @_ by default, except for certain AUTOLOAD routines. It's better to avoid side-effects that might confuse someone else reading my code.

      They'll be confused enough already. *sigh*

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54923]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found