Solved

Perl || and or and exception handling

Posted on 2011-09-22
8
265 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

10 Experts available now in Live!

Get 1:1 Help Now