I have a situation were I run a certain command which prints to STDOUT. I capture this STDOUT in a buffer. The problem is that if the string captured in the buffer contains any symbols special to Perl, they are getting expanded, which I don't want to happen. I want to capture the string in the buffer in a literal mode. Is there some kind of syntax I can use to achieve this? If you suggest using single quotes to me, please demonstrate how, as I cannot see how it could possible fit in the sample code. Lastly I have my reasons for not wanting to use taint mode.
use strict; sub runCLIcommand { local *STDERR; my $lineSTDERR; my $returnValue = 0; open STDERR, '>', \$lineSTDERR; # This simulates what would in reality come from a command line in +terface. # In reality I would invoke: commands::display::ssh::run(); # which in turn would print to STDOUT. print "%dsadada ssadasd\n"; # Hash symbol does not cause a problem +, which I think is inconsistent? print "@dsadada sdsadasd\n"; # Problem !!!!!!!!!!!!!!!!!!!!!!!!!!! +!!!!!!!!!! print "$dsadada sdsadasd\n"; # Problem !!!!!!!!!!!!!!!!!!!!!!!!!!! +!!!!!!!!!! # Looks for these declarations: my @dsadada; my $dsadada; # Check that there was nothing written to STDERR, which implies # everything went alright. if (length($lineSTDERR) > 0) { $returnValue = 1; } return $returnValue; } sub processCLIoutput { my $array_ref = $_[0]; my @array = @$array_ref; for (my $i = 0; $i < @array; $i++) { $array[$i] =~ s/^\s*//; $array[$i] =~ s/\s*$//; # Not a neat solution plus, doesn't work in the simulation, # since the print command itself gets there first. $array[$i] =~ s/\$/DOLLAR/g; $array[$i] =~ s/\@/ARRAY/g; $array[$i] =~ s/\%/HASH/g; } return \@array; } sub getSshDetails { local *STDOUT; my @sshDetails = (); my @capturedSTDOUT; my $lineSTDOUT; my $array_ref; open STDOUT, '>', \$lineSTDOUT || die "Could not open STDOUT. $!"; # Anything that is printed to STDOUT from now on is captured in th +e buffer # $linesSTDOUT. Is there some kind of syntax I can use here that d +isables # variable expansion of the captured output??????????????????????? +????????????????????? if (runCLIcommand() != 0) { die "CLI command did not run successfully. \n"; } @capturedSTDOUT = split(/\n/, ($lineSTDOUT)); $array_ref = processCLIoutput(\@capturedSTDOUT); @sshDetails = @$array_ref; return \@sshDetails; } ################################################################### # main: ################################################################### my $array_ref = getSshDetails(); my @array = @$array_ref; for (my $i = 0; $i < @array; $i++) { print "Array entry: ". $i. "\t". $array[$i]. "\n"; }
PS: The above will not work unless you remove the '@' and '$' from the print statements. For some reason this is not a problem for the % symbol which, I find inconsistent. Surely the same attempt at variable expansion would apply to all the primitives.

In reply to How do I disable variable expansion of strings? by seank

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.