Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

add double quotes to a perl variable

by casperdaghost (Initiate)
on Nov 15, 2013 at 20:21 UTC ( #1062786=perlquestion: print w/ replies, xml ) Need Help??
casperdaghost has asked for the wisdom of the Perl Monks concerning the following question:

I have a scrit that picks out any word with a "$" in it. I am attempting to add double quotes to this variable in perl

#!/usr/bin/perl use warnings; use strict; my $escaping_file = $ARGV[0]; open( my $filehandle, "<" , "$escaping_file" ) || die "failed : $!"; while (<$filehandle>) { if ($_ =~ /^\$?\w+\$+\w+\$?$/g) { s/\$/\\\$/g ; #$escaped_wquotes = "".$_."" ; #$escaped_wquotes = "\"$_\"" ; $escaped_wquotes = qq("$_") ; print "$escaped_wquotes"; } }

each time I try i get this. the two commented out varaibles give the saem result.

[casper@casperbox$ ./check_escapes /tmp/new_list "bc\$stp ""soc\$ny ""icon\$test ""btec\$stp ""ec\$stp2 ""bos\$nyc ""tec\$live5 ""\$foo\$foo\$ ""\$foo\$baz ""foo\$bad\$

i am trying to get the varaible to spit this out.

"bc\$stp" "soc\$ny" "icon\$test" "btec\$stp" "ec\$stp2" "bos\$nyc" "tec\$live5" "\$foo\$foo\$" "\$foo\$baz" "foo\$bad\$"

Comment on add double quotes to a perl variable
Select or Download Code
Re: add double quotes to a perl variable
by Kenosis (Priest) on Nov 15, 2013 at 20:33 UTC

    Add chomp:

    while (<$filehandle>) { chomp; ... print "$escaped_wquotes\n";

    (Also, consider changing || die to or die -- it's a precedence issue...)

      ok so there was a new line at the end of each interation of while. while adds a new line after each print?

        An input record separator (found in Perl's variable $/ and usually set to \n) determines the end of a line ('record') when reading from a file--like you were doing in your while loop. It's kept on the end of the line, unless you do something like chomping the line. In your case, not removing the input record separator produced undesirable results in your final output. (Sometimes, however, the very last line of a file may not terminate with an input record separator, because the line ends at the end of the file.)

Re: add double quotes to a perl variable
by oiskuu (Pilgrim) on Nov 16, 2013 at 07:02 UTC
    Note that escaping only '$' may lead to problems in a general case. For example, a string such as 'abc\$xyz' converts to 'abc\\$xyz', where the '$' remains unprotected.

    I would suggest using quotemeta(), as your strings are just labels anyway:

    #! /usr/bin/perl -l while (<>) { chomp; print '"'.quotemeta.'"' if /^[\$\w]+$/; }

    Or simply:
    #! /usr/bin/perl -ln print '"'.quotemeta.'"' if /^[\$\w]+$/;

      Consider also B::perlstring(), which not only handles escaping, but also adds the quote characters...

      #!/usr/bin/perl -l use B; while (<>) { chomp; print B::perlstring($_) if /^[\$\w]+$/; }

      The B module is bundled with Perl itself, so you already have it. The perlstring() function was added in Perl 5.8, so this will not work on Perl 5.6.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1062786]
Approved by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-04-19 23:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls