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";
| [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
Re: or DIE adding line number to error
by Zaxo (Archbishop) on Mar 10, 2005 at 03:11 UTC
|
$ perl -e'die "Willingly"'
Willingly at -e line 1
$ perl -e'die "Willingly\n"'
Willingly
$
| [reply] [d/l] |
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);
}
| [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] |
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.
| [reply] [d/l] |
Re: or DIE adding line number to error
by baztastic (Scribe) on Mar 10, 2005 at 03:14 UTC
|
unless(open(LOG, "< $file")){
print "Sorry could not open $file - $!","\n";
exit;
}
| [reply] [d/l] |
|
|
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
| [reply] [d/l] [select] |
|
|
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.
| [reply] |