The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Exception Handling bertpereira Shell Programming and Scripting 5 01-14-2009 09:28 PM
MMU exception Puntino Linux 2 05-07-2008 12:35 PM
How to catch the exception Vijayakumarpc UNIX for Dummies Questions & Answers 0 02-08-2007 02:18 AM
Help with RPC Exception ejbrever HP-UX 2 08-24-2006 02:08 PM
RPC Exception - Help ejbrever UNIX for Advanced & Expert Users 0 08-21-2006 12:56 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2008
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Question Perl Exception - $!,$?,$@

Hi,

I am trying to understand the significance of the special variables $!,$@ and $? in perl. I have a code block as follows:

eval {

Code Segment 1:
#authenticating to the remote server
$ftpobj -> login($username,$password) or die "Can't login to $remote_host";

Code Segment 2:
#setting up the current working directory
$ftpobj -> cwd ($cwd) or die "Can't change directory";

Code Segment 3:
#putting the file in remote server
$ftpobj -> put($filename) or die "Can't ftp the file:",$!;
}

[Where $ftpobj is an Net::FTP object and $filename is an invalid filename]

In Code Segment 1 and 2,if there were any errors,i can able to catch it using if($@) since the above code is enclosed by eval.

But the trapping is not successful in the case of Code Segment 3.Although the control comes inside the conditional loop if($@) after eval,error message
is written to the Standard output with this.The error message is:

"Cannot open Local file test2.log: No such file or directory
at ftp_file.pl line 44"

Why is this happening?Exception handling in perl is implemented using eval{} and if($@), but in this case the error is going out-of control and got printed to STDOUT or STDERR

Can anyone suggest what is going on and what is the reason for this behaviour?

Can anyone explain me what makes $!,$@,$? different in various situations?

Your help and support is appreciated!

With Regards
Dileep
  #2 (permalink)  
Old 08-26-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
man perlvar will help you.

$@ is the error message from the last eval(). Period. It's null if there was no eval.

$! is what went wrong after the last call to the OS -- it's essentially from errno. It gets reset as soon as you do something that succeeds, like the statement "1;".

$? Is only after an external command, pipeline, system(), or wait(), it contains the error the last command returned, shifted left 8 bits.

About the error message, it could be Net::FTP is printing out to stderr. You can instantiate the object with a Debug parameter...

new Net::FTP( $hostname, ( 'Debug'=> 10 ));
  #3 (permalink)  
Old 08-27-2008
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Post

Quote:
Originally Posted by otheus View Post
man perlvar will help you.

$@ is the error message from the last eval(). Period. It's null if there was no eval.

$! is what went wrong after the last call to the OS -- it's essentially from errno. It gets reset as soon as you do something that succeeds, like the statement "1;".

$? Is only after an external command, pipeline, system(), or wait(), it contains the error the last command returned, shifted left 8 bits.

About the error message, it could be Net::FTP is printing out to stderr. You can instantiate the object with a Debug parameter...

new Net::FTP( $hostname, ( 'Debug'=> 10 ));
Thank you for your response.I understand about the variable difference.But am just wondering what is Debug code "10".My understanding is that a non-zero debug code will print each and every internal process to STDOUT.So how this will help to solve my problem.ie,Throwing error to STDOUT or STDERR

Regards
Dileep
  #4 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
My guess is that the misplaced comma in your third segment is causing the error to be lost. You probably mean die "Can't ftp the file: $!" rather than (die "Can't ftp the file"), $! which is what your code currently seems to do, if I managed to get the precedence right.

Where in your code is there a reference to Local file test2.log? Is that the file you are attempting to upload? (What's the value of $filename?)
  #5 (permalink)  
Old 08-27-2008
DILEEP410 DILEEP410 is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 148
Post

Quote:
Originally Posted by era View Post
My guess is that the misplaced comma in your third segment is causing the error to be lost. You probably mean die "Can't ftp the file: $!" rather than (die "Can't ftp the file"), $! which is what your code currently seems to do, if I managed to get the precedence right.

Where in your code is there a reference to Local file test2.log? Is that the file you are attempting to upload? (What's the value of $filename?)
Thanks for your response. I know the filename is invalid.Basically am trying to workout on the exception handling mechanism, where exceptions from all the above statements like,creating ftp object,loggging,change directory etc are trapped in $@ and print only if i give a print statement in the catch block or if($@) block.

But in this case alone ie,putting the file to remote server,error is thrown to the STDOUT/STDERR along with trapping it in $@.I am wondering about this behaviour and looking for the reason and solution.

Unfortunately, the possibility you had given(comma) is not the trum card!
  #6 (permalink)  
Old 08-27-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
"die" takes a list, so that comma is fine. The problem might be in the Net::FTP implementation. Why don't you use the debugger and trace your way through the code?

It's possible to override the die/warn handlers with $SIG{__DIE__} = code; In this way, a module can redirect stderr to stdout, or something like that. It might be the Net::FTP module is doing this.
  #7 (permalink)  
Old 08-27-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Sorry, yes, you are right, I tested what I thought to be an analogous case here but my test was flawed.

It would be intriguing to find out which part of the code corresponds to "line 44" in the error message.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:57 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0