1

Very new to perl and have been stuck for quite awhile on this.

If I change the variable from READSTDIN to google.com, it says google.com is online as it should. If I use the STDIN and input google.com and print $host it prints google.com, however in the ping it doesn't work.

Sample output:

    perl perl.pl
What is the website that is offline or displaying an error?google.com
Warning: google.com
 appears to be down or icmp packets are blocked by their server

Code:

use strict;
use warnings;
use Net::Ping;

#optionally specify a timeout in seconds (Defaults to 5 if not set)
my $timeout = 10;

# Create a new ping object
my $p = Net::Ping->new("icmp");

#Domain variable
print "What is the website that is offline or displaying an error?";
my $host = readline STDIN;

# perform the ping
if ( $p->ping( $host, $timeout ) ) {
    print "Host $host is alive\n";
} else {
    print "Warning: $host appears to be down or icmp packets are blocked by their server\n";
}

# close our ping handle
$p->close();

If I change the variable from READSTDIN to google.com, it says google.com is online as it should. If I use the STDIN and input google.com and print $host it prints google.com, however in the ping it doesn't work. I appreciate anyone who can help me at all!

Miller
  • 34,962
  • 4
  • 39
  • 60
Adam Black
  • 11
  • 1
  • 2
  • possible duplicate of [I'm looking for some clarification on chomp](http://stackoverflow.com/questions/7290445/im-looking-for-some-clarification-on-chomp) – Oesor Aug 26 '14 at 19:07
  • possible duplicate of [Why does my file content/user input not match? (missing chomp canonical)](http://stackoverflow.com/questions/25571878/why-does-my-file-content-user-input-not-match-missing-chomp-canonical) – RobEarl Aug 29 '14 at 19:39

1 Answers1

1

Note the newline in your input:

    perl perl.pl
What is the website that is offline or displaying an error?google.com
Warning: google.com <--- newline after google.com puts the rest of the output on the next line...
 appears to be down or icmp packets are blocked by their server

You should be using chomp to remove the newline from your input:

chomp( my $host = readline STDIN );

Or more simply:

chomp( my $host = <STDIN> );  # same thing as above
Miller
  • 34,962
  • 4
  • 39
  • 60
Oesor
  • 6,632
  • 2
  • 29
  • 56
  • I'm not bothered by the new line being outputted (but thanks for helping me there) -- I'm more concerned that it doesn't recognize that my variable READLINE STDIN "google.com" is online. If I change the variable from my $host = readline STDIN; to my $host = "google.com" The script correctly outputs that google.com is online, if however I input google.com when it asks me "What is the website that is offline or displaying an error?" It tells me google.com is offline. – Adam Black Aug 26 '14 at 19:23
  • 1
    No. It's telling you that "google.com\n" is offline. Which it is. There's no tld 'com\n' so the name fails to resolve. – Oesor Aug 26 '14 at 19:30
  • Because you are not chomping the input, your program is telling you that "google.com\n" is off line. Your input buffer $line, includes the newline, because that is how buffered input on ttys works. Since there is no server with a newline in its name, you can't ping it. The when you go to print it, the newline in the buffer causes the newline in the output. – Len Jaffe Aug 26 '14 at 19:31
  • Thanks so much! Apologies I misunderstood, that totally makes sense. What is the website that is offline or displaying an error?google.com Host google.com is alive :) – Adam Black Aug 26 '14 at 19:39