Use of string var within qw//

J

joe.cipale

I am attempting to build a Tk:Optionmenu pull down from a string that
is passed to a sub-routine from one of my menu items. The string may
look like this:
2 3 8 10 14

The string WILL change, which is why I want to pass the string to my
menu pulldown as such:

my @slt_opt = qw/\$slt/;

$frame3->Label(-text => "Slot ID: ")->pack(-side => 'left',
-anchor => 's',
-expand => 1);

$frame3->Optionmenu(-variable => \$slots,
-options => [@slt_opt],
-command => [sub {print "args=@_\n"}, 'First']);

======================================
Am I reaching with what qw can do here?

Regards,

Joe
 
P

Paul Lalli

I am attempting to build a Tk:Optionmenu pull down from a string that
is passed to a sub-routine from one of my menu items. The string may
look like this:
2 3 8 10 14

The string WILL change, which is why I want to pass the string to my
menu pulldown as such:

my @slt_opt = qw/\$slt/;

You have just created the following array:
@slt_opt = ('\$slt');
That is, an array with one element - the string containing a slash, a
dollar sign, and the letters 's','l','t'. I am not clear on whether or
not that's what you want. I find it likely, however, that you wanted an
array containing the variable $slt. For that, you would do:
my @slt_opt = $slt;
or if you feel the need for explict parens:
my @slt_opt = ($slt);

$frame3->Label(-text => "Slot ID: ")->pack(-side => 'left',
-anchor => 's',
-expand => 1);

$frame3->Optionmenu(-variable => \$slots,
-options => [@slt_opt],
-command => [sub {print "args=@_\n"}, 'First']);

======================================
Am I reaching with what qw can do here?

Your post does not make clear what you expect qw// to do. qw// takes a
space-separated list of barewords, and transforms it into a list of
single-quoted strings. Under no circumstances does variable
interpolation happen in qw//. To wit:

qw/foo bar baz/ is equivalent to ('foo', 'bar', 'baz')
qw/$name $id $addr/ is equivalent to ('$name', '$id', '$addr') NOT
($name, $id, $addr).

Does this answer your question?

Paul Lalli
 
P

Paul Lalli

I am attempting to build a Tk:Optionmenu pull down from a string that
is passed to a sub-routine from one of my menu items. The string may
look like this:
2 3 8 10 14

The string WILL change, which is why I want to pass the string to my
menu pulldown as such:

my @slt_opt = qw/\$slt/;

$frame3->Label(-text => "Slot ID: ")->pack(-side => 'left',
-anchor => 's',
-expand => 1);

$frame3->Optionmenu(-variable => \$slots,
-options => [@slt_opt],
-command => [sub {print "args=@_\n"}, 'First']);

Reading this again, I *think* I understand. Are you looking to have
the -options parameter be a reference to an anonymous array containing
2, 3, 8, 10, 14? If so, you are indeed confused about qw//. In fact,
I'd say you're assuming the exact *opposite* of qw//'s functionality.
:)

Instead, you should populate the anonymous array with the return value
of the split() function:
my $slt = '2 3 8 10 14';
.. . .
-options => [split ' ', $slt]

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top