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

What's the difference between calling a subroutine using
foo(); &foo(); &foo();
Is there any special time when you need to use a certain syntax?

Replies are listed 'Best First'.
Re: subroutine call
by dragonchild (Archbishop) on Aug 03, 2001 at 21:21 UTC
    If you use &, then you're bypassing any prototyping that might have been set up for the function. If there's no prototyping, then there's no difference. :)

    ------
    /me wants to be the brightest bulb in the chandelier!

      You can also "override"1 a builtin with the &foo() syntax. You can also pass your current @_ through with it, if you leave off the parens.

      sub time { print "Is on my side\n"; print &bar, "\n"; } sub bar { my ($baz) = @_; uc $baz; } &time('yes it is'); # prints 'Is on my side', a newline, followed by ' +YES IT IS'

      HTH

      1 OK, maybe it's not truly overriding, because of the special syntax you have to use to call it.

      perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
        thanks
        &foo;

        will call your subroutine with the @_ array set to the arguments of the caller.

        an example

        bar("a","b"); sub bar { foo(); &foo(); &foo; } sub foo { ($a, $b)=@_; print "$a, $b\n"; }
        the call to bar will now print
        ,
        ,
        a, b
        
        Try doing a search on prototyping. Or, you could just click on prototyping, which will end up doing the same thing, eventually. :)

        ------
        /me wants to be the brightest bulb in the chandelier!

(tye)Re: differences between ways of calling subroutines
by tye (Sage) on Aug 04, 2001 at 00:42 UTC

    Gee, this has been coming up a lot lately. I'll include my usual link to (tye)Re: A question of style. (:

            - tye (but my friends call me "Tye")
Re: subroutine call
by cLive ;-) (Prior) on Aug 03, 2001 at 22:02 UTC
    And don't forget this, for subs without args:
    # before declared - use () print_me(); # sub sub print_me { print "hello there!\n"; } # after declaration, you can drop the () # - but see prototyping as well... print_me;

    cLive ;-)