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!!


In reply to Unexpected Behavior - Variables and Regex by ImJustAFriend

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.