I have used Perl for a while now, but I have never even thought about using it's class capabillites. Now I am and I am dying. The code in "Perl Cookbook" didn't work, so I wrote some of my own. I was able to get some limited functionality, but it seems ridiculous compared with some of the other classes I have seen. here is my module:
package Person;
require 5.004;
#use strict;
my $Person;
$Person::version = "1.00.01";
$Person::build = "\$ID:2255518";
$Person::list = {};
sub new
{
my $class = shift;
return bless($Person::list,shift);
}
sub name
{
$Person::list->{NAME} = $_[0];
return $Person::list->{NAME};
}
sub age
{
$Person::list->{AGE} = shift if @_;
return $Person::list->{AGE};
}
sub exclaim
{
printf "I'm %s and i'm %d years old!\n",$Person::list->{NAME},$Per
+son::list{AGE};
return 1;
}
1;
And here is my test program:
#!/usr/bin/perl
use Person qw/name age exclaim/;
my $foo = Person->new();
$foo->Person::name("Stefan");
$foo->Person::name(20);
$foo->Person::exclaim();
Any one care to rip this apart and show me really and truly how to do classes in Perl? I would like to be able to call class functions without the "Person:: in front of it. I was looking at the AUTOLOAD in Perl Cookbook, but I wanted to see what the monks would come up with.Any help is appreciated. Thanx
straywalrus
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.