[Melbourne-pm] $0 manipulation
mathew.blair.robertson at gmail.com
Thu Aug 30 22:15:46 PDT 2012
I had a scenario recently where 'pod2usage' was failing (ref: Pod::Usage)
when used in conjunction with a GetOptions call -> the code went something
my $man = 0;
my $help = 0;
my $cmd = "";
'help|h|?' => \$help,
man => \$man,
"cmd=s" => \$cmd,
) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
pod2usage(2) unless $cmd;
Now call "blah.pl --help" and I get an error on the ...if $help line
So drilling into it further, I identify that the problem is related to how
I am manipulating $0 to also show automatically my command line arguments,
and how Pod::Usage attempts to read from $0. Thus depending on the order
in which you call pod2usage vs manipulate $0, you may or may not get an
Some further reading of the perlvar doc says that manipulating $0 to status
information, is a normal thing.... Looking at FindBin, etc al also shows
that $0 is consulted (at call-time) for its current value.
I have posted previously some discussion on the use of $0 (eg: pidof
somescript.pl vs killall somescript.pl) -> where that discussion depended
on your point of view.
However, this one seems somewhat contradictory -> to my eyes, either $0
should contain a somewhat non-updatable pointer to the location of the
script, *or* you can update its value to show the current script-status,
but then cause your tools to break. Obviously there are workarounds, such
as monkey patching FindBin, pos2usage, etc, but that gets somewhat
cumbersome. And I dont recall there being a separate variable (other than
$0), which holds the path to the executing script.
So how do I update $0 to show script status, but also to have pod2usage,
FindBin, etc do the right thing?
I dont expect an easy answer to this... other than:
a) wont fix
b) use some variation of another variable ** to fix either the CPAN
modules, or script status.
** I vote $^0... I dont think it is being used, and it follows both the $0
= name and $^ = internal-var convention.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Melbourne-pm