... [how] you knew to promote my string to an array?
A subroutine such as your add() can be called with an argument list; choroba just made the arguments you wanted to pass into an expression (specifically, an array) that would be accepted as a list expression by a subroutine call; see perlsub. (BTW: There's no need to use the & sigil for a subroutine call. Indeed, you should avoid this syntax because it has some oddball effects; see especially the paragraph beginning 'A subroutine may be called using an explicit "&" prefix.' in the DESCRIPTION section of perlsub.) (Also BTW: IMHO it's not, in general, a good idea to use the variable names $a $b as lexical names even in short example code; these are Perl special variables and have special significance and usage; see perlvar.)
... arguments ... all these generated the same answer ...
I'll avoid using the & sigil prefix in all discussion and examples.
print add($a, $b) . "\n";
Pass a list of two scalars (list elements are always scalars) to the add subroutine.
my $opt = "$a, $b";
print add(eval($opt)) . "\n";
First, eval the Perl string list expression "$a, $b" and then pass the resulting list returned by eval to the subroutine.
my @opt = ($a, $b);
print add(@opt) . "\n";
Expand an array to the argument list of a subroutine call.
c:\@Work\Perl\monks>perl -wMstrict -le "my ($x, $y) = (2, 3); ;; print add($x, $y); ;; my $opt_dq = \"$x, $y\"; print add(eval($opt_dq)); ;; my $opt_sq = '$x, $y'; print add(eval($opt_sq)); ;; my @opt = ($x, $y); print add(@opt); ;; my @ra = (99, @opt, 98); print add(@ra[ 2, 1 ]); ;; exit; ;; sub add { my ($x, $y) = @_; return $x + $y; } " 5 5 5 5 5
Questions:
... dash notation ...
This is notation used in Tcl/Tk that was, AFAIU, intentionally imported into | implemented in Perl/Tk just to make it look more familiar. Otherwise, the => is just a "fat comma"; see perlop.
Give a man a fish: <%-{-{-{-<
In reply to Re^4: tk option+value in variable?
by AnomalousMonk
in thread tk option+value in variable?
by cniggeler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |