in reply to Proper syntax for using a method as a call back routine?

The callback routine, _add_link, is another method within the object and needs access to some of the object's data to work properly.

Is this something you could profitably use local for?

E.g.,

local $instance = $self; $parser->handler(start => \_add_link, 'tagname,attr'); ... sub _add_link { my $self = $instance; ...
This makes _add_link a sort of spring-loaded method call.

Update: Forget this, and go with BrowserUk's suggestion below. Using a closure is a superior approach.

Replies are listed 'Best First'.
Re: Re: Proper syntax for using a method as a call back routine?
by BrowserUk (Patriarch) on Jul 08, 2003 at 03:15 UTC

    That isn't going to work. As soon as the method containing the the call to $parser->handler() exits, (or the nearest local scope), then the value assigned to $instance will be forgotten as the global $instance reverts to its former value and whne the _add_link() routine is called back by the parser, $instance could have just about any value.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      That isn't going to work.

      I assumed that the parser would be called on to do its thing within the scope of the current method. If that's not the case, consider stashing $self into a package variable.

        I assumed that the parser would be called on to do its thing within the scope of the current method.

        In which case, it would work, and I'm talking out my lower lumbar region again :)

        Needless to say, I assumed that it wouldn't be called until later, which was the case when I 'discovered' this problem, and after falling foul of trying to push and pop instance handles for multiple instances, arrived at the work-around of passing a ref to a closure around a call to the method.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller