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

subroutine question

by 2501 (Pilgrim)
on Dec 07, 2000 at 22:00 UTC ( [id://45556]=perlquestion: print w/replies, xml ) Need Help??

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

I just had an interesting question I wasn't able to answer.
if I had:
use strict; sub foo { print "hi\n"; } foo; &foo;
is there a difference in these two calls of foo() other then aesthetic?
thank you for your time!

Replies are listed 'Best First'.
(jeffa) Re: subroutine question
by jeffa (Bishop) on Dec 07, 2000 at 22:06 UTC
    Sure there is a difference, try this:
    #!/usr/bin/perl -w use strict; foo; &foo; sub foo { print "hi\n"; }
    The first call to foo:
    • won't work when you have use strict turned on
    • will complain if you use -w
    • and won't do anything if you don't use strict or -w
    The reason is because the perl interpeter sees the call to foo BERFORE it knows about foo.

    What about the second call? Well, because you precede it with an ampersand, perl knows that somewhere in the file a subroutine named foo exists. This also works if you append parens to the function name.

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: subroutine question
by chipmunk (Parson) on Dec 07, 2000 at 22:17 UTC
    perlsub explains the semantics of the different calling syntaxes. One little-known difference is that &foo, with no parentheses, gives foo access to the current contents of @_:
    #!/usr/local/bin/perl sub foo { $_[0] = 'b'; } @_ = 'a'; foo(); print "@_\n"; &foo; print "@_\n"; __END__ a b
Re: subroutine question
by mirod (Canon) on Dec 07, 2000 at 22:06 UTC
Re: subroutine question
by mrmick (Curate) on Dec 07, 2000 at 22:09 UTC
    I don't think that there's any real difference here except that
    &foo
    is more reliable for calling without parentheses. You can run into trouble with an error at compile time.

    My personal preference is to use the parentheses when calling subroutines to make things a little more readable for others.

    foo();

    Mick

      An additional difference is that if you call a sub with foo; (no parens) you're calling it without arguments. (Note that, as jeffa mentions, this call will only work so long as foo has already been defined)

      In current versions of perl , &foo; calls foo with the current contents of @_. The difference shows up in the following code:

      #!/usr/bin/perl use strict; sub foo { my $name = shift || "anonymous monk"; uc $name; } print foo, "\n"; bar('arturo'); sub bar { print foo, "\n"; print &foo, "\n"; }

      when run, this will print "ANONYMOUS MONK" twice, and "ARTURO" once, at the end, because the second call to foo in subroutine bar inherits the argument list passed to bar, while the first doesn't.

      HTH

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        An additional difference is that if you call a sub with foo; (no parens) you're calling it without arguments.

        Not strictly true. Calling foo (or &foo) without arguments will pass on the current subroutine's @_ to the called subroutine.

        Update:
        Actually, I'm being less than 100% accurate here :)

        &foo; will pass on the calling sub's @_, foo; doesn't.

        Apologies for any confusion caused.

        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: subroutine question
by lemming (Priest) on Dec 07, 2000 at 22:11 UTC
    Since you declare foo before you "foo;", there isn't a problem. Try putting the sub declaration afterwards and you'll see strict get mean.
    Perl 4 and before you have to call functions with & prepended. For readability and because I do my subs after the main section, I use foo(). YMMV.
Re: subroutine question
by damian1301 (Curate) on Dec 08, 2000 at 01:43 UTC
    Correct me all if Im wrong, but wasn't this calling of a sub (foo;) illegal (with -w on or not) in earlier versions of Perl? I like calling subroutines better with the ampersand because with some methods (param()) I get an error...

    Undefined subroutine &main::param called at c:\windows\TEMP\DzTemp.pl line 20

    This is quite annoying and I would wish that this didn't have to be stopped by use strict;, but just stopped altogether. To avoid this error I had to make more code to get around it...
    if(!$query->param("blah")){ ... #do stuff here }


    So, if they changed the rule about the ampersand (&) in the first place, they should definitely change it back.

    Wanna be perl hacker.
    Dave AKA damian

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-23 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found