Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script that I copied from somewhere:

#!/usr/local/bin/perl

@machines = qw(
    host1.foo.com
    host2.foo.com
    host3.foo.com
);
$HTML = "<html>\n";
$HTML .= " <body>\n";
$HTML .= " 
\n";
foreach $machine (@machines) {
    $response = `ping $machine`;
    $HTML .= "$machine: $response";
}
$HTML .= " 
\n"; $HTML .= " </body>\n"; $HTML .= "</html>\n"; open(HTMLFILE, ">pingresults.html") || die "Couldn't open pingresults\n"; print HTMLFILE "$HTML\n"; close (HTMLFILE);

How can I get the output to print to STDOUT rather than an HTML file?

I know this seems kind of pointless, but I eventually want to be able to pipe the output to an external program for monitoring purposes.

I'm new to perl (did you guess?), so thanks in advance for your patience.

rgds,
protos

  • Comment on How do I print output to STDOUT instead of to an HTML file?

Replies are listed 'Best First'.
Re: How do I print output to STDOUT instead of to an HTML file?
by little (Curate) on Apr 18, 2001 at 21:43 UTC
    unless you redefine STDOUT everything you write to STDOUT will go there :-)
    print STDOUT "processed pingresults.html\n";
    but you could also ommit specifiying STDOUT eg.:
    print "processed pingresults.html\n";

    Have a nice day
    All decision is left to your taste
      little is definately on the right track here, but if you're this new to Perl, I'll try to help a little (being a little new to Perl myself, I can relate).

      The lines you're using to print a message have the following effect:

      open(HTMLFILE, ">pingresults.html") || die "Couldn't open pingresults\ +n"; print HTMLFILE "$HTML\n";
      The first line opens a file with read/write access (the '>' means you'll be doing read/write - no character there would indicate read only). You're associating with this file the filehandle "HTMLFILE".

      In the print statement, the filehandle is optional, like this:

      print HTMLFILE "This will print to file"; print "This will print to STDOUT";
      Hopefully this will help you get started.

      -Sherlock
        The first line opens a file with read/write access (the '>' means you'll be doing read/write - no character there would indicate read only). You're associating with this file the filehandle "HTMLFILE".

        Almost--the ">" prefix means it's opening the file for write access only--and if the file exists already, it's truncated (that is to say, it's erased the moment you open the file with >). For read/write, you'd want to use +< or +>> (see open for why not to use +>, and why not to do this in a text file anyway).

        When you omit the filehandle in the print statement, it prints to the selected filehandle, which is STDOUT when you start but can be changed using select (e.g. select (HTMLFILE);)



        If God had meant us to fly, he would *never* have give us the railroads.
            --Michael Flanders

Re: How do I print output to STDOUT instead of to an HTML file?
by protos (Initiate) on Apr 18, 2001 at 22:35 UTC
    I'd like to thank everyone for their speedy and very helpful replies. This was probably the best lesson in perl that I've ever gotten. Books are good, but they aren't the same as collaboration.

    With your help my script now looks like this:

    #!/usr/local/bin/perl -w use strict; my @machines = ('ldap0.slb.com','ldap.slb.com','ldap2.slb.com'); foreach my $machine (@machines) { my $response = `ping -l 64 -n 1 $machine`; my $output .= "$machine: $response"; print "$output\n"; }

    Now I just need to work on parsing out the garbage I don't want with regexen. : (

    Thanks again,
    protos

Re: How do I print output to STDOUT instead of to an HTML file?
by tinman (Curate) on Apr 18, 2001 at 21:47 UTC

    The quickest thing for you to do is to comment out the opening of the file and closing, ie: these lines

    open(HTMLFILE,">pingresults.html") || die "Couldn't open pingresults"; close (HTMLFILE);
    Place a # mark in front of it. and instead of print HTMLFILE "$HTML\n"; just say print "$HTML\n";, or if you want to be explicit about it, replace HTMLFILE with STDOUT in the print statement. So,
    open(HTMLFILE, ">pingresults.html") || die "Couldn't open pingresults\ +n"; print HTMLFILE "$HTML\n"; close (HTMLFILE);
    becomes
    # open(HTMLFILE, ">pingresults.html") || die "Couldn't open pingresult +s\n"; print STDOUT "$HTML\n"; # close (HTMLFILE);

    HTH
Re: How do I print output to STDOUT instead of to an HTML file?
by iguane (Beadle) on Apr 18, 2001 at 22:04 UTC
    I proposed this part of code
    #!/usr/local/bin/perl my @machines =( 'host1.foo.com' , 'host2.foo.com' , 'host3.foo.com' ); my $HTML = "\n\n\n"; for ( @machines ) { $HTML .= $machine.': '.`ping $_`; } $HTML .= "\n\n\n"; print $HTML
    i write $html="\n\n\n"; to replace the tree lines which seem really not beautiful.
    Sure it's the same but shorter.
    The content will be automaticly put on the STDOUT by using a print command.
Re: How do I print output to STDOUT instead of to an HTML file?
by THRAK (Monk) on Apr 18, 2001 at 21:53 UTC
    First off when posting code put it in <code> <\code> tags. So:
    #!/usr/local/bin/perl @machines = qw( host1.foo.com host2.foo.com host3.foo.com ); $HTML = "\n"; $HTML .= " \n"; $HTML .= " \n"; foreach $machine (@machines) { $response = `ping $machine`; $HTML .= "$machine: $response"; } $HTML .= " \n"; $HTML .= " \n"; $HTML .= "\n"; open(HTMLFILE, ">pingresults.html") || die "Couldn't open pingresults\ +n"; print HTMLFILE "$HTML\n"; close (HTMLFILE);
    Kill the "open" and "close" lines and change the print to:
    print "$HTML\n";
    There are better ways to do these kinds of things, so do some reading and searching around here and you will learn some things for the long run. Also, learn the first two rules of good Perl: use the "-w" flag on the #!/usr/local/bin/perl -w line and learn/read about "use strict". Life will be much less painful in the long run.

    -THRAK
    www.polarlava.com