in reply to calling f(arg1,arg2,arg3) better than &f

What argument checking?

sub f { my ($arg1,$arg2,$arg3) = @_; .... uses arg1, arg2 arg3 .... } f(); # Works. ($arg1, $arg2 and $arg3 are undef.) f(1,2,3,4); # Works. ('4' is ignored.)

Maybe you're thinking of prototypes? & causes the prototype to be ignored. Use of & is discouraged for this reason. Then again, most prototypes are discouraged as well (for other reasons).

Aside from ignoring prototypes, (&f) means something different than f():

sub f { my ($arg1) = @_; print(defined($arg1) ? $arg1 : '[undef]', "\n"); } sub t1 { print("t1: "); f(); } sub t2 { print("t2: "); &f; } t1('moo'); # t1: [undef] t2('moo'); # t2: moo

When function f is called using &f (with no arguments), a new @_ isn't created; it shares the @_ of its caller.

Looking at it from the other direction,
f(...);
is equivalent to
do { local @_ = (...); &f };
(when no prototypes are involved).

Update: Added talk of prototypes.

Replies are listed 'Best First'.
Re^2: calling f(arg1,arg2,arg3) better than &f
by edwardt_tril (Sexton) on Feb 08, 2006 at 17:50 UTC
    Thanks. But why we want to share @_ with the caller? Please enlighten me. I am a true newbie in perl. Thanks
      Sometimes, you want to pass the arguments you received on to the called function. It's more efficient than creating a new @_. Even more efficient is goto(&f) which returns directly to the caller. Both of these tricks are useful when dealing with advanced features and techniques such as AUTOLOAD and currying. This is not something one commonly does, so & is rarely required.