Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

two statements in or

by Selvakumar (Scribe)
on Jun 26, 2009 at 10:36 UTC ( [id://775008]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
i want to process two statement here in open(FILE,"file") or (first statement; second statement);
how can i add that? if the file cannot able to open i need to print something and process something.

Replies are listed 'Best First'.
Re: two statements in or
by davorg (Chancellor) on Jun 26, 2009 at 10:50 UTC

    Well, you could use do:

    open my $fh, 'file' or do { something; something else; };

    But that's getting a bit obscure, so I'd probably write something a bit more expressive.

    if (!open my $fh, 'file') { something; something else; }

    Or

    unless (open my $fh, 'file') { something; something else; }
    --

    See the Copyright notice on my home node.

    Perl training courses

Re: two statements in or
by moritz (Cardinal) on Jun 26, 2009 at 10:46 UTC
    if (open(my $handle, '<', 'filename.txt')) { # success } else { # failure die "can't open 'filename.txt' for reading: $!"; }
Re: two statements in or
by targetsmart (Curate) on Jun 26, 2009 at 11:25 UTC
    I think you got carried away by the elegance of modifiers
    modifiers are meaningful if you have one statement to execute upon it.
    but in case of more than one statement, better use some other flow control statements with blocks


    Vivek
    -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.
Re: two statements in or
by poolpi (Hermit) on Jun 26, 2009 at 12:59 UTC

    See autodie

    eval { use autodie; open( my $fh, '<', $my_file ); # ... close($fh); }; if ( $@ and $@->isa('autodie::exception') ) { if ( $@->matches('open') ) { print "Error from open\n"; # ... } } else { # open OK... }


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
      open ... or do{ .. }; open ... or ..., ...;
Re: two statements in or
by Marshall (Canon) on Jun 27, 2009 at 10:35 UTC
    I would do this:
    #!/usr/bin/perl -w use strict; open (IN, "somefile") || warn "unable to open somefile"; open (IN, "somefile") || hey_do_something(); sub hey_do_something { print "some stuff in a sub\n"; print "more stuff in a sub\n"; }
    In the above normally "warn" would be "die". In almost all cases an OPEN should succeed on a file_path (usual case is that you know a valid file_path). There are exceptions of course, but they are aren't "normal".

    Anyway above "warn", "die", "hey_do_something" are just subroutines to execute if the OPEN fails.

    Update: If you are working with some user input, I would check that the file exists before even getting to the "open" statement unless the error message from the open() is very clear: open() or die "can't open $file $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://775008]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found