in reply to Question about warn statement

unless (open(FILE,">temp")) { warn "error"; $x=1; }


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: Re: Question about warn statement
by sauoq (Abbot) on Aug 22, 2003 at 23:27 UTC
    unless (open(FILE,">temp")) {

    This is fine and dandy. I used to prefer it. But, it should come with a caveat... If you like to autovivify your filehandles, using that construct might cause a problem. Consider:

    unless (open my $fh, '<', 'file') { # . . . }
    In that code, $fh is lexical to the unless block. That probably isn't what you want because you only enter the block if the open fails. This pitfall may be more likely to come up when changing existing code than when writing new code altogether.

    You can get around it by declaring $fh prior to the unless but, now anyway, I prefer to avoid it by using

    open my $fh, '<', 'file' or do { # . . . };
    instead. That also puts the emphasis back on the open statement itself.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re: Question about warn statement
by hmerrill (Friar) on Aug 22, 2003 at 16:11 UTC
    I know 'unless' is the right *perl* way to do that, but I've never quite gotten used to using 'unless'. I prefer to use 'if not' in place of 'unless' - it just makes more sense to me:
    if (not open(FILE, ">temp")) { warn "error"; $x = 1; }
    HTH.