Solved

Perl || and or and exception handling

Posted on 2011-09-22
8
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
Six Sigma Control Plans

628 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