Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: What's so bad about &function(...)?

by tirwhan (Abbot)
on Dec 07, 2005 at 18:16 UTC ( [id://514952]=note: print w/replies, xml ) Need Help??


in reply to What's so bad about &function(...)?

(As read in Perl Best Practices): The ampersand is ambiguous and can be interpreted by perl as bitwise AND in certain contexts. Since it is not necessary, it is better to always omit the &.


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
  • Comment on Re: What's so bad about &function(...)?

Replies are listed 'Best First'.
Re^2: What's so good about &function(...)
by tye (Sage) on Dec 08, 2005 at 07:39 UTC
    The ampersand is ambiguous and can be interpreted by perl as bitwise AND in certain contexts.

    Wow, I've never seen that trap sprung.

    The trap I have seen sprung quite a few times is sub log { ... }. That is why I follow my own advice in my 'classic' node, (tye)Re: A question of style, and mix case in all of my subroutine names.

    But, I sometimes slip and use camelCase and then get lazy and give a routine or two one-word names and... I've actually still been bit by sub log { ... }, just less frequently than before.

    So using &func( ... ) is still in my list of optional best practices for two reasons. First, it prevents you from being caught (often very confusingly) if you unintentially give your subroutine a "reserved" name. Second, it is a valid style choice to visually distinguish user sub calls from other similar-looking constructs.

    Note that if you import a &log, then log() will call it and so the leading ampersand is not as useful for imported routines. So I don't find the "might disable prototypes" argument too worrisome. Since, if I didn't important the routine, then I'm sure to know whether or not I was stupid enough to use a prototype on it. While, if I imported the routine, I don't need to use the ampersand (and modules that export over the top of built-in functions usually have a good reason for doing so or get bug reports filed against them).

    Now, as far as best practices go, I consider "always mix case (or add an underscore) in your subroutine names" to be a better "best practices" then "use &sub(...) when calling your own subs", because there are fewer nearby potential down-sides (such as using &sub w/o parens). But I do get annoyed when I see (every time the subject comes up) a bunch of people spouting poorly justified proclamations against using &sub(...) while also not warning against using subroutine names that might accidentally match a built-in.

    - tye        

Re^2: What's so bad about &function(...)?
by Perl Mouse (Chaplain) on Dec 08, 2005 at 20:27 UTC
    The ampersand is ambiguous and can be interpreted by perl as bitwise AND in certain contexts. Since it is not necessary, it is better to always omit the &.
    Sounds profound, and it got you quite some XP. It should have been negative XP, because your conclusion is 100% wrong. If you you are afraid to get be bitten by the ambiguity you mention, you should avoid using bitwise and, not the sub-sigil. Because whenever perl has the option to parse an ampersand as a sub-sigil or as a bitwise and, it will parse it as the sub-sigil.
    $ perl -MO=Deparse,-p -e 'sub foo {} sub bar {} bar & foo()' sub foo { } sub bar { } bar(&foo()); -e syntax OK
    Someone using sub-sigils in the above case would have gotten the binary and parsing:
    perl -MO=Deparse,-p -e 'sub foo {} sub bar {} &bar & &foo()' sub foo { } sub bar { } (&bar & &foo());
    So, you have two options to avoid this ambiguity: either avoid using binary and, or to always use the sub-sigil.
    Perl --((8:>*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found