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

I would expect the following code to either print all three conditions or to print none of them - however, when the host is up, it always prints:

condition1
condition3

What's up with that?! Is this a bug? I have the dreadful feeling this may be a FAQ and betray a profound misunderstanding on my part... but it makes no sense... Please enlighten me!!!

Code:

#!/opt/perl5/bin/perl # What the heck is going on here?! use Net::Ping; $host1="hqlinuxdev.hj-int"; $host2=hqlinuxdev.hj-int; $host3='hqlinuxdev.hj-int'; print "condition1\n" unless pingecho("$host1"); print "condition2\n" unless pingecho("$host2"); print "condition3\n" unless pingecho("$host3");

Replies are listed 'Best First'.
Re: Is this a bug or am I insane/stupid/ignorant/etc
by chromatic (Archbishop) on Feb 01, 2002 at 17:37 UTC
    $host2 is set to 0. You've made a bareword. Turn on strict and warnings, and you'll see the problem. You could also print it to see: print "($host2)\n";

    You have to quote strings.

Re: Is this a bug or am I insane/stupid/ignorant/etc
by VSarkiss (Monsignor) on Feb 01, 2002 at 17:37 UTC

    Add either use strict; or a -w option (or use warnings if you have 5.6 or greater) to your code. You will see exactly what the problem is.

    HTH

Re: Is this a bug or am I insane/stupid/ignorant/etc
by YuckFoo (Abbot) on Feb 01, 2002 at 17:41 UTC
    Add your variables to the print statement, see if they are what you think they are.

    print "$host1\n" unless ...

    Then think about starting all your programs with 'use strict;' to see about eliminating these kinds of errors.

    YuckFoo