How to eliminate the extra 0's in printing to html lines?

Discussion in 'Perl Misc' started by cibalo@gmx.co.uk, Jul 3, 2006.

  1. Guest

    Hello,

    The following script produces two extra 0's at the end of the lines ---
    see below.
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "# which sendmail<br>";
    $out = system("which sendmail");
    print "$out<br>\n";
    print "<br>";
    print "# which perl<br>";
    $out = system("which perl");
    print "$out<br>\n";
    print "<br>";

    And the browser displays as:
    # which sendmail
    /usr/sbin/sendmail 0

    # which perl
    /usr/bin/perl 0

    Please help me to eliminate the two extra 0's at the end of the lines.
    I have no idea where these 0's are coming from.

    Thank you very much in advance!!!
     
    , Jul 3, 2006
    #1
    1. Advertising

  2. wrote:

    > The following script produces two extra 0's at the end of the lines ---
    > see below.
    > #!/usr/bin/perl
    > print "Content-type: text/html\n\n";
    > print "# which sendmail<br>";
    > $out = system("which sendmail");
    > print "$out<br>\n";
    > print "<br>";
    > print "# which perl<br>";
    > $out = system("which perl");
    > print "$out<br>\n";
    > print "<br>";
    >
    > And the browser displays as:
    > # which sendmail
    > /usr/sbin/sendmail 0
    >
    > # which perl
    > /usr/bin/perl 0
    >
    > Please help me to eliminate the two extra 0's at the end of the lines.
    > I have no idea where these 0's are coming from.


    I see the same results here. The problem is that you can't trust the
    return value of the 'system'-command. The following should work:

    $out = `which perl`;

    --
    Bart
     
    Bart Van der Donck, Jul 3, 2006
    #2
    1. Advertising

  3. -berlin.de Guest

    Bart Van der Donck <> wrote in comp.lang.perl.misc:
    > wrote:
    >
    > > The following script produces two extra 0's at the end of the lines ---
    > > see below.
    > > #!/usr/bin/perl
    > > print "Content-type: text/html\n\n";
    > > print "# which sendmail<br>";
    > > $out = system("which sendmail");
    > > print "$out<br>\n";
    > > print "<br>";
    > > print "# which perl<br>";
    > > $out = system("which perl");
    > > print "$out<br>\n";
    > > print "<br>";
    > >
    > > And the browser displays as:
    > > # which sendmail
    > > /usr/sbin/sendmail 0
    > >
    > > # which perl
    > > /usr/bin/perl 0
    > >
    > > Please help me to eliminate the two extra 0's at the end of the lines.
    > > I have no idea where these 0's are coming from.

    >
    > I see the same results here. The problem is that you can't trust the
    > return value of the 'system'-command.


    You can trust it to be what it it supposed to be, but that is not the
    output of the invoked command.

    > The following should work:
    >
    > $out = `which perl`;


    Read "perldoc -f system" with attention to the third paragraph to see
    *why* this works.

    Anno
     
    -berlin.de, Jul 3, 2006
    #3
  4. wrote:
    > The following script produces two extra 0's at the end of the lines
    > --- see below.
    > #!/usr/bin/perl
    > print "Content-type: text/html\n\n";
    > print "# which sendmail<br>";
    > $out = system("which sendmail");
    > print "$out<br>\n";
    > print "<br>";
    > print "# which perl<br>";
    > $out = system("which perl");
    > print "$out<br>\n";
    > print "<br>";
    >
    > And the browser displays as:
    > # which sendmail
    > /usr/sbin/sendmail 0
    >
    > # which perl
    > /usr/bin/perl 0


    That's exactly what should happen in this script.

    > Please help me to eliminate the two extra 0's at the end of the lines.
    > I have no idea where these 0's are coming from.


    You are printing them ;-)
    If you don't want to see the return value of your system() calls, then don't
    print it, i.e. change
    print "$out<br>\n";
    to
    print "<br>\n";

    jue
     
    Jürgen Exner, Jul 3, 2006
    #4
  5. writes:

    > The following script produces two extra 0's at the end of the lines ---
    > see below.
    > #!/usr/bin/perl
    > print "Content-type: text/html\n\n";
    > print "# which sendmail<br>";
    > $out = system("which sendmail");
    > print "$out<br>\n";
    > print "<br>";
    > print "# which perl<br>";
    > $out = system("which perl");
    > print "$out<br>\n";
    > print "<br>";
    >
    > And the browser displays as:
    > # which sendmail
    > /usr/sbin/sendmail 0
    >
    > # which perl
    > /usr/bin/perl 0
    >
    > Please help me to eliminate the two extra 0's at the end of the lines.
    > I have no idea where these 0's are coming from.


    They're the value of $out when it's printed. System() does not do what you
    think it does. For details have a look at the docs for the system() function:

    perldoc -f system

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jul 3, 2006
    #5
  6. "Bart Van der Donck" <> wrote in
    news::

    > The problem is that you can't trust the
    > return value of the 'system'-command.


    In what way does the return value of the system function match its
    documentation?

    perldoc -f system

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 3, 2006
    #6
  7. Ciba LO Guest

    Hello Bart, Steffen, Anno, jue, sherm and Sinan!

    Many thanks to you guys for replying to my post. Both the backquote
    substitution and the omission of $out in the print statements are
    working okayed to me.

    Ciba

    Bart Van der Donck wrote:
    >
    > I see the same results here. The problem is that you can't trust the
    > return value of the 'system'-command. The following should work:
    >
    > $out = `which perl`;
    >
    > --
    > Bart
     
    Ciba LO, Jul 4, 2006
    #7
  8. Joe Smith Guest

    Bart Van der Donck wrote:

    > I see the same results here. The problem is that you can't trust the
    > return value of the 'system'-command.


    You most certainly _CAN_ trust the return value of the system() function.

    The return value is documented, it's just not what you think it is.

    if (system($command) == 0) {
    print "The command '$command' executed with no errors\n";
    } else {
    warn "The command '$command' returned a non-zero error code: $?\n";
    }

    -Joe
     
    Joe Smith, Jul 5, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ciba LO
    Replies:
    4
    Views:
    186
    Ciba LO
    Jul 8, 2006
  2. Igor Korot

    Eliminate "extra" variable

    Igor Korot, Dec 6, 2013, in forum: Python
    Replies:
    0
    Views:
    65
    Igor Korot
    Dec 6, 2013
  3. Peter Otten

    Re: Eliminate "extra" variable

    Peter Otten, Dec 8, 2013, in forum: Python
    Replies:
    0
    Views:
    88
    Peter Otten
    Dec 8, 2013
  4. Tim Chase

    Re: Eliminate "extra" variable

    Tim Chase, Dec 8, 2013, in forum: Python
    Replies:
    0
    Views:
    74
    Tim Chase
    Dec 8, 2013
  5. Tim Chase

    Re: Eliminate "extra" variable

    Tim Chase, Dec 8, 2013, in forum: Python
    Replies:
    0
    Views:
    81
    Tim Chase
    Dec 8, 2013
Loading...

Share This Page