afarber has asked for the wisdom of the Perl Monks concerning the following question:

Does anybody please have an advice on how to get rid of the warnings Use of uninitialized value in pattern match (m//) without (ab)using the no warnings qw(uninitialized);?

I have a habit of untainting input data this way:

$user = $1 if $query->param('user') =~ /(\w{3,12})/; $pass = $1 if $query->param('pass') =~ /(\w{8})/; ..... unless ($user and $pass and ...) { print $query->start_form(), ... } else { # do the real work with data }

(for example see my full script for adding new users to AD and NIS) and thus I can't set $query->param('blah'); to an empty string '' before I try to match it.

Thank you for your wisdom

Replies are listed 'Best First'.
Re: CGI.pm and Use of uninitialized value in pattern match
by Corion (Patriarch) on Jan 21, 2009 at 14:29 UTC

    You can test it like this, mapping undef to an empty string just for the comparison:

    unless ($user || '' and $pass || '') { ... }

    Personally, I wouldn't force people into using a password that matches ^\w{8}$ (which you seem to do) - maybe you want a check like 8 <= length $pass and some more checks? Your current way would set my password to "threepig" from "one!threepiggies".

Re: CGI.pm and Use of uninitialized value in pattern match
by jeffa (Bishop) on Jan 21, 2009 at 14:26 UTC

    Try this:

    $user = $1 if $query->param('user') && $query->param('user') =~ /(\w{3 +,12})/;
    Or, if the value zero is valid:
    $user = $1 if defined( $query->param('user') ) && $query->param('user' +) =~ /(\w{3,12})/;
    etc. The idea is to first check for defined-ness and then act on that variable if it is.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: CGI.pm and Use of uninitialized value in pattern match
by andye (Curate) on Jan 21, 2009 at 19:01 UTC
    Why not turn off the warnings you don't want?

    I mean, isn't that why no warnings exists?

    IMHO it needn't be taboo.

    All the best, andye

    update: after thinking about this a little more, I've decided that what I really mean is: Surely there should be no taboos in programming?

    What I'm getting at is that, sure, using strict and warnings is, in general, a good idea - but that shouldn't make them a religious imperative. They're useful for a specific reason, not because it's a Sin to do without them.

    If the code is clearer without them - and in my view it is in this case - then switching them off is the Right Thing, no?

      Thank you for replies
Re: CGI.pm and Use of uninitialized value in pattern match
by Your Mother (Archbishop) on Jan 21, 2009 at 17:38 UTC

    I use no warnings "uninitialized" frequently in CGIs and have never had any trouble from it. I much prefer it to the inline logic contortions or repetitive if ( param("asdf") and... statements you end with. Any important subs or whatever that need to have real vars should be doing their own checking anyway.