in reply to Re: subroutine question
in thread subroutine question

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

Replies are listed 'Best First'.
Re: Re: Re: subroutine question
by davorg (Chancellor) on Dec 08, 2000 at 13:56 UTC

    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