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

Good morning, fellow Monks. I am working on a script where I am seeing totally unexpected behavior. I am hoping one of you fine folks can get me back on the right path with this issue...

On my *nix server, I can run the following command to extract the first part of the hostname:

ABCXYZ123-0-0-1:/home/dir-> hostname | sed 's/-.*?//g' ABCXYZ123

So I translated that into my Perl script. I am then running another command to create a couple of files, then checking the output of the command to ensure they were created with no issues. Well, my regex was failing (even though I know it was working properly) so I sucked out all the appropriate lines from my big script and put them into a test script (server names and some info changed to protect company data):

#!C:/Perl64/bin/perl.exe use strict; use warnings; use Control::CLI; my $cli; my $output; my $temp; my $junk; #my $host = "1.11.1.11"; my $host = "1.12.1.12"; $cli = new Control::CLI( Use => 'SSH', Timeout => 180, ); $cli->connect( Host => $host, Username => 'user', Password => 'pass', ); $output = $cli->cmd("clear"); $output = ""; my $hostname = $cli->cmd("hostname | sed \'s/-.*//g\'"); print "hostname: $hostname"; $junk = $cli->print("su -c \"/some/dir/path/here/command.py -o /dir\"" +); $output = $cli->cmd("rootpwd"); print "\n$output\n"; #if ( $output =~ m/.*$hostname.ext.*/s && $output =~ m/.*name.ext.*/s +) { if ( $output =~ m/$hostname/s ) { print "\t$hostname.ext and name.ext created successfully on server + in /spdata...\n"; } else { print "\tThere was a problem generating $hostname.ext and name.ext + on server in /spdata... Please investigate and start application aga +in... Exiting program.\n"; exit; } $cli->disconnect;

When I run this script stub, I get the following on my command prompt of my laptop:

C:\Users\ImJustAFriend\Documents\Perl\Test>CLIControlTest.pl hostname: ABCXYZ123 Password: Last login: Mon Jan 13 13:26:40 UTC 2014 from blah on ssh /spdata/ABCXYZ123.ext is generated /spdata/name.ext is generated There was a problem generating ABCXYZ123 .ext and name.ext on server in /spdata... Please investigate and start + application again... Exiting program. C:\Users\ImJustAFriend\Documents\Perl\Test>

So I noticed the new line between "ABCXYZ123" and ".ext" in my output. Thinking this was the issue, I put in a "chomp($hostname);" line in my script, right after the "my $hostname=" line. When I ran it again, I got this output:

C:\Users\ImJustAFriend\Documents\Perl\Test>CLIControlTest.pl hostname: ABCXYZ123 Password: Last login: Mon Jan 13 13:26:40 UTC 2014 from blah on ssh /spdata/ABCXYZ123.ext is generated /spdata/name.ext is generated .ext and name.ext on server in /spdata... Please investigate and start + application again... Exiting program. C:\Users\ImJustAFriend\Documents\Perl\Test>

So putting the "chomp" in killed the whole beginning of my line. I have spent quite a few cycles on this, and still don't know what's going on. Can anyone provide some insight as to what the issue is here?

Thanks in advance!!

Replies are listed 'Best First'.
Re: Unexpected Behavior - Variables and Regex
by tobyink (Canon) on Jan 13, 2014 at 14:16 UTC

    Firstly, make sure that you're not doing something like:

    $foo = chomp($foo); # remove trailing line

    That doesn't work. chomp modifies $foo in place, and then returns a fairly useless value, so you don't want to then assign that to $foo.

    If that's not the case, then at a guess, $hostname might end in "\r\n" (a Windows-style line ending); perhaps because SSH knows you're connecting from a Windows host. In this case, chomp may be removing the "\n", but not the "\r". Outputting an "\r" results in the cursor returning to the start of the line; subsequent output will then overwrite your old output.

    Perhaps instead of chomp($hostname) try $hostname =~ s(\s)()g which will remove all whitespace from the host name.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: Unexpected Behavior - Variables and Regex
by ImJustAFriend (Scribe) on Jan 13, 2014 at 14:26 UTC

    OK, I did some googling for "Perl remove newline" and came across a technique that worked where chomp failed. Instead of "chomp($hostname)", I used this regex:

    $hostname =~ s/\R//g;

    Now when I run my code, the regex match and the print output all works as expected. I hope this can help someone else in the future!!

    ImJustAFriend

      Just some additional information:

      -- Ken