Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi there, a friend of mine has been struggling with an aspect of XML::Twig. He wants to be able to call private methods on his object from within the handlers. However the problem is that the handler params are the $twig and the element.

The solution i suggested is as follows, where the create_sub method is called which then creates a reference to $self before it is assigned to the handler.

Is this the correct way to do this? Or can someone suggest a better way.

#!/usr/bin/perl -w use warnings; use strict; package Stupid; { use XML::Twig; sub new { my ( $caller, $args ) = @_; my $class = ref($caller) || $caller; my $self = {}; $self->{a_note} = "This should print something\n"; bless $self, $class; my $code_ref = $self->create_sub; my $t = XML::Twig->new( twig_handlers => { 'a/b' => $code_ref +} ); my $xml = "<a><b>3</b><c>4</c></a>"; $t->parse($xml); $t->flush; } sub create_sub { my $hidden_self = shift; return sub { my ( $self, $t ) = @_; print ref($self) . "\n"; print ref($t) . "\n"; print $t->text; print "\n\n"; $hidden_self->gah(); }; } sub gah { my $self = shift; print ref($self) . "\n"; print "$self->{a_note}\n"; } }; my $s = Stupid->new();

In reply to XML::Twig and closures. by absolut.todd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found