Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: An error message to understand

by tirwhan (Abbot)
on Jan 06, 2006 at 12:09 UTC ( [id://521457]=note: print w/replies, xml ) Need Help??


in reply to An error message to understand

Does it mean that one of the @CHOICE array contents are empty?

Yes. You can avoid that warning by checking for definedness:

foreach (@CHOICE) { if (defined && m/2001\sCensus\sOutput\sArea/){
or
foreach (@CHOICE) { next if (!defined); if (m/2001\sCensus\sOutput\sArea/){

This uses $_ implicitly in both the definedness test and the pattern match. Often it's clearer to assign the value to a lexical variable:

for my $chosen (@CHOICE) { if (defined $chosen && $chosen =~ m/2001\sCensus\sOutput\sArea/){

As a further comment, usage of upper-case variable names is generally reserved for constants or perl private variables.


There are ten types of people: those that understand binary and those that don't.

Replies are listed 'Best First'.
Re^2: An error message to understand
by Perl Mouse (Chaplain) on Jan 06, 2006 at 12:14 UTC
    And since the pattern can never match the empty string, or 0, you could skip the define and write:
    foreach (@CHOICE) { if ($_ && /.../) { ... } }
    Or:
    foreach (@CHOICE) { $_ or next; if (/.../) { ... } }
    if all you have inside the foreach is the if statement.
    Perl --((8:>*

      I don't particularly want to argue about it but I would not approve of such a change. True, as the pattern match is now it does not make a difference, but if the pattern is ever changed it's easy to miss changing the test as well, which can introduce a subtle bug. Just checking for truth makes the code more brittle. IMO, YMMV, etc.


      There are ten types of people: those that understand binary and those that don't.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://521457]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found