# Save this file as 'MyModule.pm' package MyModule; # declare your module to be in a package, use strict; use warnings; # warnings can help catch logic errors, and typos use Carp; # issue warnings from calling code. my %HANDLE_MODES = ( # replace globals with package scoped lexical that is NOT accessed outside the file. READMODE => '<', WRITEMODE => '>', APPENDMODE => '>>', ); sub openFile { my $file = shift; # shift automatically works with @_ my $mode = shift; # use strings to match keys in %HANDLE_MODES instead of global variables. # check to see if valid mode in call. if( exists $HANDLE_MODES{$mode} ) { # open a lexical filehandle, capturing any errors my $fh; my $error = open( $fh, $mode, $file) ? undef : $!; # return filehandle and error string. return ( $fh, $error ); } else { # if mode is bad issue error message pointing to calling code. croak "Illegal file handle mode '$mode'" } } 1;