in reply to tk option+value in variable?

No advanced referencing is needed, just use an array of options instead of a string:
my @opt = (-fg => $fg, -bg => $bg); push @opt, -font => $newfont if length $newfont; $mw->Label(-text => "Col. 1", @opt, -padx => 10)->grid( $mw->Label(-text => "Col. 2", @opt, -padx => 11), $mw->Label(-text => "Col. 3", @opt, -padx => 11));
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: tk option+value in variable?
by cniggeler (Sexton) on Jan 25, 2019 at 21:04 UTC
    Bingo! Thanks so much!

      Maybe this should be a nother top-level question, but can you tell me know you knew to promote my string to an array? I was playing with subroutines and arguments, and I found that all these generated the same answer (5):

      my $a = 2; my $b = 3; print &add($a, $b) . "\n"; my $opt = "$a, $b"; print &add(eval($opt)) . "\n"; my @opt = ($a, $b); print &add(@opt) . "\n"; exit; sub add { my ($x, $y) = @_; return ($x +$y); }
      Another area my knowledge is lacking is the dash notation for the options (-font => 'courier', for example); is that common in other areas of perl or specific to Tk?
        Use split to split a string into a list.
        #!/usr/bin/perl use warnings; use strict; use List::Util qw{ sum }; my ($x, $y) = (2, 3); my $opt = "$x, $y"; my @opts = split /,\s*/, $opt; print sum(@opts);

        The dashes are just a convention. They were somehow common at the time Perl/Tk was created, so there might be other modules using them. See the explanation of "unary -" in perlop.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        ... [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:

        • What's the difference between feeding the strings  "$x, $y" and  '$x, $y' to eval?
        • What's  @ra[ 2, 1 ] all about? (Hint: see Slices.)

        ... 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:  <%-{-{-{-<