If you use strict; use warnings; whenever you try to compare an undefined value it will yelp. This is great to do, but if you want to fix that warning you should let perl know you expect the possibility of an undef value by testing for an undef value (and only performing concatanation, regular expressions, or anything else with it) if it's defined, i.e.:
# note that we have to check if its defined
# because it's possible it could be 0, and false
#62 exit 0 unless ((defined ($page))
and (defined($pages{$page}))
and ($pages{$page} == '1'));
You should note that you are shifting from @_ to set $page == undef; especially on this little bit of test code. So if @_ is undefined then you'll get those warnings. You can turn off warnings for uninitialized values by using:
NOWHINING: {
no warnings qw (uninitialized);
my %pages = ("index" => '1',
"me" => '1',
"stuff" => '1',
"links" => '1');
my $page = shift;
exit 0 unless $pages{$page} == '1';
}
Just be careful with the above code because:
- The no warnings qw(uninitialized); is declared within the NOWHINING: block so as to keep its scope only for the offending code. You could extend this to your entire program by using no warnings qw(uninitialized); at the top of your script, but this becomes dangerous because you want to know when uninitialized values are being used.
- The %pages and the $page variables are only valid within the {}s. This may or may not be an issue. Adjust your scope accordingly.
- Is @_ supposed to contain undef values? If it isn't you've got bigger problems.
- because @_ is "magical" as soon as you use any function that modifies @_ it can on change you. I recommend you never use it unless where the @_ comes from is clear from the code.
- Use something like my @arguments = @_; at the beginning of wherever you get the @_ from so that any sub that is called doesn't modify @_ on you.
- perl -e 'use strict; use warnings; my $foo; print $foo;' and perl -e 'use strict; use warnings; undef =~ m/foo/;' will show you some ways you can get and reproduce that error.
Good luck!
Vautrin
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.