At 06:24 PM 2/23/04, wolf blaum wrote:
On Monday 23 February 2004 18:31, Michael Ragsdale generously enriched virtual reality by making up this one:

> my $ent =$right->Entry->pack;
^line 1

> my $go = $right->Button(-command=>sub{compute($ent)})->pack;
^line2

> MainLoop();
>
> sub compute {
>  [...]
>      $sth->execute($_[0]) or die "Cannot execute query: $DBI::errstr\n";
^line 3

> When I enter an id in the Entry widget and click on the button, I get the
> following error message:

This

sub compute {
my $msg = " You passed ".$_[0]->get()." to compute.";
$main->messageBox(-message=>"$msg", -type=>'OK',-icon=>'info');
}

works.


Thanks. After about 3 hours of Googling and then sending my original post, I finally stumbled on an example of the get() function. I had already seen it in the docs, but I was using it like:

my $input = $ent->get();
my $go = $right->Button(-command=>sub{compute($input)})->pack;
sub compute {
    $sth->execute($_[0]) or die.....
}

and of course I got the same "can't bind a reference" error.

After seeing the example, I changed my code to...

my $go = $right->Button(-command=>sub{compute($ent)})->pack;
sub compute {
    my $input = $_[0]->get();
    $sth->execute($input) or die.....
}

and everything worked fine.

However, dont pass the object - pass a refernece to it in line 2, like:
my $go = $right->Button(-text=>'Get Data',-command=>sub{compute(\
$ent)})->pack(-side=>'top');

and use $$_[0]->get in compute


I see the advantages of passing by reference, however, when I added the \ to pass the reference of $ent and the $ to dereference $_[0], it no longer worked. These were the only two changes and it left me with...

my $go = $right->Button(-command=>sub{compute(\$ent)})->pack;

sub compute{
    my $input = $$_[0]->get();
    $sth->execute($input) or die.....
}

The error was "can't call method get on an undefined value". Now I'm really confused. Why would passing the object work, but passing the reference and then dereferencing in the sub doesn't?

-Mike


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to