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

Discussion in 'Perl Misc' started by bernd, Jul 27, 2007.

  1. bernd

    bernd Guest

    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
     
    bernd, Jul 27, 2007
    #1
    1. Advertising

  2. bernd <> writes:

    > 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
     
    Peter Makholm, Jul 27, 2007
    #2
    1. Advertising

  3. bernd

    Guest

    bernd <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jul 27, 2007
    #3
  4. bernd wrote:
    > 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";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 27, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen

    Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    440
    Natty Gur
    Jan 26, 2004
  2. Lloyd Sheen

    More Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    455
    Alvin Bruney
    Jan 25, 2004
  3. morrell
    Replies:
    1
    Views:
    968
    roy axenov
    Oct 10, 2006
  4. fuzziy
    Replies:
    1
    Views:
    405
    Gabriel Genellina
    Jun 3, 2009
  5. John Crichton
    Replies:
    6
    Views:
    267
    John Crichton
    Jul 12, 2010
Loading...

Share This Page