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

Im sure this is an easy question, but since im a perl n00b, here it goes

I need to run an executable to my Ubuntu Linux box, get the status of something then use that status to fill out and if/else statement

This is what I have right now

my $enable_poe = `poe ds`; if ($enable_poe eq 'POE device missing or unpowered') { print $enable_poe, "\n", "Your Poe IS NOT enabled, dummy\n"; } elsif($enable_poe eq 'Device ID 2, Rev. 0') { print $enable_poe, "\n", "Your PoE IS enabled, dummy\n"; } else { print "\nnothing happened\n"; }

when I run poe ds on the terminal it will either return

user@device:/tmp$ poe ds Device ID 2, Rev. 0

if the port is active or

POE device missing or unpowered

if the device is missing

Whats happening is the print statement to return the value of $enable_poe is working, but the if/else conditions are not

In this case, if the poe ds command recognizes the unit and returns

Device ID 2, Rev. 0

But the output of the script reads

Device ID 2, Rev. 0 Your Poe IS NOT enabled, dummy

so its returning the first print statement and not reading the

if ($enable_poe == 'POE device missing or unpowered')

line

What am I doing incorrect here? I want the poe ds command to be read, then use what it returns in that if/else statement

Code tags added by GrandFather

Replies are listed 'Best First'.
Re: Returning a string from executable
by Old_Gray_Bear (Bishop) on Oct 27, 2006 at 18:32 UTC
    You are looking for strict equality in your tests --
    if ($enable_poe eq 'POE device missing or unpowered') { print $enable_poe, "\n", "Your Poe IS NOT enabled, dummy\n"; }
    What happens if the string is really "POE device missing or unpowered\n"? (Note the new-line character....)

    Try

    if ($enable_poe =~ /POE device missing or unpowered/) { print $enable_poe, "\n", "Your Poe IS NOT enabled, dummy\n"; }

    ----
    I Go Back to Sleep, Now.

    OGB

      Alternatively, you could just chomp $enable_poe; before you check it (thus avoiding the need for a regex)
      EGADS! That was it, thanks!!