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


In reply to Different output of print statement. by srm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.