CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Error when Object calls subroutine

 



death_metal
Novice

May 23, 2013, 9:35 AM

Post #1 of 3 (383 views)
Error when Object calls subroutine Can't Post

I have simply created one basic perl class and an object with attributes height and name.
there is one subroutine in class - calc.
When I try to call that method, the call doesn't take place. Here is the code below.


Code
package par; 
use strict;

sub new{
my $class = @_;

my $self={
_name=>$_[1],
_height=>$_[2]
};
bless $self, $class;
return $self;

}
sub calc{
print "hellloo more";

my($self,@arg)=@_;


print $self;

}

my $h =new par('rahim',23);
print "\n";
print $h;


$h->calc;


I get error as 'Can't locate object method calc via package '3' at par.pl line 29'

Any help shall be greatly appreciated. Can someone please tell me why it is wrong?
The same thing works in other program, but it fails to work here.


FishMonger
Veteran / Moderator

May 23, 2013, 10:02 AM

Post #2 of 3 (379 views)
Re: [death_metal] Error when Object calls subroutine [In reply to] Can't Post


Code
my $class = @_;

That line is not doing what you think. $class is being assigned the number of elements in @_, not the value of the first element.

You need to change it to list context

Code
my ($class) = @_;


or reference the exact array element

Code
my $class = $_[0];


or use the shift function

Code
my $class = shift @_;


There are a couple other problems, but fixing that one will resolve the issue you're asking about.


death_metal
Novice

May 23, 2013, 10:27 AM

Post #3 of 3 (375 views)
Re: [FishMonger] Error when Object calls subroutine [In reply to] Can't Post

Hi FishMonger,
Thank you for your prompt reply.
I was completely lost in this.

Thanks much for this help. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives