Is there something better than this?

U

user

Is there another way to handle failure to create a directory handle
other than the following:
opendir($MYHANDLE,$mydir) || die ("Unable to open dir $mydir");

Another way to put this would be, is there a way to tell the reason
for the failure create the handle?
 
M

Michel Rodriguez

Is there another way to handle failure to create a directory handle
other than the following:
opendir($MYHANDLE,$mydir) || die ("Unable to open dir $mydir");

Another way to put this would be, is there a way to tell the reason
for the failure create the handle?

opendir($MYHANDLE,$mydir) || die ("Unable to open dir $mydir: $!");

$! gives you the error as sent by the OS.

From man perlvar



$OS_ERROR
$ERRNO
$! If used numerically, yields the current value of the C "errno"
variable, or in other words, if a system or library call fails,
it sets this variable. This means that the value of $! is
meaningful only immediately after a failure:

if (open(FH, $filename)) {
# Here $! is meaningless.
...
} else {
# ONLY here is $! meaningful.
...
# Already here $! might be meaningless.
}
# Since here we might have either success or failure,
# here $! is meaningless.

In the above meaningless stands for anything: zero, non-zero,
"undef". A successful system or library call does not set the
variable to zero.

If used as a string, yields the corresponding system error
string. You can assign a number to $! to set errno if, for
instance, you want "$!" to return the string for error n, or
you want to set the exit value for the die() operator.
(Mnemonic: What just went bang?)

Also see "Error Indicators".
 
T

Tore Aursand

Is there another way to handle failure to create a directory handle
other than the following:
opendir($MYHANDLE,$mydir) || die ("Unable to open dir $mydir");

Another way to put this would be, is there a way to tell the reason
for the failure create the handle?

The '$!' variable holds the error message;

opendir( $MYHANDLE, $mydir ) or die "$!\n";

If you don't want to die each time something like this fails, you can do
something like this;

if ( opendir($MYHANDLE, $mydir) ) {
# ...
closedir( $MYHANDLE );
}
else {
# Handle error
}

....or you could check out the Error module which lets you do try, catch
and all that stuff.
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

Is there another way to handle failure to create a directory handle
other than the following:
opendir($MYHANDLE,$mydir) || die ("Unable to open dir $mydir");

Another way to put this would be, is there a way to tell the reason
for the failure create the handle?


opendir(MYHANDLE,$mydir) ||
die("opendir failed for $mydir : $!\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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top