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

This gives a "Use of uninitialized value in pattern match" warning:
perl -e'open FOO, "/dev/null"; use warnings; undef=~/bar/'
as does this:
perl -e'open FOO, "/dev/null"; $x=scalar <FOO>; use warnings; $x=~/bar +/'
but this is strangely silent:
perl -e'open FOO, "/dev/null"; use warnings; <FOO>=~/bar/'
Can anyone offer an explanation?

Replies are listed 'Best First'.
Re: matching against failed readline doesn't warn?
by tilly (Archbishop) on Apr 08, 2004 at 06:33 UTC
    I think that you can rule out the regular expression engine from being a key part of this oddity. The following pair shows the same behaviour:
    perl -we'open FOO, "/dev/null";$x=<FOO>; print scalar $x' perl -we'open FOO, "/dev/null"; print <FOO>'
    And it isn't just print either:
    perl -we'open FOO, "/dev/null"; $x=<FOO>; print $x+2' perl -we'open FOO, "/dev/null"; $x=2+<FOO>; print $x'
    demonstrates the same oddity. However:
    perl -we'open FOO, "/dev/null"; print defined <FOO>'
    notices that the value is undef.

    UPDATE: Fixed the scalar issue that both Abigail-II and ysth pointed out to me. (It didn't make a difference in the warning behaviour.)

      perl -we'open FOO, "/dev/null"; print <FOO>'
      This one has <FOO> in list context, so an empty list is returned. No warnings when printing an empty list isn't to be taken as a surprise.

      Abigail

Re: matching against failed readline doesn't warn?
by sgifford (Prior) on Apr 08, 2004 at 16:43 UTC
    The strangeness seems to be in the diamond operator. If I use a sub instead, I get a warning:
    #!/usr/bin/perl -w open FOO, "< /dev/null" or die "No foo: $!\n"; #scalar(<FOO>) =~ /bar/ (rf() =~ /bar/) and print "bar\n"; sub rf { return scalar(<FOO>); }