homepage Welcome to WebmasterWorld Guest from 50.19.206.49
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Basic Perl object oriented example
bornlsr




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

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.

 

mark_roach




msg:4078213
 10:41 am on Feb 11, 2010 (gmt 0)

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.

bornlsr




msg:4078388
 3:06 pm on Feb 11, 2010 (gmt 0)

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.

janharders




msg:4078400
 3:21 pm on Feb 11, 2010 (gmt 0)

Package should be package, with a lower case p, that's why perl is confused.

bornlsr




msg:4078429
 3:59 pm on Feb 11, 2010 (gmt 0)

Yeah, that worked !

Thank you so much janharders and mark_roach

janharders




msg:4078600
 8:02 pm on Feb 11, 2010 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved