jbw@bucsf.bu.edu (Joe Wells) (03/06/90)
Some questions for the great perl gods: 1. Can a subroutine tell whether it was invoked without arguments? example: &subroutine_name; 2. Can a subroutine tell whether it was invoked in an array or scalar context? A boolean scalar context? 3. Can a subroutine tell whether its invocation was part of an expression or was the entire expression? example: &sub($x); $x = (&sub ($x) + &sub2 ($y)); -- Joe Wells <jbw@bu.edu> jbw%bucsf.bu.edu@cs.bu.edu ...!harvard!bu-cs!bucsf!jbw
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/06/90)
In article <JBW.90Mar5185907@bucsf.bu.edu> jbw@bucsf.bu.edu (Joe Wells) writes:
: Some questions for the great perl gods:
:
: 1. Can a subroutine tell whether it was invoked without arguments?
:
: example: &subroutine_name;
Not that I know of.
: 2. Can a subroutine tell whether it was invoked in an array or scalar
: context?
Yes, that's what the wantarray operator tells you.
: A boolean scalar context?
No such animal in Perl.
: 3. Can a subroutine tell whether its invocation was part of an expression
: or was the entire expression?
:
: example: &sub($x);
: $x = (&sub ($x) + &sub2 ($y));
No. And you can't even tell that the value of &sub($x) above is
unwanted without more context, since the last expression in a block might
return it's value to the outside of the block.
So, no, you can't cheat much... :-)
Larry
tchrist@convex.COM (Tom Christiansen) (03/07/90)
In article <7283@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: >In article <JBW.90Mar5185907@bucsf.bu.edu> jbw@bucsf.bu.edu (Joe Wells) writes: >: Some questions for the great perl gods: >: >: 1. Can a subroutine tell whether it was invoked without arguments? >: >: example: &subroutine_name; > >Not that I know of. Really? $#_ has always worked for me: #!/usr/bin/perl &callme; &callme('blue'); &callme(0); &callme(7,1); &callme(); exit 0; sub callme { local($count) = $#_ + 1; # assumes $[ == 0 printf "i was called with %s argument%s\n", $count, ($count == 1) ? '' : 's'; } Is there some reason this is not reliable? --tom -- Tom Christiansen {uunet,uiucdcs,sun}!convex!tchrist Convex Computer Corporation tchrist@convex.COM "EMACS belongs in <sys/errno.h>: Editor too big!"
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/08/90)
In article <100405@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes: : Really? $#_ has always worked for me: : : : #!/usr/bin/perl : : &callme; : &callme('blue'); : &callme(0); : &callme(7,1); : &callme(); : : exit 0; : : sub callme { : local($count) = $#_ + 1; # assumes $[ == 0 : printf "i was called with %s argument%s\n", : $count, ($count == 1) ? '' : 's'; : } : : : Is there some reason this is not reliable? #!/usr/bin/perl &callme; &callfrom(1,2,3); exit 0; sub callme { local($count) = $#_ + 1; # assumes $[ == 0 printf "i was called with %s argument%s\n", $count, ($count == 1) ? '' : 's'; } sub callfrom { &callme; } Larry
allbery@NCoast.ORG (Brandon S. Allbery) (03/10/90)
As quoted from <100405@convex.convex.com> by tchrist@convex.COM (Tom Christiansen): +--------------- | In article <7283@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes: | >In article <JBW.90Mar5185907@bucsf.bu.edu> jbw@bucsf.bu.edu (Joe Wells) writes: | >: 1. Can a subroutine tell whether it was invoked without arguments? | > | >Not that I know of. | | sub callme { | local($count) = $#_ + 1; # assumes $[ == 0 | printf "i was called with %s argument%s\n", | $count, ($count == 1) ? '' : 's'; | } +--------------- More portably, local($count) = $#_ - $[ + 1; Just another Perl diver, ;-) ++Brandon -- Brandon S. Allbery (human), allbery@NCoast.ORG (Inet), BALLBERY (MCI Mail) ALLBERY (Delphi), uunet!cwjcc.cwru.edu!ncoast!allbery (UUCP), B.ALLBERY (GEnie) BrandonA (A-Online) ("...and a partridge in a pear tree!" ;-)