system/backticks output not as expected

system/backticks output not as expected

am 30.10.2009 14:37:50 von extern.Lars.Oeschey

Hi,

I have a problem getting the output of a system call. It's a simple
code:

my $return=`$command`;
print "$?\n";
print "result:$return\n";

(I am calling a command which actually doesn't exist, to check error
handling)

as result on the console I have:

Das System kann das angegebene Laufwerk nicht finden.
256
result:

The first error says it can't find the program. Actually I expected this
in $return.

compared to system():

my $return=system("$command");
print "$?\n";
print "result:$return\n";

Das System kann das angegebene Laufwerk nicht finden.
256
result:256

So obviously system() returns the error code. But why don't the
backticks return anything at all? Ideally I'd want all the output
catched to log, but still be able to react on errors...

Lars

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: system/backticks output not as expected

am 30.10.2009 14:49:17 von Conor Lillis

I usually capture the output for backticks to an array rather than a
string so try

my @return=`$command`;
Foreach my $line(@output){print "$line\n"}

Regards,
Conor Lillis
---------------------------------------------
Senior Systems Administrator,
Group Network Services,
Phone +353-1-616-2540

Anglo Irish Bank
www.angloirishbank.ie
mailto:conorlillis@angloirishbank.ie
---------------------------------------------

-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of
Oeschey, Lars (I/ET-83, extern)
Sent: 30 October 2009 13:38
To: activeperl@listserv.ActiveState.com
Subject: system/backticks output not as expected

Hi,

I have a problem getting the output of a system call. It's a simple
code:

my $return=`$command`;
print "$?\n";
print "result:$return\n";

(I am calling a command which actually doesn't exist, to check error
handling)

as result on the console I have:

Das System kann das angegebene Laufwerk nicht finden.
256
result:

The first error says it can't find the program. Actually I expected this
in $return.

compared to system():

my $return=system("$command");
print "$?\n";
print "result:$return\n";

Das System kann das angegebene Laufwerk nicht finden.
256
result:256

So obviously system() returns the error code. But why don't the
backticks return anything at all? Ideally I'd want all the output
catched to log, but still be able to react on errors...

Lars

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


************************************************************ **********
Private, Confidential and Privileged. This e-mail and any files and attachments transmitted with it are confidential and/or privileged. They are intended solely for the use of the intended recipient. The content of this e-mail and any file or attachment transmitted with it may have been changed or altered without the consent of the author. If you are not the intended recipient, please note that any review, dissemination, disclosure, alteration, printing, circulation or transmission of this e-mail and/or any file or attachment transmitted with it, is prohibited and may be unlawful. This e-mail and any files and attachments transmitted with it are unencrypted unless specifically advised otherwise. If you have received this e-mail or any file or attachment transmitted with it in error please
notify Anglo Irish Bank Corporation Limited, Stephen Court, 18/21 St Stephen's Green, Dublin 2, Ireland, telephone no: +353-1-6162000.
Directors: D.M. O'Connor Chairman, A.M.R. Aynsley (Australian) Chief Executive, F.M. Daly, A.M. Dukes, M.A. Keane.
Registered Office: Stephen Court, 18/21 St Stephen's Green, Dublin 2 Ireland
Registered in Ireland: No 22045
Anglo Irish Bank Corporation Limited is regulated by the Financial Regulator. Anglo Irish Bank Corporation Limited (trading as Anglo Irish Bank Private Banking) is regulated by the Financial Regulator. Anglo Irish Assurance Company Limited is regulated by the Financial Regulator.
************************************************************ **********

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: system/backticks output not as expected

am 30.10.2009 14:57:06 von Brian Raven

Oeschey, Lars (I/ET-83, extern) <> wrote:
> Hi,
>
> I have a problem getting the output of a system call. It's a simple
> code:
>
> my $return=`$command`;
> print "$?\n";
> print "result:$return\n";
>
> (I am calling a command which actually doesn't exist, to check error
> handling)
>
> as result on the console I have:
>
> Das System kann das angegebene Laufwerk nicht finden.
> 256
> result:
>
> The first error says it can't find the program. Actually I expected
> this in $return.
>
> compared to system():
>
> my $return=system("$command");
> print "$?\n";
> print "result:$return\n";
>
> Das System kann das angegebene Laufwerk nicht finden.
> 256
> result:256
>
> So obviously system() returns the error code. But why don't the
> backticks return anything at all? Ideally I'd want all the output
> catched to log, but still be able to react on errors...

Backticks only capture output from stdout, while error messages are
generally output on stderr. In unix-like shells it usual to redirect
stderr to stdout by adding 2>&1 to the command line. I don't know if it
works the same on win32, but it might be worth a try, if that is your
platform.

Also, you could always try verifying that the command exists before
trying to run it.

HTH

--
Brian Raven
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: system/backticks output not as expected

am 30.10.2009 15:44:12 von Serguei Trouchelle

Brian Raven wrote:

> Backticks only capture output from stdout, while error messages are
> generally output on stderr. In unix-like shells it usual to redirect
> stderr to stdout by adding 2>&1 to the command line. I don't know if it
> works the same on win32, but it might be worth a try, if that is your
> platform.

Yes, it works.

--
Serguei Trouchelle
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs