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 | |
by ikegami (Patriarch) on Feb 08, 2006 at 17:54 UTC |