Block of statements after "open" and before "die"

B

bernd

Hello folks,

I wondering whether it is possible to execute more statements than the
"traditional" die after the invocation of open in a perl script:

Instead of:

open (FH, ">testfile") or die "Cannot open testfile" ;

I want to do a little bit more. My idea is deployment of a "do"-block

open (FH, ">testfile") or do { # some other stuff, e.g. sending a mail
message; die "Cannot open testfile" }

my @include ;

This does not seem to work. In my case the statement declaring an
array immediately after the do block is refused by the compiler with
the message:

"Global symbol @include requires a package name..."

Apart from the $SIG(__DIE__)-stuff, which, from my point of view is
not that obvious, do I have any other possibility?

Cheers

Bernd
 
P

Peter Makholm

bernd said:
I want to do a little bit more. My idea is deployment of a "do"-block

open (FH, ">testfile") or do { # some other stuff, e.g. sending a mail
message; die "Cannot open testfile" }

my @include ;

You're missing a ; after the 'do { ... }'. do BLOCK isn't a part of
the syntax like if and fo. It is just like a function taking a code
block as argument like sort and map.

//Makholm
 
X

xhoster

bernd said:
Hello folks,

I wondering whether it is possible to execute more statements than the
"traditional" die after the invocation of open in a perl script:

Instead of:

open (FH, ">testfile") or die "Cannot open testfile" ;

I want to do a little bit more. My idea is deployment of a "do"-block

open (FH, ">testfile") or do { # some other stuff, e.g. sending a mail
message; die "Cannot open testfile" }

my @include ;

You need a ";" after your do block.
This does not seem to work. In my case the statement declaring an
array immediately after the do block is refused by the compiler with
the message:

"Global symbol @include requires a package name..."

Before the global symbol error, I get another error message:
syntax error at -e line 1, near "} my "

Don't you get that error, too? It generally doesn't make sense to worry
about declaration errors when earlier syntax errors are also present.

Xho
 
G

Gunnar Hjalmarsson

bernd said:
I wondering whether it is possible to execute more statements than the
"traditional" die after the invocation of open in a perl script:

Instead of:

open (FH, ">testfile") or die "Cannot open testfile" ;

I want to do a little bit more. My idea is deployment of a "do"-block

As others have explained, a do-block may be fine. Another opportunity is
to write a sub:

open FH, "> testfile" or mydie("Couldn't open file: $!");

sub mydie {
my $msg = shift;
my ($file, $line) = (caller)[1,2];

# send email
# ,,,

die "$msg at $file line $line.\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top