Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Can't locate object method "new" via package" 1

Status
Not open for further replies.

jollekox

Programmer
Feb 3, 2004
23
0
0
NO
Hi, I have decided to pick up object-oriented perl programming, and I feel fascinated by packages.

Although, when I run the script below, I only get:
Can't locate object method &quot;new&quot; via package &quot;Packet&quot; at /Users/jas/Desktop/oop.pl line 9, <STDIN> line 1.

I can't make out anything of the error message, what is wrong, and what should I do? Thanks in advance!

####
#oop.pl
####
#!/usr/bin/perl -w
package Packet;
use strict;

print &quot;Enter number to be altered:\n&quot;;
my $input = <STDIN>;
chomp($input);

my $object = Packet->new($input);
my $result = $object->alter();

print &quot;$result\n&quot;;


####
#Packet.pm:
####
package Packet;
use strict;

sub new {
my $new_num = shift;
bless $new_num;
}

sub alter {
my $temp = shift;
$temp += 5;
return $temp;

}

1;
 
Here you go..

####
#oop.pl
####
#!/usr/bin/perl -w

use strict;

use Packet;

my ($obj) = Packet->new(@_);


print &quot;Enter number to be altered:\n&quot;;
my $input = <STDIN>;
chomp($input);

my $result = $obj->alter($input);


print &quot;$result\n&quot;;
[/i]

####
#Packet.pm:
####
package Packet;
use strict;


sub new {
my $class = shift;

my $self = {};
bless $self, $class;
}


sub alter {
my $temp = shift;
$temp += 5;
return $temp;
}

1;

M. Brooks
X Concepts LLC
 
OOPS forgot.. Add this too.

sub alter {
my $self = shift;

my $temp = shift;
$temp += 5;
return $temp;
}

M. Brooks
X Concepts LLC
 
Wow! It works! Thanks a lot for your help! ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top