package Foo; use strict; sub new { my $class = shift; bless { _errno => 0, _errmsg => '', _foo => undef }, $class; } sub set_foo { my ( $self, $foo ) = @_; if ( $foo <= 0 ) { @{$self->{qw/_errno _errmsg/}} = ( 1, 'foo must be positive' ); return; } else { $self->{_foo} = $foo; return 1; } } sub error { $_[0]->{_errno} } sub errmsg { $_[0]->{_errmsg} } #### use Foo; my $thing = Foo->new; my $success = $thing->set_foo( -2 ); if ( $success ) { # do something } else { # you can also do if ( $thing->error ) { die $thing->errmsg; }