Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Syntactic Confectionery Delight
 
PerlMonks  

Re: ..."while" i do perl...

by Maclir (Curate)
on Jan 29, 2001 at 02:39 UTC ( [id://54939]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to ..."while" i do perl...

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?

Replies are listed 'Best First'.
Re (tilly) 2: ..."while" i do perl...
by tilly (Archbishop) on Jan 29, 2001 at 02:54 UTC
    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.

Re: Re: ..."while" i do perl...
by chromatic (Archbishop) on Jan 29, 2001 at 03:00 UTC
    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: note [id://54939]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.