Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Different output of print statement.

by srm (Initiate)
on Oct 03, 2013 at 23:14 UTC ( #1056814=perlquestion: print w/ replies, xml ) Need Help??
srm has asked for the wisdom of the Perl Monks concerning the following question:

I have simple code that is trying to print same thing twice. First I try to print via a scalar and then I try to print samething using an array. The output of two print statements is different. Could you please help me understand the reason(s) for this difference in behaviour? Thanks, srm

#!/usr/bin/perl my $dest = shift; my $cmd = "ping -c 1 $dest"; @output = `$cmd`; print "\n$cmd\n\n"; print "\n@output\n";

Upon executing the above code, test.pl 139.130.4.5, the output is:

ping -c 1 139.130.4.5 PING 139.130.4.5 (139.130.4.5) 56(84) bytes of data. 64 bytes from 139.130.4.5: icmp_seq=1 ttl=42 time=187 ms --- 139.130.4.5 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 187ms rtt min/avg/max/mdev = 187.409/187.409/187.409/0.000 ms

I understand that while using scalar it prints just the command. But while using array it executes the command and prints the output of the command. Could you please help me understand the reasoning for this differnce in behaviour? Thanks, srm

Comment on Different output of print statement.
Select or Download Code
Re: Different output of print statement.
by toolic (Chancellor) on Oct 03, 2013 at 23:23 UTC
    The backticks (``) execute the ping command and store the output in the @output array. The output of the ping command is only displayed when you print the @output array.

      Thanks Backticks,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056814]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2014-09-23 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (221 votes), past polls