Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

perlsub prototype sub(_) sub foo(_)

by Anonymous Monk
on Jun 30, 2013 at 06:58 UTC ( [id://1041551]=perlquestion: print w/replies, xml ) Need Help??

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

This compiles, but what does it mean?
sub foo(_){ warn @_ }

Replies are listed 'Best First'.
Re: perlsub prototype sub(_) sub foo(_)
by Athanasius (Archbishop) on Jun 30, 2013 at 07:29 UTC
Re: perlsub prototype sub(_) sub foo(_)
by farang (Chaplain) on Jun 30, 2013 at 07:32 UTC

    From perlsub:

    As the last character of a prototype, or just before a semicolon, a @ or a % , you can use _ in place of $ : if this argument is not provided, $_ will be used instead.

    So I guess _ being the only character in the prototype, is also the last, and is equivalent to $ there.

    Update: I see from the response of Athanasius above that my guess is slightly off. I didn't quite understand what the documentation is saying.

Re: perlsub prototype sub(_) sub foo(_)
by LanX (Saint) on Jun 30, 2013 at 11:26 UTC
    Whats wrong with the documentation?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      If you are referring to my node, there's probably nothing wrong with the documentation, just my quick reading of it. When it says you can use _ in place of $ I got the idea that prototypes (_) and ($) would be the same, which is not the case.

      sub foo(_){ warn @_ } sub goo($){ warn @_ } foo(); goo();
      output:
      Not enough arguments for main::goo at pm3 line 4, near "()"
      Execution of pm3 aborted due to compilation errors.

      Whats wrong with the documentation?

      It didn't show up in the search :)

      It doesn't mention  (_) and  (_) is not on the list Declared as            Called as

        TIP: a file-search in perlsub#Prototypes for  _ (surrounded by spaces) helps! =)

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

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

    No recent polls found