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

Strange Compilation Failed in Require Error

by Perllace (Acolyte)
on Apr 05, 2011 at 05:04 UTC ( [id://897441]=perlquestion: print w/replies, xml ) Need Help??

Perllace has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I've written a couple of perl wrapper modules for a C# Tool. The idea is that the script will pass a device name, port number and server address which is used to establish a communication socket. When I write a script to use these modules I get a Compilation Failed in require error

"Can't call method "_serveraddr" on an undefined value at Device.pm line 23. Compilation failed in require at Launch.pl line 11. BEGIN failed--compilation aborted at Launch.pl line 11."

Launch.pl
use Device; use System; my ( $serverAddress, $port, $reportFile ) = @_; my $System = new System($serverAddress, $port); my $dut = new Device('DEV',127.0.0.1,5000);
Device.pm
package Device; use Command; use Comm; sub new { my $class = shift; my $self = { _device => shift, _serveraddr => shift, _port => shift }; bless $self, $class; return $self; } my $SockObj = Comm->new( $self->_serveraddr, $self->_port ); my $ComObj = Comm->new(); sub Action1 { my ( $self, $x, $y ) = @_; my $tmp = { 'hash1' => 'Command', 'value1' => $x, 'value2' => $y, 'Device' => $self->{_device} }; $InputRequest = $ComObj->CreateInputString($tmp); $SockObj->WriteInfo($InputRequest); my $Response = $SockObj->ReadData(); $ComObj->TapResponse($Response); }
System.pm
package System; use Comm; use Command; my $SockObj = Comm->new(); my $ComObj = Command->new(); sub new { my $class = shift; my $self = { _serveraddr => shift, _port => shift }; bless $self, $class; return $self; }
Could someone please help..I have no clue how to move forward..Please let me know if the question is unclear.. Thanks a lot, Perllace

Replies are listed 'Best First'.
Re: Strange Compilation Failed in Require Error
by chromatic (Archbishop) on Apr 05, 2011 at 05:14 UTC

    If your line 23 looks something like this:

    my $SockObj = Comm->new( $self->_serveraddr, $self->_port );

    ... the error message means that $self contains an undefined value. If you use the strict pragma, Perl will tell you that you haven't declared a variable named $self in that scope. That's the most likely explanation as to why its value is undefined.

Re: Strange Compilation Failed in Require Error
by wind (Priest) on Apr 05, 2011 at 05:15 UTC
    This line is your problem.
    my $SockObj = Comm->new( $self->_serveraddr, $self->_port );

    A few issues

    • 1) You need to use strict and warnings
    • 2) There is no $self at this point in the program, did you mean to include this code in a method?
    • 3) Access the attributes of an object using hash notation.

    Perhaps this is closer to what you want for that line:

    sub sock_object { my $self = shift; return $self->{_sock_object} ||= Comm->new( $self->{_serveraddr}, +$self->{_port} ); }
      Thanks for that..Using Strict and Warnings helped me remove some errors. Howvere, I'm still getting the Compilation failed in require error in use Device.pm Some of my questions are: In the code that you sent, what's the operator "|| = " ? In the code, could I add a line
      my $SockObj = $self->sock_object;
      in order to call that particular socket object.
      sub Action1 { my ( $self, $x, $y ) = @_; my $tmp = { 'hash1' => 'Command', 'value1' => $x, 'value2' => $y, 'Device' => $self->{_device} }; $InputRequest = $ComObj->CreateInputString($tmp); my $SockObj = $self->sock_object; $SockObj->WriteInfo($InputRequest); my $Response = $SockObj->ReadData(); $ComObj->TapResponse($Response); }
      Thanks so much.. You're inputs gave me some valid direction.

        Operators can be looked up at perlop. Just search for '||='.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://897441]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-19 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found