From: Tom Phoenix Date: Wed, 29 Apr 1998 22:48:16 +0000 (-0700) Subject: Using Getopts::* with strict vars X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=535b5725d48083aeb233abbcdfef2fd3274b2619;p=p5sagit%2Fp5-mst-13.2.git Using Getopts::* with strict vars p4raw-id: //depot/perl@964 --- diff --git a/lib/Getopt/Long.pm b/lib/Getopt/Long.pm index 5b5b495..fe7e12f 100644 --- a/lib/Getopt/Long.pm +++ b/lib/Getopt/Long.pm @@ -955,6 +955,12 @@ identifier is $opt_ . The linkage specifier can be a reference to a scalar, a reference to an array, a reference to a hash or a reference to a subroutine. +Note that, if your code is running under the recommended C pragma, it may be helpful to declare these package variables +via C perhaps something like this: + + use vars qw/ $opt_size @opt_sizes $opt_bar /; + If a REF SCALAR is supplied, the new value is stored in the referenced variable. If the option occurs more than once, the previous value is overwritten. diff --git a/lib/Getopt/Std.pm b/lib/Getopt/Std.pm index 2788293..18b5739 100644 --- a/lib/Getopt/Std.pm +++ b/lib/Getopt/Std.pm @@ -27,6 +27,12 @@ switch name) to the value of the argument, or 1 if no argument. Switches which take an argument don't care whether there is a space between the switch and the argument. +Note that, if your code is running under the recommended C pragma, it may be helpful to declare these package variables +via C perhaps something like this: + + use vars qw/ $opt_foo $opt_bar /; + For those of you who don't like additional variables being created, getopt() and getopts() will also accept a hash reference as an optional second argument. Hash keys will be x (where x is the switch name) with key values the value of diff --git a/lib/strict.pm b/lib/strict.pm index 8492e93..af95b3d 100644 --- a/lib/strict.pm +++ b/lib/strict.pm @@ -38,6 +38,7 @@ use symbolic references (see L). =item C This generates a compile-time error if you access a variable that wasn't +declared via C, localized via C or wasn't fully qualified. Because this is to avoid variable suicide problems and subtle dynamic scoping issues, a merely local() variable isn't good enough. See L and @@ -48,6 +49,10 @@ L. my $foo = 10; # ok, my() var local $foo = 9; # blows up + package Cinna; + use vars qw/ $bar /; # Declares $bar in current package + $bar = 'HgS'; # ok, global declared via pragma + The local() generated a compile-time error because you just touched a global name without fully qualifying it.