Howdy,
Here is my scenario:
- I have about a dozen or so scripts that perform related (but each slightly different) tasks.
- And I have a single library of common functions, which each of the above scripts requires
- Currently, none of the scripts accept command-line options, but I now need to add this functionality.
- As each of the scripts perform (almost) identical functions, and will hence accept the same command-line switches, I thought it would make more sense to add the code for reading these into the library, rather than each individual script.
So far, so good (I think)...
So I started playing around with Getopts:Std. I added code to my library like so:
use Getopt::Std;
getopts('fs', \%opts);
I found that if I then referred to %opts from within the library, like so:
if ($opts{s}) { #do stuff }
...it all works as expected. But if I try to refer to %opts from
outside the library, I get:
Global symbol "%opts" requires explicit package name.....
The reason for the error is pretty obvious - the script doesn't know about %opts, and as I'm using strict, it barfs. But I'm not sure where to go from here. I'm guessing that I need to create a package, and refer to that - but I'm afraid that this is uncharted territory for me. I've tried putting this:
{
package FOO;
use Getopt::Std;
getopts('fs', \%opts);
}
in the library, but that doesn't work.
Can somebody point me in the right direction please?
Thanks,
Darren :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.