Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Variable scope with XML::Parser

by Basilides (Friar)
on Mar 17, 2003 at 12:43 UTC ( [id://243628]=note: print w/replies, xml ) Need Help??


in reply to Re: Variable scope with XML::Parser
in thread Variable scope with XML::Parser

OK, so global variables, which usually get a right slagging off, are the only solution here?
  • Comment on Re: Re: Variable scope with XML::Parser

Replies are listed 'Best First'.
Re: Re: Re: Variable scope with XML::Parser
by zby (Vicar) on Mar 17, 2003 at 13:03 UTC
    Ah that! For sure you can resolve this for many ways. Here is one (it might be a bit convoluted, I admit):
    my $p = new XML::Parser (Handlers => {Start => sub{hdl_start($tag, $na +me, @_)}, ...
    and
    sub hdl_start { $_[0] = $_[2]; $_[1] = $_[3]; }
    What I use here is an anonymous subroutine as the handler, and I've changed the hdl_start subroutine to change it's two first parameters ($_[0] and $_[1]).
Re: Variable scope with XML::Parser
by dragonchild (Archbishop) on Mar 17, 2003 at 16:22 UTC
    Have just read the Streams chapter in Perl and XML (a book I highly reccomend), I offer the following solution - you can pass a class to XML::Parser instead of the specific handlers. You can then have the class maintain the state, instead of global variables.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Re: Re: Variable scope with XML::Parser
by grantm (Parson) on Mar 17, 2003 at 19:01 UTC
    OK, so global variables, which usually get a right slagging off, are the only solution here?

    Well, you can use closures to avoid globals, but a better solution is to not use XML::Parser directly, but use SAX instead. Or depending on your application, an even better solution might be to use a higher level module like XML::Twig or XML::XPath (etc).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2024-04-18 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found