Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

system return value makes for strange logic
I was writing a short script to create a mysql database and kept getting
confusing results:

system("mysqladmin --user='$user' --password='$pass' create $db")
or die "$db not created due to error\n";

The function lead to die being called and printing the error message but the
database was being created successfully.  I changed it to:

system("mysqladmin --user='$user' --password='$pass' create $db")
and die "$db not created due to error\n";

and even though it doesn't look right at first glance, it makes sense
because system apparently returns 0 with success and numbers > 0 for
failure.  I read about it in 'Programming Perl' and saw the explanation
that the return value gives information about the nature of the failure.
Isn't this strange?  Don't we normally look to $! for errors and consider 0
to mean failure?  I can accept it, but 'do and die' sounds funny compared
to the usual 'do or die'.

wana

Report this thread to moderator Post Follow-up to this message
Old Post
wana
10-29-04 08:57 PM


Re: system return value makes for strange logic
On Fri, 29 Oct 2004 11:25:35 -0400, wana wrote:

> I was writing a short script to create a mysql database and kept getting
> confusing results:
>
> system("mysqladmin --user='$user' --password='$pass' create $db")
>         or die "$db not created due to error\n";
>
> The function lead to die being called and printing the error message but t
he
> database was being created successfully.  I changed it to:
>
> system("mysqladmin --user='$user' --password='$pass' create $db")
>         and die "$db not created due to error\n";
>
> and even though it doesn't look right at first glance, it makes sense
> because system apparently returns 0 with success and numbers > 0 for
> failure.  I read about it in 'Programming Perl' and saw the explanation
> that the return value gives information about the nature of the failure.
> Isn't this strange?  Don't we normally look to $! for errors and consider 
0
> to mean failure?  I can accept it, but 'do and die' sounds funny compared
> to the usual 'do or die'.
>
Try this for a more sensible format:

system("mysqladmin --user='$user' --password='$pass' create $db") == 0 or
die "$db not created due to error\n";

BTW why aren't you using perl DBI? That will give you more control over
errors and the like.
Chris.

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Cole
10-29-04 08:57 PM


Re: system return value makes for strange logic
 
> Try this for a more sensible format:
>
> system("mysqladmin --user='$user' --password='$pass' create $db") == 0 or
> die "$db not created due to error\n";
>
> BTW why aren't you using perl DBI? That will give you more control over
> errors and the like.
> Chris.

I didn't realize you could create a database with DBI.  I'll have to read up
on it more.  I actually use DBI with CGI and MySQL a lot but I thought you
had to create the database first and specify it for DBI->connect.  Thanks
for the suggestions.  I like your == 0 or alternative.

wana

Report this thread to moderator Post Follow-up to this message
Old Post
wana
10-29-04 08:57 PM


Re: system return value makes for strange logic
wana <ioneabu@yahoo.com> wrote in
news:10o4odkcq2dgm01@news.supernews.com:

> and even though it doesn't look right at first glance, it makes sense
> because system apparently returns 0 with success and numbers > 0 for
> failure.  I read about it in 'Programming Perl' and saw the
> explanation that the return value gives information about the nature
> of the failure. Isn't this strange?  Don't we normally look to $! for
> errors and consider 0 to mean failure?  I can accept it, but 'do and
> die' sounds funny compared to the usual 'do or die'.

The reason is that system() returns the exit status of the program it runs.
What that status is is under the program's control, not perl's.  The
convention (which again has nothing to do with Perl) is that a program
exits with 0 on success and any number of non-zero values upon failure.
Why?  Because there's only one way a program can succeed, but quite a few
ways it can fail (the number of such ways varying from program to program)
and it's helpful if it can signal *why* it failed.

Report this thread to moderator Post Follow-up to this message
Old Post
Eric Bohlman
10-29-04 08:57 PM


Re: system return value makes for strange logic
Eric Bohlman <ebohlman@omsdev.com> writes:
> wana <ioneabu@yahoo.com> wrote in
> news:10o4odkcq2dgm01@news.supernews.com:
> 
>
> The reason is that system() returns the exit status of the program it runs
.
> What that status is is under the program's control, not perl's.  The
> convention (which again has nothing to do with Perl) is that a program
> exits with 0 on success and any number of non-zero values upon failure.
> Why?  Because there's only one way a program can succeed, but quite a few
> ways it can fail (the number of such ways varying from program to program)
> and it's helpful if it can signal *why* it failed.

