Solved

Perl || and or and exception handling

Posted on 2011-09-22
8
284 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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 …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now