Welcome to WebmasterWorld Guest from 54.166.224.46

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Basic Perl object oriented example

     
5:05 am on Feb 11, 2010 (gmt 0)

5+ Year Member



I am trying to write a simple code using object oriented concepts, but could not.

Can anybody please help.

File name : Mycalc.pm
-------------------------------
#!/usr/bin/perl
Package Mycalc;

sub _area {
my $length = shift;
my $breadth = shift;
return $length * $breadth;
}
sub _perimeter {
my $length = shift;
my $breadth = shift;
return 2 *( $length + $breadth );
}

1;
------------
File name : test.pl
#!/usr/bin/perl
use Mycalc;

my $area = &_area(5,6);
print $area;
------------
Getting errors
Undefined subroutine &main::_area called at test.pl line 4.
10:41 am on Feb 11, 2010 (gmt 0)

10+ Year Member



The way I would code this is:

my $area = Mycalc::_area(5,6);

I have no idea if it is the best way to do it, but it works for me.
3:06 pm on Feb 11, 2010 (gmt 0)

5+ Year Member



Now I am getting errors like

Can't locate object method "Package" via package "Mycalc" (perhaps you forgot to load "Mycalc"?) at Mycalc.pm line 2.
Compilation failed in require at test.pl line 2.
BEGIN failed--compilation aborted at test.pl line 2.
3:21 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Package should be package, with a lower case p, that's why perl is confused.
3:59 pm on Feb 11, 2010 (gmt 0)

5+ Year Member



Yeah, that worked !

Thank you so much janharders and mark_roach
8:02 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



You're welcome. It's always good to see a little proof of life in the perl-section.
one could argue that the perl-programmers just have less issues than the fallen ones that have sold their soul to php, but I'm afraid, we're simply shrinking in numbers.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month