anonymous sub inside anonymous sub? 
Author Message
 anonymous sub inside anonymous sub?

I have a button inside a widget ($SW) which pops up a separate child window.
Inside the button's anonymous subroutine, I do the usual window construction,
including creation of anonymous subroutines to do things. But anytime I
execute some method on some previously-defined widget, Perl complains,
"Error: Can't call method "get" without package or object reference at ..."
(see note below, where I do a "$findentry->get").
In the same way, when I push the "Dismiss" button
(see bottom of this fragment), I get a similar message, but substitute
"destroy" for "get". Hope I didn't cut anything necessary out of this
fragment.

Is the problem that I cannot have an anonymous sub inside an anonymous sub?

        $lbotframe->Button(-text => 'Find',
                -command => sub
                {
                        my $SSW = $SW->Toplevel;
                        $SSW->title("Find");
                        $SSW->iconname("Find");
                        $SSW->minsize(1,1);

                        my $findframe = $SSW->Frame()->pack;
                        $findframe->Label(-text => "Find",
                                -height => 1)->pack(-side => 'left');
                        my $findentry = $findframe->Entry(
# I was going to access $find directly, below, but it didn't work
                                -textvariable => \$find,
                                # other irrelevant parameters here
                                )->pack(-side => 'right');
                        my $box = $SSW->ScrlListbox(-relief => 'sunken',
                                # other irrelevant parameters here
                        )->pack(-fill => 'both');
                        $findentry->bind('<Return>' => sub
                        {
                                my($line, $dl);
# tried to read $find here, but it is empty
#                               $find = uc $find;
# but this fails vocally
                                $find = uc($findentry->get);
                                # allow for spaces after ':' in ca:pr
                                ($myfind = $find) =~ s/:/: */;
# this is printf-style debugging
&message_box("\$find is \"$find\"\n\$myfind is \"$myfind\"");
                                # here, I fill the $box with what I found

# this is printf-style debugging

                        });
                        $findentry->focus;
                        # other irrelevant activities deleted here

                        # make a frame for buttons
                        my $bframe = $SSW->Frame()->pack(
                                -side => 'bottom',
                                -fill => 'x',
                                -before => $box);
                        $bframe->Button(-text => 'Clear',
                                -command => sub
                                {
                                        $find = "";
                                        $box->delete(0, 'end');

                                },
                                )->pack(-side => 'left');
                        $bframe->Button(-text => 'Dismiss',
# this also reaps a complaint when button is pressed
                                -command => sub { $SSW->destroy },
                                )->pack(-side => 'right');
                },
                )->pack(-side => 'left');
--
Bob McClure, Jr.       | Bobcat Open Systems, Inc.  | Cumby:  800-929-5513


1. God is still in control.  2. You are not He.



Mon, 22 Nov 1999 03:00:00 GMT  
 anonymous sub inside anonymous sub?


Quote:

> I have a button inside a widget ($SW) which pops up a separate child
> window.
> Inside the button's anonymous subroutine, I do the usual window
> construction,
> including creation of anonymous subroutines to do things.

<snip>

Bob,

As it happens, I wrote the snippet below only this morning...
you may find it of some assistance, since I think you are
looking to achieve the same thing.

I have made no attempt to analyse your code, just downloaded
this from the office.

Suggestions for improvements always welcome.....
My code isn't as succinct - no a=->b->c->pack
..brain's too slow :)
<------------------------------>
# ($but_row is a 'sub' Frame off of the listbox widget)

$find_entry = $but_row->LabEntry(-textvariable => \$findkey,
                -label => 'Find :',
                        -width => '10',
                -labelPack => [-side => "left", -anchor => "e"],
                       );

$find_entry->pack(-side => 'right', -anchor => 'e', -padx => '10');

# I _know_ contents of listbox are u/c - I made it that way...

$find_entry->bind('<KeyPress>' => sub
                       {
            $findkey = uc($findkey);
                       }
                       );

#
# OK - Like I said, I only wrote it this morning - this sub really
# be a function......
#
# The contents of this listbox are a short (< 100 lines)'flat' file
#
#  $ed_wl_list is the listbox

$find_entry->bind('<Return>' => sub
                       {
            my $c = 0;                    
            my $found = 0;                 #flag
            my $siz = $ed_wl_list->size(); #no of lines in listbox
            while($c < $siz)
                {
                $ed_wl_list->activate($c);
                $ed_wl_list->see($c);
                my $line = $ed_wl_list->get('active');
                $ed_wl_list->selectionSet($c);
# The obove 3 lines will get re-written.... but for debugging the
# sequence may be handy: A printf $line _here_ does the job, but
# a sleep(1) is prettier :)
                if($line =~ $findkey)
                    {
                    $found = 1; #I'm not looking for any more....unique
#                                key
                    $ed_wl_list->selectionSet($c);

#below, I'm activating buttons to work on the found line...
#..the conditions are immaterial to the question
                            if(defined($ed_wl_list->curselection))
                                    {
                                    $ed_but_del->configure(-state =>
'normal');
                                    $ed_but->configure(-state =>
'normal');
                                    my
$tbuf=$ed_wl_list->get($ed_wl_list->curselection);
                                    if($tbuf =~ "op=")
                                            {

$ed_but_av->configure(-state => 'disabled');
                                            }
                                            else
                                                    {

$ed_but_av->configure(-state => 'normal');
                                                    }
                                    }

                    last; #break if found
                    }
                $c++;
                }
            if($found == 0)
                {
                fes_error("$findkey not found"); #dialog box func

                }
            $findkey = ""; #zero out LabEntry
                       }
                       );

HTH

Lynn. Rickards



Wed, 01 Dec 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Differences between anonymous subs and named subs

2. anonymous sub as a parameter to a sub?

3. Scoping rules for anonymous sub's?

4. difficulty calling anonymous sub with params

5. order of declaration - my, local, anonymous sub

6. Prototypes and anonymous subs

7. anonymous subs

8. failing of call anonymous sub with my variable?

9. failing of call anonymous sub with my variable?

10. Anonymous subs, closure, and packages

11. Anonymous sub ref problem when debug mode in 5.001m

12. anonymous subs

 

 
Powered by phpBB® Forum Software