in reply to Re: warning: use of uninitialized value
in thread warning: use of uninitialized value

Hello robby_dobby,

You make many good points. I would add:

But I find this comment a bit strange:

I see this convention of if(open($filehandle, "filename") || die $!) - this is really odd style. This means that whatever happens, straight or exceptional flow, go ahead an process this file.

Of course, the if is entirely redundant here. But surely, if the code throws an exception (i.e., die is called), the file will never be processed?

17:11 >perl -Mstrict -wE "say 'Ok 1'; if (open(my $fh, 'nonexistent.tx +t') || die $!) { say 'Ok 2'; } say 'Ok 3';" Ok 1 No such file or directory at -e line 1. 17:11 >

Or have I misunderstood what you’re saying?

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^3: warning: use of uninitialized value
by Discipulus (Canon) on Jun 25, 2015 at 09:40 UTC
    for the OP might be worth only add few words about || and or: the high precedence || is valid and right as we put parens, without parens has another effect:
    use strict; use warnings; open my $fh, '<', 'notexistant.txt' or die qq(File open failed); #OK +: File open failed at open (my $fh, '<', 'notexistant.txt') || die qq(File open failed); #OK + too: File open failed at.. open my $fh, '<', 'notexistant.txt' || die qq(File open failed); #WR +ONG print <$fh>; # r +eadline() on closed filehandle $fh at..
    Just think of "and" and "or" as being the same as && and ||, but so low on the precedence chart that parenthesis are usually unnecessary to keep the things on the left or on the right properly grouped. It is almost always considered better to use "or" instead of "||" when you work with open.
    is a quote from a a very useful post about idioms and precedences: Perl Idioms Explained - && and || "Short Circuit" operators

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^3: warning: use of uninitialized value
by robby_dobby (Hermit) on Jun 25, 2015 at 08:02 UTC
    Hello Athanasius,

    No, you're completely right. I got distracted as I was typing this - this should remind me not to post when doing something. :-)

    Still, it stood out to me as weird that he relied on open's return value to check for success.