Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sub {} inside of a foreach loop using $_ 1

Status
Not open for further replies.

xhonzi

Programmer
Jul 29, 2003
196
US
So, I know this shouldn't be as hard as I'm making it,
but I've having trouble using $_ in a sub {} in a foreach loop.

Code:
@names=qw\Matt Todd Justin\;
foreach (@names) {
  $mw->Button(-title=>"$_",-command=>sub{print "My name is $_!\b"})->pack;
}

My $_ ends up being a "" when I push the button. I know there's a way to do this, and, in fact, I know I've done it before but for the life of me I can't figure it out.

xhonzi
 
I meant to also say that the title on the button shows up correctly. But I get "My name is !" from the print statement.
 
Well, the most probable solution is don't use $_.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@names[/blue] = [red]qw([/red][purple]Matt Todd Justin[/purple][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$name[/blue] [red]([/red][blue]@names[/blue][red])[/red] [red]{[/red]
	[blue]$mw[/blue]->[maroon]Button[/maroon][red]([/red]
		-[purple]title[/purple]   => [blue]$name[/blue],
		-[purple]command[/purple] => [url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [red]{[/red][url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]My name is [blue]$name[/blue]![purple][b]\b[/b][/purple][/purple][red]"[/red][red]}[/red],
	[red])[/red]->[maroon]pack[/maroon][red];[/red]
[red]}[/red]

Also, be sure to use strict in your code.

- Miller
 
But I love $_.

You can't 'my' in a foreach (@names) can you?

Frankly, your solution would work better, but I did, while writing this, think of adding a
Code:
 my $name=$_;[\code] in the loop so I could use $name and that worked. 

Thanks for the prompt reply.

xhonzi
 
If you want to write maintainable code, then I strongly advise against the use of $_ except for the most rudimentary purposes such as in a map or grep clause. For any other time, you'll save yourself and others a lot of headache if you give your variables meaningful names.

Don't let laziness, and the desire to be cute, lead you into ineffectual coding practices.

And yes, you can include the my declaration for the iterator in a for or foreach.

If you insisted on using $_, this is how I would probably do it:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@names[/blue] = [red]qw([/red][purple]Matt Todd Justin[/purple][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [red]([/red][blue]@names[/blue][red])[/red] [red]{[/red]
    [blue]$mw[/blue]->[maroon]Button[/maroon][red]([/red]
        -[purple]title[/purple]   => [blue]$_[/blue],
        -[purple]command[/purple] => [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [red]qq{[/red][purple]sub {print "My name is \Q$_\E!\b"}[/purple][red]}[/red],
    [red])[/red]->[maroon]pack[/maroon][red];[/red]
[red]}[/red]

- Miller
 
While MillerH's suggestion works, Tk actually has something to handle this kind of thing built in.

Code:
my @names = qw(Matt Todd Justin);
foreach (@names) {
   $mw->Button (
      -text => $_,
      -command => [
         sub {
            my $name = shift;
            print "My name is \Q$name\E\b";
         },
         $_,
       ],
   )->pack;
}

Basically, the -command attribute takes an array reference, where the first element is the subroutine, and the following elements are data to send to the subroutine. Sometimes $_[0] will be a reference to the widget itself though, so watch out for that (i.e. iirc Tk::Listbox's $_[0] is a reference to the listbox instead of the first bit of data you sent).

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top