For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > system v backticks









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author system v backticks
Greg John ** CTR ** Keenan

2005-07-28, 4:00 am

Hi,

Can someone explain the difference between backticks and system when
evaluated in this if statement:


sub getDate {
print "start date\n";
if ( system("/bin/date") ) {
print "can't get date\n";
exit(2);
}
print "finish date\n";
}

Returns the following:

start date
Thu Jul 28 12:13:59 EST 2005
finish date



While this:

sub getDate {
print "start date\n";
if ( `/bin/date` ) {
print "can't get date\n";
exit(2);
}
print "finish date\n";
}

Returns this:

start date
can't get date


O Lucent Technologies
Level 1, 68 Waterloo Rd.
North Ryde NSW 2113
Australia
Tel: +61 (0)2 9491 6898
Mob: +61 (0)434 127 545

Wiggins d'Anconia

2005-07-28, 4:00 am

Keenan, Greg John (Greg)** CTR ** wrote:
> Hi,
>
> Can someone explain the difference between backticks and system when
> evaluated in this if statement:
>


The difference really isn't specific to this context, it is the inherent
difference between the two that is affecting the outcome.

perldoc -f system
perldoc perlop

For the details. The main difference is that 'system' runs the command
and returns the exit value, which is (usually) 0 for success and
non-zero for failure. Perl is successfully shelling out to /bin/date and
it successfully runs so it returns 0 so the if evaluates to false.
The fact that you see the date printed to the terminal in this case is
merely a side effect of the fact that Perl and the subshell share the
same standard out filehandle and it happens to be your terminal screen.
Backticks on the other hand return whatever output was provided by the
program being run, so in this case the date string is returned, which
evaluates to true, so even though the program ran successfully the if
evaluates and you get the error message. In the first case your if
blocks should be reversed, in the second case you should capture the
output to a scalar or array and if you want to check the exit status
then you would need to look in $?. The docs for 'system' explain it more.

>
> sub getDate {
> print "start date\n";
> if ( system("/bin/date") ) {
> print "can't get date\n";
> exit(2);
> }
> print "finish date\n";
> }
>
> Returns the following:
>
> start date
> Thu Jul 28 12:13:59 EST 2005
> finish date
>
>
>
> While this:
>
> sub getDate {
> print "start date\n";
> if ( `/bin/date` ) {
> print "can't get date\n";
> exit(2);
> }
> print "finish date\n";
> }
>
> Returns this:
>
> start date
> can't get date
>


Hopefully this is an example, don't use either to get the date. Perl has
built-in functions for that. Shelling out with either of the above
methods is a last resort when there is no other way to accomplish the
same task from within Perl.

http://danconia.org
Jeff 'japhy' Pinyan

2005-07-28, 4:00 am

On Jul 28, Keenan, Greg John (Greg)** CTR ** said:

> sub getDate {
> print "start date\n";
> if ( system("/bin/date") ) {
> print "can't get date\n";
> exit(2);
> }
> print "finish date\n";
> }


system() executes a command, and returns the shell's error code. 0
indicates success, non-0 indicates failure. It does NOT return the
output. If output is produced from the program, it's printed to STDOUT.

> sub getDate {
> print "start date\n";
> if ( `/bin/date` ) {
> print "can't get date\n";
> exit(2);
> }
> print "finish date\n";
> }


Backticks execute a command and return its output. Since the return value
is true in this case, the if() statement's condition is true, and you call
exit().

If system() or ``s fail, you should check $! to see the error message.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com