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

Hello monks , I am a littel new to perl ,, my script is excuting a ping command to check if it can ping a server , i just need to store the result inot array or variable so I can print it in a nice format ,, I don't need the output to go to the command line .. I need to store to a variable and print it later in the script in the format I want ,, right now I am trying to redrict the output to a file
open (PING, ">>dummy"); unless ($pid = fork) { unless (fork) { print PING exec "ping $host"; die "no exec"; exit 0; } exit 0; } waitpid($pid,0); close(PING);

Replies are listed 'Best First'.
Re: storing result in variable
by davidrw (Prior) on Nov 03, 2005 at 17:38 UTC

      You may be right about Net::Ping to work better than a system call. But it slightly differs from the ping-program. The default protocol for Net::Ping is tcp where ping uses icmp. The tcp-connection requires a running echo-service on the remote machine whereas the icmp-connection (usually) requires the icmp-packages not to be explicitly dropped.

      With Net::Ping you can specify icmp as the connection method but this requires root permissions.

      --
      trust in bash
      but tie your camel
Re: storing result in variable
by inman (Curate) on Nov 03, 2005 at 17:37 UTC
    Try using backticks which enable you to execute a command and capture the output (STDOUT by default). e.g.
    my $output = `ping $host`;

    This is the simplest option and is probably OK for a command such as ping which will only take a short amount of time to run.

Re: storing result in variable
by hubb0r (Pilgrim) on Nov 04, 2005 at 05:51 UTC

    You can go about it by opening it with the following:

    open my $ping, "ping $host |"; my @pings = <$ping>; close $ping;

    Which will leave you with an array of the ping results.

    You can read more about this in perlopentut or perlipc