Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Yes, if I undestand you correctly :>) I paste a piece of my code here, which demonstrates the object and tied hash simultaneously.
package XML::Trivial::Element; use Scalar::Util 'weaken'; use strict; use warnings; sub new { my ($class, $aref, $nsstack) = @_; tie my %h, $class, $aref || [], $nsstack; my $self = bless \%h, $class; my %ehns; my $key; my $s = tied(%$self); foreach (@{$s->{ea}}) { tied(%$_)->{parent} = $self; weaken(tied(%$_)->{parent});#because it is circular ref $key = $_->ns(undef).'*'.$_->ln(); $ehns{$key} = $_ unless exists $ehns{$key}; } $s->{ehns} = \%ehns; return $self; } sub TIEHASH { my ($class, $a, $nsstack) = @_; #$a is arrayref like [name, atts, type1, data1, type2, data2, ...] my @ea; my %eh;#elements my @ta; #texts my @ca; #cdatas my @pa; my %ph;#process instructions my @na; #notes my $firstkey; my $lastkey; my %next; my %nh; #hash of namespaces in scope foreach (@$nsstack) { while (my ($name, $value) = each %$_) { $nh{$name} = $value; } } for (my $i = 0; $i < @$a; $i += 2) { #too much code here } return bless {a=>$a, ea=>\@ea, eh=>\%eh, ta=>\@ta, ca=>\@ca, pa=>\@pa, ph=>\%ph, na=>\@na, nh=>\%nh, parent=>undef, firstkey=>$firstkey, next=>\%next }, $class; } sub FETCH { my ($self, $key) = @_; $key =~ /^\d+$/ and return $$self{ea}[$key]; $key =~ /\*/ and return $$self{ehns}{$key}; return $$self{eh}{$key}; } sub EXISTS { my ($self, $key) = @_; $key =~ /\*/ and return exists $$self{ehns}{$key}; return exists $$self{eh}{$key}; } sub FIRSTKEY { return $$_[0]{firstkey}; } sub NEXTKEY { return $$_[0]{next}{$$_[1]}; } sub SCALAR { return $$_[0]{a}[0]; } #this is object method sub p { #parent my ($self) = @_; return tied(%$self)->{parent}; }

In reply to Re: Tying objects by pajout
in thread Tying objects by Alien

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? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2023-09-27 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?