How to catch a carp-warning?

Posted by sid_com on Stack Overflow See other posts from Stack Overflow or by sid_com
Published on 2010-05-12T10:08:29Z Indexed on 2010/05/12 10:14 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

I tried to catch a carp-warning ( carp "$start is > $end" if (warnings::enabled()); ) with eval but it didn't work, so I looked in the eval-documentation and I discovered, that eval catches only syntax-errors, run-time-errors or executed die-statements. How could I catch a carp-warning?

#!/usr/bin/env perl
use warnings;
use strict;
use 5.012;
use List::Util qw(max min);
use Number::Range;

my @array;
my $max = 20;
print "Input (max $max): ";
my $in = <>;

$in =~ s/\s+//g;
$in =~ s/(?<=\d)-/../g;

eval {
    my $range = new Number::Range( $in );
    @array = sort { $a <=> $b } $range->range;
};
if ( $@ =~ /\d+ is > \d+/ ) { die $@ }; # catch the carp-warning doesn't work 

die "Input greater than $max not allowed $!" if defined $max and max( @array ) > $max;
die "Input '0' or less not allowed $!" if min( @array ) < 1;
say "@array";

© Stack Overflow or respective owner

Related posts about perl

Related posts about carp