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

Hello Monks - Being a Newbie in perl, i am stuck in an if() situation in my code, which happens to check server health. Problem is, I am comparing 2 variables in if(), based on that perl will run commands locally or Remote, please the code snippet below :

I used arrows in below code to indicate whats what. Please help.
sub cpuchk() { my $SRVCHX = shift; message("Server Received is : $SRVCHX"); ----> Testing to chec +k Server name passed from outside(good) message("host test : $HOSTNME"); ----> testing to check curre +nt hostname(good) if($SRVCHX eq $HOSTNME ) ------>Problem here - when both t +he values are same, it still does not execute if and goes to else { message("Local Server Selected, Executing commands loc +ally.."); localcpucmds(); } else { message("Remote server selected: $SRVC +HX"); . . Executes commands on remote server...

Replies are listed 'Best First'.
Re: Perl IF Issue
by stevieb (Canon) on Aug 20, 2015 at 15:28 UTC

    Put print ">$HOSTNME< >$SRVCHX<"; right before the if statement. It should print without a line break, and the arrows should be right next to the expected text.

    First thing to do in these situations is verify that your data is exactly what you expect.

      Thanks Stevieb for your suggestion. Here how it looks like after i did what you told (when i keep the both the argurments same):

      >tantra.zdr.moksha.com < >tantra.zdr.moksha.com<

        Do a chomp $HOSTNME; prior to the if statement. That variable has an attached newline (\n) at the end if it.

        Update: See perldoc -f chomp to understand the purpose of chomp.

        -stevieb

Re: Perl IF Issue
by choroba (Cardinal) on Aug 20, 2015 at 15:27 UTC
    Are you sure the strings are exactly the same?

    Try comparing them character by character:

    die "Different length" if length $HOSTNME != length $SRVCHX; for my $pos (0 .. length($HOSTNME) - 1) { warn warn "$HOSTNME\n", " " x $pos, "| $pos\n$SRVCHX\n" if substr($HOSTNME, $pos, 1) ne substr($SRVCHX, $pos, 1); }

    Also, do you use strict? Just to make sure there's no typo in the variables' names.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Hi Choroba, thanks for your reply. yes i am using strict. And below is the output when i did the testting your ways :

      Different length at ./cpu_chk.pl line 149, <STDIN> line 1.

Re: Perl IF Issue
by dasgar (Priest) on Aug 20, 2015 at 16:00 UTC

    In addition to the suggestions about checking the values of the two variables, I've got two ideas. First thought: do you have the variable names correct? Using a variable named $HOSTNME instead of $HOSTNAME seems a bit odd to me personally and I'm just wondering if you might have a slight typo there. As choroba pointed out, using strict will help catch this kind of typo with variable names.

    The other idea is that maybe the two variables have basically the same string, but without exact case matching (such as 'MyServer' and 'myserver'). The eq operator is looking for exact matches, which means that it is doing case-sensitive matching.

    If you want/need case-insensitive matching, you just need to use a simple regex instead. Basically, change:

    if($SRVCHX eq $HOSTNME )

    to be:

    if($SRVCHX =~ m/$HOSTNME/i )

      You need to create boundaries to get an exact match when using that syntax:

      if($SRVCHX =~ m/^$HOSTNME$/i)

      or else things like "one" =~ /on/i; will match.

        Got one, missed one. The provided example value was an FQDN and contains dots. ;)

        Thanks Stevieb, your trick worked. Great Help:)

      Thanks dasgar

Re: Perl IF Issue
by Random_Walk (Prior) on Aug 21, 2015 at 09:22 UTC

    Good $localtime

    I see this in your code:
        message("host test : $HOSTNME");  ----> testing to check current hostname(good)
    I guess $HOSTNAME is the name of the host you are running on. If so a nice portable way to get this, is to use Sys::Hostname (in Perl Core)

    use strict; use warnings; use Sys::Hostname; print "I am running on:". hostname .", thank you and goodnight\n";
    Sys::Hostname creates the constant hostname that provides a nice clean, portable, line break free instance of your hostname. You may want to copy it to $host for easier use in strings:

    use strict; use warnings; use Sys::Hostname; my $host = hostname; print "I am running on: $host, thank you and goodnight\n";

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!