in reply to Sharing Getopts::Std amongst several related scripts
package main; use Getopt::Std; our %opts; getopts('fs', \%opts); package Foo; if ($main::opts{s}) { #do stuff } package Bar; if ($main::opts{s}) { #do stuff }
You can even create an alias:
package main; use Getopt::Std; our %opts; getopts('fs', \%opts); package Foo; our %opts; *opts = \%main::opts; if ($opts{s}) { #do stuff } package Bar; our %opts; *opts = \%main::opts; if ($opts{s}) { #do stuff }
I hate referencing main explicitely, so I prefer importing the symbol from a module that will handle the options:
# Optionally in seperate file Opts.pm package Opts; use Getopt::Std; our %opts; our @EXPORT = qw( %opts ); our @ISA = 'Exporter'; require Exporter; getopts('fs', \%opts); 1; package Foo; use Opts qw( %opts ); if ($opts{s}) { #do stuff } package Bar; use Opts qw( %opts ); # Safe to do multiple times. if ($opts{s}) { #do stuff }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sharing Getopts::Std amongst several related scripts
by McDarren (Abbot) on Dec 13, 2005 at 16:15 UTC | |
by ikegami (Patriarch) on Dec 13, 2005 at 17:31 UTC |