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

I am trying to use the warn statement to check if a file has been opened successfully. If it wasn't, I wish to issue a warning AND then set the value of another variable. I can issue the warning with no problem, but I need to be able to set a variable only if the open statement wasn't successfull (i.e. the warning message was displayed). After using "| warn", how do I run another command or commands (such at setting a variable value as in the the following code snippet:
open (FILE,">temp") || warn "error message";$x=1;

Replies are listed 'Best First'.
Re: Question about warn statement
by dreadpiratepeter (Priest) on Aug 22, 2003 at 15:52 UTC
    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."
      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.";
      
      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.
Re: Question about warn statement
by mattriff (Chaplain) on Aug 22, 2003 at 15:54 UTC
    open(FILE,">temp") or warn "error message" and $x = 1;

    Or, if you don't feel a need to use one line:

    open(FILE,">temp") or $x = 1; $x and warn "error message";

    Or, do what dreadpiratepeter said to, if you want to be absolutely clear. :)

    - Matt Riffle
      Sr. Systems Programmer, pair Networks, Inc.
      (although, I speak only for myself; code is untested unless otherwise stated)

Re: Question about warn statement
by roju (Friar) on Aug 22, 2003 at 16:52 UTC
    In the spirit of TMTOWTDI, and because of my love for the comma operator,

    open (FILE,">temp") or $x=1, warn "error message"

Re: Question about warn statement
by CombatSquirrel (Hermit) on Aug 22, 2003 at 16:03 UTC
    Or do a do (and be careful about the scope):
    open (FILE, '>', "temp") or do { warn "error message"; $x=1 };
Re: Question about warn statement
by vek (Prior) on Aug 22, 2003 at 16:31 UTC

    I'd probably do something like this:

    my $x; open (FILE, ">temp") || do { warn "error message"; $x = 1; };
    -- vek --

      ...but then $x is still undefined outside the do{} block (which may or may not be a problem).

      my $x; open (FILE, ">temp") || do { warn "error message"; $x = 1; } print "\$x is $x";

      dave

        You're right but (as you mentioned) the fact that $x is undefined may not be a problem at all.

        my $x; open (FILE, ">temp") || do { warn "error message"; $x = 1; }; if ($x) { # so something }
        -- vek --

        Ooops sorry, my bad. Please ignore the above.

        dave

Re: Question about warn statement
by tcf22 (Priest) on Aug 22, 2003 at 16:05 UTC
    Try something like this:
    open(FILE,">temp") || (warn "error message" and ($x = 1));
    Update: Changed code to avoid wierd issue
      This is dangerous:
      open(FILE,"<temp") || (($x = 1) && warn "error message1: $x"); open(FILE,"<temp") || (($x = 0) && warn "error message2: $x"); __END__ error message1: 1 at test-a.pl line 1.