mybluegrass
asked on
Perl || and or and exception handling
This is my code:
my $f = new IO::File "$attachmentFileName", "w" or die "Can not create file: $!\n";
print $f $content;
I need to write content a file named $attachmentFileName. When the filename is too long, perl won't be able to write the file to linux folder. So, it will generate errors like this:
Can not create file: File name too long
But if I change "or" with "||", it will generate:
Can't use an undefined value as a symbol reference for the line "print $f $content;";
What is the difference between "or" and "||"? Why do they give different result?
Also, I want to catch the exception:
eval {
$f = new IO::File "$attachmentFileName", "w";
};
if ($@) {
print $@->getErrorMessage();
} else {
print $f $content;
return $attachmentFileNameOnly;
}
But all it does is to print out the following error:
Can't use an undefined value as a symbol reference at the line "print $f $content;".
I also tried Error.pm module and no luck either.
Your help is highly appreciated!
Thanks!
Ming
my $f = new IO::File "$attachmentFileName", "w" or die "Can not create file: $!\n";
print $f $content;
I need to write content a file named $attachmentFileName. When the filename is too long, perl won't be able to write the file to linux folder. So, it will generate errors like this:
Can not create file: File name too long
But if I change "or" with "||", it will generate:
Can't use an undefined value as a symbol reference for the line "print $f $content;";
What is the difference between "or" and "||"? Why do they give different result?
Also, I want to catch the exception:
eval {
$f = new IO::File "$attachmentFileName", "w";
};
if ($@) {
print $@->getErrorMessage();
} else {
print $f $content;
return $attachmentFileNameOnly;
}
But all it does is to print out the following error:
Can't use an undefined value as a symbol reference at the line "print $f $content;".
I also tried Error.pm module and no luck either.
Your help is highly appreciated!
Thanks!
Ming
The reason is that the operator precedence for the double pipe is higher than the comma, so perl is resolving the double pipe first, which chooses the first true result of its two parameters, in this case "w" and the die statement. The "w" is a nonempty string it counts as true and always gets returned by the double pipe. So you will never see the message in ‘die’.
Did you try:
my $f = new IO::File("$attachmentFileN ame", "w") or die "Can not create file: $!\n";
i.e. adding parenthesis.
my $f = new IO::File("$attachmentFileN
i.e. adding parenthesis.
ASKER
Thanks! But how can I catch the exception in eval or Error.pm's exception handling? thanks!
Ming
Ming
make sure you declare $f & $content before eval {}.
my $f;
eval {
$f = new IO::File "$attachmentFileName", "w";
};
if ($@) {
print $@->getErrorMessage();
} else {
print $f $content;
return $attachmentFileNameOnly;
}
my $f;
eval {
$f = new IO::File "$attachmentFileName", "w";
};
if ($@) {
print $@->getErrorMessage();
} else {
print $f $content;
return $attachmentFileNameOnly;
}
ASKER
I did but here is what I got:
Can't locate object method "getErrorMessage" via package "Can not create file: File name too long
" (perhaps you forgot to load "Can not create file: File name too long
"?)
Can't locate object method "getErrorMessage" via package "Can not create file: File name too long
" (perhaps you forgot to load "Can not create file: File name too long
"?)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you trying to do something like:
eval {
$f = new IO::File "$attachmentFileName", "w" or die $!;
};
if ($@) {
print $@;
}
eval {
$f = new IO::File "$attachmentFileName", "w" or die $!;
};
if ($@) {
print $@;
}
Why do an eval when the constructor provides you with a way to detect whether it succeeded or not, and then use $! for the failure reason?