Error handling

S

Simon

Hi guys, bit of a newbie here.

Im trying to learn error handling in perl.

Here are a couple of scenarios.

Im trying to write messages to the terminal/screen and output these over
operating system returned error messaages.

============================================================================

use Win32;

print `ipconig` || die "no good";



----------------------------------------- output is:

C:\Test>alright.pl

'ipconig' is not recognized as an internal or external command,

operable program or batch file.

no good at C:\Test\alright.pl line 4.



Why isnt it the following?

no good

============================================================================

C:\_Test>Connect.pl

hi thereSystem error 1326 has occurred.

Logon failure: unknown user name or bad password.



!Error Connecting!

Please check the workstation name and/or password!

C:\_Test>Connect.pl

hi thereSystem error 1326 has occurred.

Logon failure: unknown user name or bad password.



!Error Connecting!

Please check the workstation name and/or password!





Instead of the operating system reprorting these messages, I want to hide
these and generate my own.



Any help appreciated.
 
J

Jürgen Exner

Simon said:
Im trying to write messages to the terminal/screen and output these
over operating system returned error messaages.

print `ipconig` || die "no good";
----------------------------------------- output is:
'ipconig' is not recognized as an internal or external command,
operable program or batch file.
no good at C:\Test\alright.pl line 4.

Why isnt it the following?
no good

Most likely because the OS/shell/... is writing that error message to STDERR
which is not captured by backticks.
C:\_Test>Connect.pl
hi thereSystem error 1326 has occurred.
Logon failure: unknown user name or bad password.
!Error Connecting! [...]
Instead of the operating system reprorting these messages, I want to
hide these and generate my own.

Then you need to capture STDERR in addition to STDOUT. For details please
see the FAQ "perldoc -q STDERR":
"How can I capture STDERR from an external command?"

jue
 
T

Tim Southerwood

Simon said:
Hi guys, bit of a newbie here.

Im trying to learn error handling in perl.

Here are a couple of scenarios.

Im trying to write messages to the terminal/screen and output these over
operating system returned error messaages.

============================================================================

use Win32;

print `ipconig` || die "no good";



----------------------------------------- output is:

C:\Test>alright.pl

'ipconig' is not recognized as an internal or external command,

You have a typo:

ipconfig

C:\_Test>Connect.pl

hi thereSystem error 1326 has occurred.

Logon failure: unknown user name or bad password.
Instead of the operating system reprorting these messages, I want to hide
these and generate my own.

That's hard without knowing the contents of Connect.pl

Would to care to post it?

Cheers

Tim
 
J

Jürgen Exner

Tim said:
You have a typo:

ipconfig

Which in the context of his question is intentional.
If he hadn't a typo then there wouldn't be any error message to capture :)
That's hard without knowing the contents of Connect.pl

Actually, all he has to do is to capture STDERR, no matter what Connect.pl
does.

jue
 
T

Tim Southerwood

Jürgen Exner said:
Which in the context of his question is intentional.
If he hadn't a typo then there wouldn't be any error message to capture
:)

Yes of course - I mis-understood his message.
 
T

Tad McClellan

Simon said:
Hi guys, bit of a newbie here.

Im trying to learn error handling in perl.

Here are a couple of scenarios.

Im trying to write messages to the terminal/screen and output these over
operating system returned error messaages.

============================================================================

use Win32;

print `ipconig` || die "no good";



----------------------------------------- output is:

C:\Test>alright.pl

'ipconig' is not recognized as an internal or external command,

operable program or batch file.

no good at C:\Test\alright.pl line 4.


Your primary problem here is abusing backticks.

A secondary problem is precedence.

A tertiary problem (after fixing the precedence) is that print()
will succeed even when given an empty list to print.

They all disappear if you use the right tool for the job.

Backticks are appropriate if you want to capture the STDOUT of
on external program.

system() is appropriate if you simply want to run an external program
and let its STDOUT go where STDOUT normally goes:

system 'ipconig' and die "no good";
or
!system 'ipconig' or die "no good";

Instead of the operating system reprorting these messages, I want to hide
these and generate my own.


Error message are normally output on STDERR, so

perldoc -q STDERR

How can I capture STDERR from an external command?
 
S

Simon

Hi Tim, Jurgen!

You guys are great.

Tim I deliberately mispelt the ipconfig just to get the operating system's
error message.
 
S

Simon

Guys I really appreciate all your input thank you so much to all of you
experts :>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top