in reply to Re^6: why does push not default to $_? (simple)
in thread why does push not default to $_?
already tried it, it's a trap forget it. If you do push (@a,@b) and @b is empty, then $_ will be pushed, and that's definetly not what you expect!sub push (\@;@) { CORE::push ( @{+shift}, @_ ? @_ : $_ ) }
If there is any feature request, which might make sense on the wishlist for the next perl5 version, it's to add a prototype symbol for "list defaulting to $_". "_" is already a symbol for "scalar defaulting to $_" which must be placed as last symbol before ";".
So I suggest double underscore "__" shall mean "list defaulting to $_".
I remember slightly that in PBP one can find a somehow similar reasoning why not to use prototypes! *
If anybody knows a way to already implement push() this way, please try these testcases:
use subs 'push'; sub push (\@;@) { # print $_[0],$_[1]; CORE::push( @{+shift}, $_[0]? @_ :$_) ; } $\="\n"; my @a; $_="_"; print "--- one parameter"; push @a; print @a; print "--- long list of parameters"; @b=("x","y"); @a=(); push @a,@b; print @a; print "--- one element"; @a=(); push @a,"z"; print @a; print "--- one element list"; @a=(); @b=("z"); push @a,@b; print @a; print "--- empty list"; @a=@b=(); push @a,@b; print @a; __END__ --- one parameter _ --- long list of parameters xy --- one element z --- one element list z --- empty list _
Cheers Rolf
|
|---|