Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Global variables are generally to be avoided. If you must use them and have strict vars enabled, you must specify their package as well as their name: $main::foo.

You should take a look at perlmod, perlmodstyle, and perlmodlib. They have some good information about how to write and use modules.

A few things in your code sample jump out at me. I took the liberty of rewriting your code below. I haven't tested it, so there may be errors.

# 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 t +hat 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 in +stead 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;

Calling code:

use strict; use warnings; use MyModule; # load your module. # call openFile. my ($fh, $error) = MyModule::openFile('my/file/here.txt', 'READMODE' ) +; # Check results if ( $fh ) { # file opended ok, process file. # reading from a lexical handle works just like a global handle. while ( defined ( $foo = <$fh> ) ) { # do stuff } } else { # File did not open, show error. warn "Error opening file- $error\n"; }

I hope this helps.

Update: I forgot to make sure that the module example returned a true value. Fixed it. I told you I didn't test the code :)


TGI says moo


In reply to Re: Using Global Variables in Perl by TGI
in thread Using Global Variables in Perl by gzayzay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found