Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open(BLA,"<...")|| more than one option?

Status
Not open for further replies.

myggel

Programmer
Dec 11, 2005
15
DE
hello,

does anyone know how to make this work?
open(TESTFILE, "<"."myfile.txt")|| (print OUTFILE "blabla\n"; print "wrote blabla to OUTFILE");
I'd like to have more than just one Option if it fails to open TESTFILE. is this possible?

thanks for any help.
michael

 
open (FH, 'blah.txt') or die "Can't open blah.txt: $!";

open (FH, 'blah.txt') or do_this($!);

etc
etc
etc
 
@kevin: this works. but isn't it possible to test the file just once?

@rharsch:
i'd like to print something to the shell and to a file(OUTFILE).
 
Try this:

# for if the open() fails
unless(open(BLAH, 'blah.txt')){
[tab]print "couldn't open file\n";
[tab]print "I'm having a bad day...\n";
}

# for if the open() suceeds
if(open(BLAH, 'blah.txt')){
[tab]print BLAH "opened file\n";
[tab]print BLAH "things are looking up...\n";
}


Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
yes it's possible to test the file once and do any number of things you wish depending on failure or success. See Mikes post above, or you could do something like this:

Code:
open (FH, 'blah.txt') or do_error('Unable to open file',$!);


sub do_error {
   my $error = shift;
   my $error2 = shift;
   open(ERROR,'error.txt') or die "$!";
   print ERROR "$error: $error2 [" .time . "]\n";
   close ERROR;
   print "$error: $error2 [" .time . "]\n";
}

which gives you a sub routine that you can feed all your error messages to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top