[Bath-pm] OO / global instance variable

Matt S Trout bath-pm at trout.me.uk
Tue May 24 10:27:58 PDT 2005


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 = <STDIN>;
> 
> 	$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.


More information about the Bath-pm mailing list