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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the DIE command

Status
Not open for further replies.

m4trix

Vendor
Jul 31, 2002
84
CA
how is die used? I've seen it many times, infact someone posted:

open(MCF,"member_colour_file.txt") || die "error message";

to help someone on this forum... Does this mean that if the file can't be opened an error message is given? When I try that, I don't see the error message. Where does it appear? couldn't I say
open(MCF,"member_colour_file.txt") || print "error message";
and that way I'd see it?

Secondly, according to some of the perl stuff I've read in the O'Reilly book and on the net,
open(MCF,"member_colour_file.txt") || die "error message";
is wrong, it should be
open(MCF,"member_colour_file.txt") or die "error message";


is this true?
 
At a command prompt, do

perldoc -f die

to read what the "die" does.

You could say

open(MCF,"member_colour_file.txt") || print "error message";

but after printing "error message", your program would continue on, which is not what you want.

I'd have to read up on || and "or" to see if that's true, but I regularly use || - maybe I'm doing it wrong :-(

Which O'Reilly book are you referring to? They put out a log of good Perl books. Hardy Merrill
 
umm, I don't have it with me. it's the one with the Camel on it. I think it's just called Perl. and I think it's the 3rd edition
 
first of die or || is correct. || means 'logocial or'

so you are you with the following:
open(MCF,"member_colour_file.txt") || die "error message";
open MCF (which will now be a reference to) member_colouur_file.txt) OR if you can't open, access, read, the file then die and end the entire program NOW.

yes you could say:
open(MCF,"member_colour_file.txt") || print "error message";
and rather than die and end the program it will now print to the screen "error message"

what you may want to start doing as a good practice is to start adding the $! to the end of your error message which will give you the exact error reported to the program like such:
open(MCF,"member_colour_file.txt") || print "error message: $!\n";

You can also write your errors out to logfiles for later viewing like so:

open (LOGFILE, ">c:/path/to/directory/where/logfile/will/go/logfile.log") or die "Unable to create logfile: $!";
open(MCF,"member_colour_file.txt") || print LOGFILE "error message: $!\n";

HTH - Mike

 
Die indicates an error. There are a couple of ways to use it: die "Couldn't open Mail Program: $!\n" or die $DBI::errstr. In the latter, errstr is a message provided by the DBI library. The former is a custom message that you can make to provide a more meaningful error message. On a web page, you must have 'use CGI::Carp qw(fatalsToBrowser);' in your file - this sends the error message to the browser rahter than to an error log or STDOUT.

Your second question: 'or' and '||' are the same thing - just as 'and' and '&&' are the same. I'd bet that O'Reilly covers it somewhere. There's always a better way...
 
The difference between '||' and 'or' is their precedence. You can do
Code:
# using the open function
open(MCF,"member_colour_file.txt") || die "error message";
# or
# using the open list operator
open MCF, "member_colour_file.txt" or die "error message";
# but not
open MCF, "member_colour_file.txt" || die "error message";
The last is equivalent to
Code:
open MCF, ("member_color_file.txt" || die "error message");
Since the string "member_color_file.txt" evaluates to TRUE the die statement never gets executed even if the file doesn't exist or can't be opened. This is because '||' has a higher precedence than list operators whereas 'or's precedence is lower. Read perldoc perlop ( for more about this.

jaa
 
I agree with justice41:
In this context it is better to use 'or' instead of '||'.

The following is from the perlop man page:
[tt]

Logical or and Exclusive Or

Binary "or" returns the logical disjunction of the
two surrounding expressions. It's equivalent to ||
except for the very low precedence. This makes it
useful for control flow

print FH $data or die "Can't write to FH: $!";


This means that it short-circuits: i.e., the right
expression is evaluated only if the left expression
is false. Due to its precedence, you should probably
avoid using this for assignment, only for control flow.

$a = $b or $c; # bug: this is wrong
($a = $b) or $c; # really means this
$a = $b || $c; # better written this way

However, when it's a list-context assignment and you're
trying to use "||" for control flow, you probably
need "or" so that the assignment takes higher precedence.

@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due

Then again, you could always use parentheses.
[/tt]
 
Could the reason that 'm4trix' was not seeing the error message sent by 'die' be due to the STDERR being re-directed away from STDOUT on that machine? OR that the window in which the error message is being displayed is being terminated before you get chance to see it.

I am VERY new to Perl programming, so please excuse my ignorance if these suggestions are rubbish (but if they are, I would really like some feedack).

cheers,
Barrie
 
Hi m4trix,

All the above suggestions would have given you a clear insight by now.
Let me share with you all some stuff abt error handling.
1.Use die only when you want your program to stop processing any further.
2.Use "warn" otherwise,to just throw a warning and continue.
3.You can use the Carp::croak to give messages according to the perspective of the program.
4.The best way to handle errors are to enclose you code segment within an eval block.

eg>
eval
{
open(SOMEFILE,"/usr/home/somefile/");
}
if($@)
{
die "error:$@";
}

Hope this helped you.
 
Got a quick question if I use:


Die "c:\somefile, stopped";
exit;

It does not kill the program, the program keeps running? Why is this? That is just a simple die script that should work? Correct?

The file that I am trying to kill is a .dll, could that be the reason why it is not working???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top