Solved

Perl || and or and exception handling

Posted on 2011-09-22
8
295 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 500 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 84

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 73
Port 80 requests 16 100
Perl Awk Need Help 3 129
Vb script to unzip a files and rename the files 12 113
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

830 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