test.pm: ... sub status { my $self = shift; if ($self ne 'hm') { $! = '2'; return undef; } } ... test.pl $obj->status() || die "Error: $!";