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 }
In reply to Re: Sharing Getopts::Std amongst several related scripts
by ikegami
in thread Sharing Getopts::Std amongst several related scripts
by McDarren
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |