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

open(LOG, "< $file") or die "Sorry could not open $file";
If this fails it says "Sorry could not open $file on line 28" and I don't want that number there.

How can I make it into some error that prints just my error message if it can't open it? I was thinking doing an if/else and exit but I can't see how I could get it to work with an open().

Any ideas?

Replies are listed 'Best First'.
Re: or DIE adding line number to error
by sgifford (Prior) on Mar 10, 2005 at 03:09 UTC
    Just put a newline at the end of the string in die:
    open(LOG, "< $file") or die "Sorry could not open $file\n";
Re: or DIE adding line number to error
by Thilosophy (Curate) on Mar 10, 2005 at 03:11 UTC
    die adds a line number only if the message string does not end in "\n". So if you want to suppress the line number, you can do
    open(LOG, "< $file") or die "Sorry could not open $file\n";

    I was thinking doing an if/else and exit

    I'd say stick with die. exit cannot be trapped later on (in case you decide the error can be handled somehow), and if you do not trap the die it will exit anyway.

Re: or DIE adding line number to error
by Zaxo (Archbishop) on Mar 10, 2005 at 03:11 UTC

    Add a "\n" to the end of your message. That will suppress file and line-number diagnostics.

    $ perl -e'die "Willingly"' Willingly at -e line 1 $ perl -e'die "Willingly\n"' Willingly $

    After Compline,
    Zaxo

Re: or DIE adding line number to error
by Tanktalus (Canon) on Mar 10, 2005 at 02:58 UTC
    open(LOG, "< $file") or do { print STDERR "Sorry, could not open $file: $!\n"; exit (1); }
    or
    unless (open(LOG, "< $file")) { print STDERR "Sorry, could not open $file: $!\n"; exit (1); }

      even easier:

      open(LOG, "< $file") or die "Sorry could not open $file\n";

      From die:

      If the value of EXPR does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied. Note that the "input line number" (also known as "chunk") is subject to whatever notion of "line" happens to be currently in effect, and is also available as the special variable `$.'. See the section on "$/" in the perlvar manpage and the section on "$." in the perlvar manpage.
Re: or DIE adding line number to error
by merlyn (Sage) on Mar 10, 2005 at 15:42 UTC
    In the future, when you're curious about what a function is doing, you can go right to the source. In this case, perldoc -tf die says:
    If the last element of LIST does not end in a newline, the current script line number and input line number (if any) +are also printed, and a newline is supplied. Note that the "in +put line number" (also known as "chunk") is subject to whateve +r notion of "line" happens to be currently in effect, and is + also available as the special variable $.. See "$/" in perlvar +and "$." in perlvar.
    I think that says pretty clearly that the behavior is expected, and how to alter the behavior.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: or DIE adding line number to error
by baztastic (Scribe) on Mar 10, 2005 at 03:14 UTC

    You could use unless and exit. And if you want to see why it fails you can add $!

    unless(open(LOG, "< $file")){ print "Sorry could not open $file - $!","\n"; exit; }

    -baztastic
      Why print "Sorry could not open $file - $!","\n"; and not print "Sorry could not open $file - $!\n";? I know that they're equivalent, but you don't have to put the newline in its own string...

      thor

      Feel the white light, the light within
      Be your own disciple, fan the sparks of will
      For all of us waiting, your kingdom will come

        To be honest I don't know. I was incredibly distracted when I wrote the response. When I originally started to reply no one else had replied yet, I probably should have looked it over better before submitting it.
        -baztastic