The history of that convention has mainly to do with Unix. I think VMS
is one system that treats return status from processes differently. At
least the C standard committee found it necessary to invent
preprocessor symbols for denoting successful and failing process exit,
since 0 for success was not universal.

Since I'm on the subject: it often happens in Perl code that people
take the return value from 'system', divide it by 256, and think they
have the exit status. They have that, if the process didn't dump core
(if we're on a Unix system). If it dumped core, the lowest bits are
nonzero. Thus, a program crash may be misinterpreted as successful
exit. The doc for 'system' does say this, but it may be easy to forget.

Report this thread to moderator Post Follow-up to this message
Old Post
Arndt Jonasson
10-29-04 08:57 PM


Re: system return value makes for strange logic
Quoth Arndt Jonasson <do-not-use@invalid.net>:
> Since I'm on the subject: it often happens in Perl code that people
> take the return value from 'system', divide it by 256, and think they
> have the exit status. They have that, if the process didn't dump core
> (if we're on a Unix system). If it dumped core, the lowest bits are
> nonzero. Thus, a program crash may be misinterpreted as successful
> exit. The doc for 'system' does say this, but it may be easy to
> forget.

Actually, they lose more than just whether or not it dumped core.  If it was
interrupted by *any* signal, that goes in the lower bits.

Try:
perl -e 'print system("sleep 30")."\n";'
and hit Ctl-C while it's sleeping.



Report this thread to moderator Post Follow-up to this message
Old Post
David Gale
10-29-04 08:57 PM


Re: system return value makes for strange logic
Eric Bohlman (ebohlman@omsdev.com) wrote on MMMMLXXVII September MCMXCIII
in < URL:news:Xns9591729577743ebohlmanomsdevc
om@130.133.1.4>:
%%  wana <ioneabu@yahoo.com> wrote in
%%  news:10o4odkcq2dgm01@news.supernews.com:
%%
%% > and even though it doesn't look right at first glance, it makes sense
%% > because system apparently returns 0 with success and numbers > 0 for
%% > failure.  I read about it in 'Programming Perl' and saw the
%% > explanation that the return value gives information about the nature
%% > of the failure. Isn't this strange?  Don't we normally look to $! for
%% > errors and consider 0 to mean failure?  I can accept it, but 'do and
%% > die' sounds funny compared to the usual 'do or die'.
%%
%%  The reason is that system() returns the exit status of the program it ru
ns.
%%  What that status is is under the program's control, not perl's.  The
%%  convention (which again has nothing to do with Perl) is that a program
%%  exits with 0 on success and any number of non-zero values upon failure.
%%  Why?  Because there's only one way a program can succeed, but quite a fe
w
%%  ways it can fail (the number of such ways varying from program to progra
m)
%%  and it's helpful if it can signal *why* it failed.


Hindsight is 20/20 of course, but it there would have been some other
options. First, there's not really a reason the return value of system
should indicate the reason why - open doesn't use its return value to
indicate the reason for failure either. Instead, $! is set. And a system
that fails sets $? already, which gives the same information. I cannot
recall I ever used the return value of system other than for its boolean
value - if I want to know the reason, I always use $?. system could also
have returned a magical value, that in boolean context returned false on
a failure, and true on success, but in numerical context, it just acts
as $? now.



Abigail
--
perl -swleprint -- -_='Just another Perl Hacker'

Report this thread to moderator Post Follow-up to this message
Old Post
Abigail
10-30-04 01:56 AM


Re: system return value makes for strange logic
wana wrote:
> system("mysqladmin --user='$user' --password='$pass' create $db")
>        or die "$db not created due to error\n";
>
> The function lead to die being called and printing the error message
> but the database was being created successfully.  I changed it to:
>
> system("mysqladmin --user='$user' --password='$pass' create $db")
>        and die "$db not created due to error\n";
>
> and even though it doesn't look right at first glance, it makes sense
> because system apparently returns 0 with success and numbers > 0 for
> failure.

Why don't you just check the documentation for the functions you are using?
It's explained right there in the third paragraph of "perldoc -f system".

jue



Report this thread to moderator Post Follow-up to this message
Old Post
Jürgen Exner
10-30-04 08:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:14 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.