How should I handle errors inside or outside a Perl module? -


i started learning how make module in perl perltoot:

 package person;  use strict;   my($name, $age, $peers) = ( 0 .. 2 );   sub new {     $self = [];     $self->[$name] = undef;     $self->[$age] = undef;     $self->[$peers] = [];     bless($self);     return $self;  }   sub name {     $self = shift;     if (@_) { $self->[$name] = shift }     return $self->[$name];  }   sub age {     $self = shift;     if (@_) { $self->[$age] = shift }     return $self->[$age];  }   sub peers {     $self = shift;     if (@_) { @{ $self->[$peers] } = @_ }     return @{ $self->[$peers] };  }   1; 
  • i know how, if possible sample code should threat errors within module , outside ?

for example:

 use person;  $test= person->new() or die person->error; 

or

sub new {    $self = [];    $self->[$name] = undef;    $self->[$age] = undef;    $self->[$peers] = [];    bless($self);    #########    # error happened here , need    #########    return $self; } 
  • if call else within module had error, problem, missing argument how correct way tell there error ?

ps: hope question not off , hi :)

carp routines can used report errors.

use carp qw{ croak };  sub new {     $self        = {};     $self->{$name } = undef;     $self->{$age  } = undef;     $self->{$peers} = [];      # replace following appropriate     if ($error_occurred) {         croak 'something did not work right';     }      return bless, $self; } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -