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

Does anyone have any clue as to why these two pieces of code does not behave the same?
my $head = $mail->head(); if (!$head->get('From') || !$head->get('To') || $head->get('From') eq "<>" || $head->get('To') eq "<>" ) {goto N +EXT;}
and
my $head = $mail->head(); if (!$head->get('From') || !$head->get('To') || $head->get('From') =~ /^<>$/ || $head->get('To') =~ /^<>$/) {got +o NEXT;}


T I M T O W T D I

Replies are listed 'Best First'.
Re: Weird eq behaviour
by choocroot (Friar) on Jul 25, 2002 at 12:49 UTC
    Because of a trailing \n char ???

    From perlop, it says :

    Binary "eq" returns true if the left argument is
    stringwise equal to the right argument.


    So the string should be exactly the same, byte by byte.

    Try this :
    perl -e 'print "Ok\n" if( "foo\n" eq "foo" )'
    this one print nothing ...
    perl -e 'print "Ok\n" if( "foo\n" =~ /^foo$/ )'
    but this one print Ok !
      Offcourse :(
      I didnt think of this because I tried to print the line after a s/./ord($&)." "/ge, but that doesnt change the newline either... ARG ;)

      T I M T O W T D I
        use the s modifier on the regexp to catch newline chars :
        s Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
        s/./ord($&)." "/ges
Re: Weird eq behaviour
by jmcnamara (Monsignor) on Jul 25, 2002 at 12:48 UTC

    There shouldn't be any difference in this case. The operators eq and =~ have different precendence but they are both higher than ||
    my $from = '<>'; my $to = '<>'; if (!$from || !$to || $from eq "<>" || $to eq "<>" ) {print "Ok 1\n +"} if (!$from || !$to || $from =~ /^<>$/ || $to =~ /^<>$/) {print "Ok 2\n +"} __END__ Prints: Ok 1 Ok 2

    --
    John.

      Well I would agree with you except for the simple fact that I had an email, for which I had to add the regexp aswell to my script because it die in the next lines trying to parse "<>"...

      T I M T O W T D I