Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

What does $_ = qq~"$_"~ do?

by mkj (Initiate)
on May 27, 2019 at 18:19 UTC ( #11100593=perlquestion: print w/replies, xml ) Need Help??

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

Just looking for an explanation of what the line $_ = qq~"$_"~; in KevinADC's response from 2007 at www.tek-tips.com/viewthread.cfm?qid=1381252 does. Thanks!

His full example script:

my $line = '1|firstname|last, name|address1|city|state|zip'; my @stuff = split(/\|/,$line); for (@stuff) { if (/,/) { $_ = qq~"$_"~; } } my $csv = join ',' ,@stuff; print $csv;

Replies are listed 'Best First'.
Re: What does $_ = qq~"$_"~ do?
by Paladin (Vicar) on May 27, 2019 at 18:27 UTC
    qq is the generic double quoting operator. It's "" with using different delimiters. So the line $_ = qq~"$_"~; encloses whatever text is in $_ with double quotes. It's the same as $_ = "\"$_\""; without having to escape the " characters.
      I was completely overthinking it - thanks for clarifying.

        I was completely overthinking it - thanks for clarifying.

        Hi,

        There is a list for overthinking avoidance :)

        Basic debugging checklist

        perl -MO=Deparse - $_ = qq~"$_"~; __END__ $_ = qq["$_"];

        ppi_dumper is also great at hints

        $ ppi_dumper input_file PPI::Document PPI::Statement PPI::Token::Magic '$_' PPI::Token::Whitespace ' ' PPI::Token::Operator '=' PPI::Token::Whitespace ' ' PPI::Token::Quote::Interpolate 'qq~"$_"~' PPI::Token::Structure ';'
Re: What does $_ = qq~"$_"~ do?
by 1nickt (Abbot) on May 27, 2019 at 18:36 UTC

    Hi,

    You can try it!

    $ perl -Mstrict -wE '$_ = q{foo}; $_ = qq~"$_"~; say $_' "foo"
    It's forcing the string to include leading and trailing double quotations marks as part of the string. The use of the tilde character to define the start and end of what's passed to qq) is just a style choice and doesn't affect the code. (Also, the use of qq in the code you posted is unnecessary: $_ = '"$_"'; would have done the same thing. See also Quotes and Quote-Like Operators)

    In this case, the code is doing so if the string contains a comma, so it is undoubtedly a misguided attempt to manually escape values in a delimited file. You should never attempt to do that yourself, as it's more complicated that it might seem. Instead, use a module designed for handling delimited files, such as Text::CSV.

    Hope this helps!


    The way forward always starts with a minimal test.
      ... the use of qq in the code you posted is unnecessary: $_ = '"$_"'; would have done the same thing.

      Not so. The use of non-interpolating  '...' quotes would produce only a literal  "$_" output:

      c:\@Work\Perl\monks>perl -wMstrict -le "for (qw(help! am trapped in noninterpolating quotes!)) { printf '\"$_\" '; } " "$_" "$_" "$_" "$_" "$_" "$_"
      See also Quote and Quote-like Operators in perlop for a link to discussion of  qq// that's closer to the mark.

      Update:

      ... a misguided attempt to manually escape values in a delimited file. ... it's more complicated that it might seem. ... use a module designed for handling delimited files, such as Text::CSV.
      Amen to that!


      Give a man a fish:  <%-{-{-{-<

      Simple examples of where this code fails:

      my $line = '1|firstname|last, name"|address1|city|state|zip'; my $line = '1|firstname|last, name|address1|city|"state,"|zip'; my $line = '1|firstname|last, name|,",address1|city|state|zip'; my $line = '1|firstname|,"|last, name|address1|city|state|zip'; my $line = "1|firstname|last, name|,\n"address1"|city|state|zip";

      and so on.


      Enjoy, Have FUN! H.Merijn
      Thanks for the example one-liner. I'm familiar with qq, but for some reason I wasn't associating the tilde with qq and was overthinking it - thought it was some black magic shorthand, but no.

        Fun fact: the delimiters can even be word characters, provided there's whitespace after the qq:

        $ perl -wMstrict -le 'print qq queueq' ueue $ perl -wMstrict -le 'print qq 01230' 123

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2019-11-14 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (76 votes). Check out past polls.

    Notices?