From andy at pasty.org.uk Tue May 24 09:14:44 2005 From: andy at pasty.org.uk (Andrew McGregor) Date: Tue, 24 May 2005 16:14:44 -0000 (GMT) Subject: [Bath-pm] OO / global instance variable Message-ID: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> Hi, An OO question. If I have an instance of an object: my $ins = My::Obj->new(); Which does a job: $ins->read_xml(); in turn triggers the event: handle_char_data(); How can I get $text into the anonymous hash referened by $self? Regards, Andy package My::Obj; use XML::Parser; sub handle_char_data { my ($expat, $text); # how do I populate $self{text} with $text } sub new { my ($this, %params) = @_; my $class = ref($this) || $this; my $self = {}; $self->{parser} = XML::Parser->new ( Handlers => { Char => \&handle_char_data, } ); bless $self, $class; return $self; } sub read_xml { my $this = shift; my $xml = ; $this->{parser}->parse($xml); } 1; From andy at pasty.org.uk Tue May 24 09:26:14 2005 From: andy at pasty.org.uk (Andrew McGregor) Date: Tue, 24 May 2005 16:26:14 -0000 (GMT) Subject: [Bath-pm] OO / global instance variable In-Reply-To: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> References: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> Message-ID: <64954.62.173.88.110.1116951974.squirrel@webmail.pasty.org.uk> > An OO question. The same way as any other global instance variable I guess :) package My::Obj; use XML::Parser; my $var; sub handle_char_data { my ($expat, $text); $var = $text } From jns at gellyfish.com Tue May 24 10:39:41 2005 From: jns at gellyfish.com (Jonathan Stowe) Date: Tue, 24 May 2005 17:39:41 +0000 Subject: [Bath-pm] OO / global instance variable In-Reply-To: <64954.62.173.88.110.1116951974.squirrel@webmail.pasty.org.uk> References: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> <64954.62.173.88.110.1116951974.squirrel@webmail.pasty.org.uk> Message-ID: <1116956381.3598.4.camel@localhost> On Tue, 2005-05-24 at 16:26, Andrew McGregor wrote: > > An OO question. > > The same way as any other global instance variable I guess :) > > package My::Obj; > > use XML::Parser; > > my $var; > > sub handle_char_data { > my ($expat, $text); > $var = $text > } > You can use a private key in the $expat thing - $expat->{_text} = $text; /J\ -- This e-mail is sponsored by http://www.integration-house.com/ From bath-pm at trout.me.uk Tue May 24 10:27:58 2005 From: bath-pm at trout.me.uk (Matt S Trout) Date: Tue, 24 May 2005 18:27:58 +0100 Subject: [Bath-pm] OO / global instance variable In-Reply-To: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> References: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> Message-ID: <20050524172758.GA25214@central.redice.net> On Tue, May 24, 2005 at 04:14:44PM -0000, Andrew McGregor wrote: > If I have an instance of an object: > > my $ins = My::Obj->new(); > > Which does a job: > > $ins->read_xml(); > > in turn triggers the event: > > handle_char_data(); > > How can I get $text into the anonymous hash referened by $self? > > Regards, > > Andy > > > package My::Obj; > > use XML::Parser; > > sub handle_char_data { > my ($self, $expat, $text); # ****** > > $self->{text} = $text; # ****** > } > > sub new { > my ($this, %params) = @_; > my $class = ref($this) || $this; > > my $self = {}; > > $self->{parser} = XML::Parser->new ( Handlers => { > Char => sub { $self->handle_char_data(@_) }, # ****** > } ); > > bless $self, $class; > return $self; > } > > sub read_xml { > my $this = shift; > > my $xml = ; > > $this->{parser}->parse($xml); > } > > 1; How about the above? (altered lines marked *****) -- Matt S Trout Website: http://www.shadowcatsystems.co.uk Technical Director E-mail: mst (at) shadowcatsystems.co.uk Shadowcat Systems Ltd. From andy at pasty.org.uk Wed May 25 05:41:36 2005 From: andy at pasty.org.uk (Andrew McGregor) Date: Wed, 25 May 2005 12:41:36 -0000 (GMT) Subject: [Bath-pm] OO / global instance variable In-Reply-To: <1116956381.3598.4.camel@localhost> References: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> <64954.62.173.88.110.1116951974.squirrel@webmail.pasty.org.uk> <1116956381.3598.4.camel@localhost> Message-ID: <33108.62.173.88.110.1117024896.squirrel@webmail.pasty.org.uk> > You can use a private key in the $expat thing - > > $expat->{_text} = $text; > Great, thank you. From andy at pasty.org.uk Wed May 25 05:42:56 2005 From: andy at pasty.org.uk (Andrew McGregor) Date: Wed, 25 May 2005 12:42:56 -0000 (GMT) Subject: [Bath-pm] OO / global instance variable In-Reply-To: <20050524172758.GA25214@central.redice.net> References: <64884.62.173.88.110.1116951284.squirrel@webmail.pasty.org.uk> <20050524172758.GA25214@central.redice.net> Message-ID: <33112.62.173.88.110.1117024976.squirrel@webmail.pasty.org.uk> >> sub handle_char_data { >> my ($self, $expat, $text); # ****** >> >> $self->{text} = $text; # ****** >> } >> >> sub new { >> my ($this, %params) = @_; >> my $class = ref($this) || $this; >> >> my $self = {}; >> >> $self->{parser} = XML::Parser->new ( Handlers => { >> Char => sub { $self->handle_char_data(@_) }, # ****** >> } ); >> >> bless $self, $class; >> return $self; >> } > > How about the above? (altered lines marked *****) > Thanks, I tried that to get the same effect sooo many times.