package - create dispatch table inside class constructor in perl -
i trying create dispatch table inside class constructor.execution fails below mentioned error.
error: use of uninitialized value in subroutine entry @ ../lib/parser.pm . can't use string ("") subroutine ref while "strict refs" in use @ ../lib/parser.pm.
code:
package parser; use strict; use warning; @packet = ("join","release","status"); #constructor sub new { ($class) = shift; $self = { _callermdn => shift, _calleelist => shift, _serverip => shift, _packethandler => { join => \&joinhandler, #dispatch table,variable "join" stores func reference release => \&releasehandler, #variable "release" stores func reference status => \&statushandler #variable "stores" stores func reference }, _mdnhandler => {}, }; print ("the server ip = $self->{_serverip}\n") if ($debug); print ("callermdn = $self->{_callermdn}\n") if ($debug); print ("tcpdump file name = $self->{_tcpdumpfile}\n") if ($debug); bless( $self, $class ); return $self; } sub start { ($self,$data) = @_; if ($data ~= "incoming packet") { $self->{_packethandler}->{$packet[0]}->($data);#**error while calling "joinhandler" function** } elsif ($data ~= "outgoing packet"){ $self->{_packethandler}->{$packet[1]}->($data);#**error while calling "releasehandler" function** } else { $self->{_packethandler}->{$packet[2]}->($data);#**error while calling "statushandler" function** } } sub joinhandler { ($self,$data) = @_; #parse packet print ("incoming packet parsed"); } sub releasehandler { ($self,$data) = @_; #parse packet print ("outgoing packet parsed"); } sub statushandler { ($self,$data) = @_; #parse packet print ("status packet"); }
please me understand , resolve issue.
i see quite few problems.
use warning;
shoulduse warnings;
you forgot
my @packet
@ top ofpackage
in
sub start
have=~
backwards~=
. shouldeq
anyways.$debug
isn't declared anywhere.
you might find useful take advantage of perl -c
, checks syntax(also runs begin blocks too), when run problems.
Comments
Post a Comment