Perl Exception - $!,$?,$@


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Exception - $!,$?,$@
# 1  
Old 08-26-2008
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  
Old 08-26-2008
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  
Old 08-27-2008
Java

Quote:
Originally Posted by otheus
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  
Old 08-27-2008
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  
Old 08-27-2008
"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.
# 6  
Old 08-27-2008
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.
# 7  
Old 08-27-2008
, precedence is higher than or

Code:
$ perl -e '$x->{y} or die 1,2,3;'
123 at -e line 1.

From perlop
Code:
           right       = += -= *= etc.
           left        , =>
           nonassoc    list operators (rightward)
           right       not
           left        and
           left        or xor


Last edited by otheus; 08-27-2008 at 05:17 AM.. Reason: changed "quote" to "code" markup
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Monitor logs for exception and if exception come then sent an email

Hi Folks, please advise , I have logs generated on unix machine at location /ops/opt/aaa/bvg.log , now sometimes there come exception in these logs also, so I want to write such a script such that it should continuously monitor these logs and whenever any exception comes that is it try to find... (3 Replies)
Discussion started by: tuntun27272727
3 Replies

2. Shell Programming and Scripting

Re: exception using AWK

I have following file: NAME=ora.DG1.svc TYPE=ora.service.type CARDINALITY_ID=1 TARGET=ONLINE STATE=ONLINE NAME=ora.orlene.DG2.svc TYPE=ora.service.type CARDINALITY_ID=1 TARGET=ONLINE STATE=OFFLINE NAME=ora.MN.acfs TYPE=ora.registry.acfs.type TARGET=ONLINE (4 Replies)
Discussion started by: rcc50886
4 Replies

3. Solaris

solaris 7 exception

Hi all, An application works well under 2.6 but under 7 it gives TEXT_IO exceptions. (Is_Open, Check_Is_Open, Get_Line procedures). Any idea? Regards (3 Replies)
Discussion started by: endoavour
3 Replies

4. Programming

Exception Handling C++

Hello All, I have a question ....which I am totally confused about! If I have a fxn foo in a program which returns a logical value. But it has a posssiblity to throw some exception. Now my exception handler returns a value as a string stating why the exception occured. But my... (1 Reply)
Discussion started by: mind@work
1 Replies

5. Shell Programming and Scripting

Exception handling

Sometimes when I try to use curl to upload to an ftp server, I get the message: $curl -T file.wmv ftp.eu.filesonic.com --user user:password curl: (8) Got a 421 ftp-server response when 220 was expected How do I get the script to try again if I get the message curl: (8)? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Linux

MMU exception

I hope to post in the right forum, otherwise I apologize for this. if a MMU exception is caused by a process which tries to access to other memory segment (out of its own address space) what the kernel does in this case ? maybe kernel kills the "bad" process ? (2 Replies)
Discussion started by: Puntino
2 Replies

7. UNIX for Dummies Questions & Answers

How to catch the exception

Dear friends, I am transferring some files to a windows system from Unix m/c thru FTP Script given below. echo "open $host quote USER $userid quote PASS $pwd $verbose $type cd $dir bin put $file close quit"|$ftp... (0 Replies)
Discussion started by: Vijayakumarpc
0 Replies

8. HP-UX

Help with RPC Exception

Hi, I am trying to use swremove to remove some old software packages before an upgrade. I keep getting the same error below. I have restarted swagent, i have killed the swagent process and started it, and I have restarted the entire system. I have now run out of ideas. Anyone know what the problem... (2 Replies)
Discussion started by: ejbrever
2 Replies

9. UNIX for Advanced & Expert Users

RPC Exception - Help

Hi, I am trying to use swremove to remove some old software packages before an upgrade. I keep getting the same error below. I have restarted swagent, i have killed the swagent process and started it, and I have restarted the entire system. I have now run out of ideas. Anyone know what the problem... (0 Replies)
Discussion started by: ejbrever
0 Replies

10. UNIX for Advanced & Expert Users

exception handling

Does exception handling exist in any UNIX enviornment? I develop on Windows MSVC++ land and need to port to UNIX. (1 Reply)
Discussion started by: RichardS
1 Replies
Login or Register to Ask a Question