Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Global vs. local?

by lodin (Hermit)
on May 28, 2009 at 13:53 UTC ( #766669=note: print w/replies, xml ) Need Help??


in reply to Re: Global vs. local?
in thread Global vs. local?

the sub can not be called directly by name

The subroutine is not called directly, but rather dispatched by the Win32::GUI event model. Ignoring that there are better ways to handle event definitions altogether, that's an argument for doing precisely what jpavel does. That way the Win32::GUI event handlers won't accidentally collide with any "regular" subroutines in the program.

lodin

Replies are listed 'Best First'.
Re^3: Global vs. local?
by shmem (Chancellor) on May 28, 2009 at 18:18 UTC
    The subroutine is not called directly, but rather dispatched by the Win32::GUI event model.

    How is the sub accessed by the event handler?

    Ignoring that there are better ways to handle event definitions altogether, that's an argument for doing precisely what jpavel does.

    No, it's not. Polluting the name space with funny things is not a good idea. I don't know about Win32::GUI, but I guess it uses coderefs rather than typeglobs. If it doesn't, then I hope for never needing to look at that module.

      Well, guess again. The documentation describes the old event model:

      Events are Perl subs that are called in response to an event that occurred in the user interface, usually generated by an action of the user. For example, a button has a Click event that is called when the user pushes it. The naming convention for events follows the Microsoft Visual Basic's one; its form is:
      OBJECTNAME_Eventname
      (note there's an underscore in between), where OBJECTNAME is the value of the -name option used when creating the object, and Eventname is the event name, eg. Click. So if you have a button named Button1, your Click event will be defined as follows:
      sub Button1_Click { # ...do something... }
      The code inside will be executed when Button1 gets pressed.

      So yeah, it does pollute the namespace. That's why I argued that its good to make the pollution "inaccessable". The new event model uses code refs, but it's quite sparsely documented. See also Re: Global vs. local? (Win32::GUI NEM).

      <update>
      Since this seems a bit unclear maybe I can clarify. The programmer that uses Win32::GUI does not call the event subroutines himself. The only place you'll usually see "Button1_Click" in the code is in the subroutine declaration. If the programmer has done e.g.

      sub Button1_Click { ... } $main->AddButton( -name => 'Button1', ..., );
      then it is the Win32::GUI dispatcher that looks in the symbol table for a subroutine named Button1_Click when the button is clicked. Note again that this is in the old event model. In the new event model you would do
      $main->AddButton( -name => 'Button1', -onClick => sub { ... }, ..., );
      </update>

      lodin

        sub Button1_Click { # ...do something... }

        Huh? That's definitely a valid subroutine proper, as opposed to sub 1Button_Click {} right? If the old model uses typeglobs but can resolve weird names, then there's messy-symbol-table-lookup and reference-taking-from-typeglobs going on under the hood without need (since sub references could be used as callbacks in the first place.) Not the kind of code I'd consider sane.

        OTOH, if such weird lookup doesn't tale place, then your of of luck with subs constructed as the OP did, since illegal typeglobs aint for subroutine dispatch, only for internal variables.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://766669]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-01-19 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (55 votes). Check out past polls.

    Notices?