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 | [reply] [d/l] [select] |
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. | [reply] |
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) | [reply] [d/l] [select] |
sub blah { return }
while( blah ) {
print "Barewords aren't always true.\n";
}
Just an exception. (:
-
tye
(but my friends call me "Tye") | [reply] [d/l] |
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? | [reply] [d/l] [select] |
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.
| [reply] |
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*
| [reply] |