I started learning how to make a module in perl with perltoot:
package Person;
use strict;
my($NAME, $AGE, $PEERS) = ( 0 .. 2 );
sub new {
my $self = [];
$self->[$NAME] = undef;
$self->[$AGE] = undef;
$self->[$PEERS] = [];
bless($self);
return $self;
}
sub name {
my $self = shift;
if (@_) { $self->[$NAME] = shift }
return $self->[$NAME];
}
sub age {
my $self = shift;
if (@_) { $self->[$AGE] = shift }
return $self->[$AGE];
}
sub peers {
my $self = shift;
if (@_) { @{ $self->[$PEERS] } = @_ }
return @{ $self->[$PEERS] };
}
1;
- I would like to know how, if possible with sample code should I threat any errors within the module and outside it ?
For example:
use Person;
$test= Person->new() or die Person->Error;
or
sub new {
my $self = [];
$self->[$NAME] = undef;
$self->[$AGE] = undef;
$self->[$PEERS] = [];
bless($self);
#########
# some error happened here and I need to say something
#########
return $self;
}
- If I call anything else within the module that had a error, problem, missing argument how is the correct way to tell there was an error ?
PS: Hope my question is not too off and hi everyone :)