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

Dear all, I have a pattern match thats bugging me and I can't seem to figure out why it's failing. I fear the answer is embarrassing, so please go ahead and embarrass me :)
Here's part of my code:
143: if(my @d = $h->fetchrow_array()) { 144: print STDERR "equip_id: $e_id -> ". $d[0] ." (rep_id = ". $d[1] + .")\n"; 145: if($d[0] eq /NA/) {
It offers this output (two examples):
equip_id: 5212 -> -NA- (rep_id = 154) Use of uninitialized value in pattern match (m//) at test.pl line 145. equip_id: 1337 -> 4047.3 (rep_id = 152) Use of uninitialized value in pattern match (m//) at test.pl line 145.
What is not initialized, or why ?

Replies are listed 'Best First'.
Re: Why are this pattern match failing ?
by citromatik (Curate) on Apr 03, 2009 at 10:33 UTC

    Did you mean...?

    if ($d[0] =~ /NA/){

    or

    if ($d[0] eq "NA"){

    Since pattern matching defaults to the $_ variable, what you wrote is interpreted as if($d[0] eq ($_=~/NA/). Probably $_ is undefined, and that is causing the Use of uninitialized value message.

    citromatik

Re: Why are this pattern match failing ?
by jettero (Monsignor) on Apr 03, 2009 at 10:33 UTC
    embarassing: $string eq m/pattern/

    not embarassing: $string =~ m/pattern/

    Oh, wait, that doesn't answer the question. :) I don't know why $d[0] is uninitialized... perhaps your database returned an undef or there were no rows to fetch, or you never did an execute. Who can tell. From the code snipped, eq vs =~ is the only real problem I can see.

    UPDATE: JavaFan, OIC. Yeah, I missed that.

    -Paul

      $d[0] isn't uninitialized, but $_ is. And that's where the pattern matches against.
Re: Why are this pattern match failing ?
by ww (Archbishop) on Apr 03, 2009 at 10:37 UTC

    Blush #1: Insufficient information (for /me anyway).

    Blush #2: But did you (maybe) mean this?

    if($d[0] =~ /NA/) {

    ...or this?

    if($d[0] eq 'NA') {
Re: Why are this pattern match failing ?
by jantelar (Initiate) on Apr 03, 2009 at 10:51 UTC
    *blush*
    I've been staring at that line for so long, and was unable to see the obvious. Of course I meant =~
    Thanks !
Re: Why are this pattern match failing ?
by codeacrobat (Chaplain) on Apr 03, 2009 at 14:03 UTC
    could have been q/NA/ ;-)

    print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});