Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl || and or and exception handling

Posted on 2011-09-22
8
Medium Priority
?
326 Views
Last Modified: 2012-05-12
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



   
   
0
Comment
Question by:mybluegrass
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36582485
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’.
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36582586
Did you try:

     my $f = new IO::File("$attachmentFileName", "w")  or die "Can not create file: $!\n";

i.e. adding parenthesis.

0
 
LVL 1

Author Comment

by:mybluegrass
ID: 36583189
Thanks!  But how can I catch the exception in eval or Error.pm's exception handling? thanks!

Ming
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:rajeeshb
ID: 36583335
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;
    }
0
 
LVL 1

Author Comment

by:mybluegrass
ID: 36583357
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
"?)
0
 
LVL 10

Accepted Solution

by:
jeromee earned 2000 total points
ID: 36583376
I would suggest this instead:

my $f = new IO::File("$attachmentFileName", "w");
if( $f ) {
    print $f $content;
    return $attachmentFileNameOnly;
 } else {
    print STDERR "Can not create file $attachmentFileName: $!\n";
 }

Open in new window

0
 
LVL 85

Expert Comment

by:ozo
ID: 36592045
Are you trying to do something like:
 eval {
        $f = new IO::File "$attachmentFileName", "w" or die $!;
    };
    if ($@) {
        print $@;
    }
     
0
 
LVL 10

Expert Comment

by:jeromee
ID: 36592897
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question