Hi I have a small piece of code similar to the one below:
#!/usr/bin/perl
package main;
use strict;
use warnings;
my $obj = test2->new();
$obj->get_name;
$obj->get_namer;
package test2;
sub new {
my $class = shift;
my $self = { lib => undef};
bless($self,$class);
my $lib = test1->new();
($self->{lib}) = ($lib->print_name());
return $self;
}
sub get_name {
my $self = shift;
return $self->{lib}->print_name();
}
sub get_namer {
my $self = shift;
return $self->{lib}->print_namer();
}
package test1;
sub new {
my $class = shift;
my $self = {};
bless($self,$class);
return $self;
}
sub print_name {
my $self = shift;
print "From test1","\n";
}
sub print_namer {
my $self = shift;
print "Here is another namer","\n";
}
The above code is saved into a file called 'll.pl'. When I execute the above code as './ll.pl', I get the following message
From test1
Can't call method "print_name" without a package or object reference a
+t ./ll.pl line 27.
can you please help me understand what I am doing wrong?
Best Regards,
Dayle Kotturi.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|