Carp



It is better to use croak and carp instead of die and warn.


examples/error/carp.pl
#!/usr/bin/perl
use strict;
use warnings;

use Module;

Module::f("x");  # Parameter needs to be a number at Module.pm line 10.

Module::g("x");  # Parameter needs to be a number at carp.pl line 8

Module::h("x");
# Parameter needs to be a number at Module.pm line 24
#        Module::h('x') called at carp.pl line 9

examples/error/Module.pm
package Module;
use strict;
use warnings;

use Carp qw(carp cluck);

sub f {
    my ($x) = @_;
 
    warn "Parameter needs to be a number" if $x =~ /\D/;
    return $x;
}

sub g {
    my ($x) = @_;
 
    carp "Parameter needs to be a number" if $x =~ /\D/;
    return $x;
}

sub h {
    my ($x) = @_;
 
    cluck "Parameter needs to be a number" if $x =~ /\D/;
    return $x;
}


1;