Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Calling subs with &

by Anonymous Monk
on Oct 28, 2003 at 20:26 UTC ( [id://302800]=perlquestion: print w/replies, xml ) Need Help??

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

A subroutine can be called with an optional & prefix. Does it matter which way I do it?
use strict; here(); &here(); sub here { print "In sub\n"; }

Replies are listed 'Best First'.
Re: Calling subs with &
by Zaxo (Archbishop) on Oct 28, 2003 at 20:37 UTC

    Calling with '&' can affect both prototype checking and the value of @_ passed to the sub. With &, the prototype check is skipped, and if no parenthesized arguments are given the existing @_ is passed as the argument list.

    After Compline,
    Zaxo

Re: Calling subs with &
by sauoq (Abbot) on Oct 28, 2003 at 20:33 UTC

    Not usually, but it does if the sub is "prototyped"; in which case the call with & will disregard the "prototype".

    It can also matter if you don't supply parens on your call; in which case you'll need to declare the sub before you call it unless you use the & on your call.

    $ perl -le 'foo; sub foo { print "foo" }' $ perl -le '&foo; sub foo { print "foo" }' foo
    Enabling warning will help you catch that sort of an error.
    $ perl -wle 'foo; sub foo{ print "foo"}' Unquoted string "foo" may clash with future reserved word at -e line 1 +. Useless use of a constant in void context at -e line 1.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Calling subs with &
by monktim (Friar) on Oct 28, 2003 at 20:49 UTC
    You can also use & for evil purposes like using reserved words as sub names. It's not bad for obfuscation though.
    use strict; &use(); #use(); #this doesn't work sub use { print "In sub\n"; }
      It's not bad for obfuscation though.
      package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g; print } sub __PACKAGE__ { & print ( __PACKAGE__)} & __PACKAGE__ ( )

      Abigail

Re: Calling subs with & (ins std link)
by tye (Sage) on Oct 28, 2003 at 21:15 UTC
Re: Calling subs with &
by sgifford (Prior) on Oct 29, 2003 at 03:23 UTC

    Another difference I haven't seen mentioned yet is that within a sub, using &here (with the ampersand and without parentheses) will pass along the arguments in @_ to the here sub.

    For example:

    #!/usr/bin/perl -w use strict; test1("hello"); test2("hello"); sub test1 { &showargs; } sub test2 { &showargs(); } sub showargs { print "Got args @_\n"; } __END__ Got args hello Got args
Re: Calling subs with &
by Anonymous Monk on Oct 29, 2003 at 09:05 UTC
    Subroutines are documented in perlsub. What you read here is information from the manual (perlsub).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found