P
Paul
Hi all:
I was wondering if someone could help me find a *better* or more
*correct* solution to my problem.
The problem:
I have a module that parses XML data using XML:
arser. This module
is written in Object oriented fashion and defines the object in the
normal way...
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
# No args by default, args is an array ref
$self = { acode => '',
file => '',
@_
};
bless $self, $class;
return $self;
}
THe instance method parseTIGRFile is called to process the XML data
file. Within this method, XML:
arser is called and a handler is
passed to deal with XML start tags (actually there are 3 handlers, but
the question/answer is the same for all 3 handlers)....
sub parseTIGRFILE {
my $self = shift;
......
$parser = new XML:
arser(Handlers => { Start => \&handle_start,
End => \&handle_end,
Char => \&handle_char,
} );
......
}
sub handle_start {
my ($expat, $elem, @atvals)= @_;
......
}
Here is the problem... the handlers cannot not take arguments (unless
I am wrong about this... ???), so I cannot pass $self to the handler.
While the data is being parsed, I want it stored in the object's
instance variables $self->{MY_VAR}, but the handler doesn't have any
way to *see* $self.
My current solution... I have defined $self globally.
my $self;
sub new {
....
bless $self, $class;
return $self;
}
Since the parseTIGRFile method is an instance method, this
appropriately sets the instance variables (I have tested this).
However, I fear there may be a consequence to globalizing $self that I
am unaware of.
So... can anyone tell me:
1. Is my current solution acceptable?
2. Is there a better alternative?
Thanks in advance for your help... and please let me know if more
information is required.
Paul
I was wondering if someone could help me find a *better* or more
*correct* solution to my problem.
The problem:
I have a module that parses XML data using XML:
is written in Object oriented fashion and defines the object in the
normal way...
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
# No args by default, args is an array ref
$self = { acode => '',
file => '',
@_
};
bless $self, $class;
return $self;
}
THe instance method parseTIGRFile is called to process the XML data
file. Within this method, XML:
passed to deal with XML start tags (actually there are 3 handlers, but
the question/answer is the same for all 3 handlers)....
sub parseTIGRFILE {
my $self = shift;
......
$parser = new XML:
End => \&handle_end,
Char => \&handle_char,
} );
......
}
sub handle_start {
my ($expat, $elem, @atvals)= @_;
......
}
Here is the problem... the handlers cannot not take arguments (unless
I am wrong about this... ???), so I cannot pass $self to the handler.
While the data is being parsed, I want it stored in the object's
instance variables $self->{MY_VAR}, but the handler doesn't have any
way to *see* $self.
My current solution... I have defined $self globally.
my $self;
sub new {
....
bless $self, $class;
return $self;
}
Since the parseTIGRFile method is an instance method, this
appropriately sets the instance variables (I have tested this).
However, I fear there may be a consequence to globalizing $self that I
am unaware of.
So... can anyone tell me:
1. Is my current solution acceptable?
2. Is there a better alternative?
Thanks in advance for your help... and please let me know if more
information is required.
Paul