Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent."
 
PerlMonks

(tye)Re: A question of style

by tye (Archbishop)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Aug 23, 2000 at 05:38 UTC ( #29139=note: print w/ replies, xml ) Need Help??

in reply to A question of style

Updates added (inside bolded parens).

Okay, this is rumor control: (:

  1. Using ampersand and parentheses can be good when calling a user-defined subroutine with a name that is all lowercase (or all uppercase) because otherwise you might unintentionally invoke a current or future built-in function by mistake. (Using an underscore in the name makes this less likely but I'm not convinced that we won't see more built-ins with underscores in their names.)
  2. Using ampersand without parentheses (as already noted) is a special form for reusing the value of @_ and should only be used for advanced purposes.
  3. Ampersand is required for certain operations involving references to functions: for taking a reference to a function ($ref= \&func) and then using that reference (&$ref(...)), though the second item can be written as $ref->(...) for recent versions of Perl.
  4. Using neither ampersand nor parentheses requires that the subroutine be predeclared. This can be very handy because with use strict 'subs', this means that your typos are caught at compile time instead of at run time (where you might not notice them for quite a while if that part of your code is seldom executed). (However, predeclaring subroutines can be tricky and turn into a maintenance problem. Putting most of your subroutines into modules can make this easier.)
  5. Adding an ampersand can break code because it disables function prototypes. If you are not certain that a function isn't prototyped, then you shouldn't use an ampersand on it. This means that it is a bad idea to make subroutines with all-lowercase names (or all-uppercase names) that have prototypes. Prototypes are rarely used (except for making constants -- where overriding them isn't very bad, just removing a performance optimization) but you can still get bit by this.
  6. I see places where a lack of parens makes the code easier to read (when you have functions that like to be stacked similar to
    print map {} sort {} map {} grep {} @list;
    ). However, the lack of parens is more likely to make the code harder to maintain (makes it easy to make precedence errors -- especially since the precedence of a function call can change based on its prototype). Note the two errors introduced below:
    print map {} sort {} map {} grep {} @list, "\n" || die;
  7. Ampersand is considered "old" style because it wasn't too long ago when you had to use ampersand to call a user-defined subroutine (gee, I've reused that part of my brain already; I think ampersand was first made optional with perl5). (However, there are some good reasons to use & for subroutines listed above. Another reasonable justification would be because you like having a visual distinction between built-ins (can't have &) and user-defined subroutines.)

Sorry, it just seemed like a lot of speculation was flying about, some correct, some wrong, some just unexplained.

        - tye (but my friends call me "Tye")


Comment on (tye)Re: A question of style
Select or Download Code
Re^2: A question of style
by throop (Chaplain) on Sep 30, 2007 at 23:08 UTC
    Thanks, tye, for your most helpful feedback.

    throop

[reply]
Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: note [id://29139]
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others rifling through the Monastery: (12)
BrowserUk
ikegami
planetscape
atcroft
herveus
thezip
Eyck
ssandv
llancet
gnosti
im2
kevinphillips80
As of 2009-11-21 02:10 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (725 votes), past polls