Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by 1nickt (Canon)
on May 27, 2019 at 18:36 UTC ( [id://11100595]=note: print w/replies, xml ) Need Help??


in reply to What does $_ = qq~"$_"~ do?

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.

Replies are listed 'Best First'.
Re^2: What does $_ = qq~"$_"~ do?
by AnomalousMonk (Archbishop) on May 27, 2019 at 18:55 UTC
    ... 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:  <%-{-{-{-<

Re^2: What does $_ = qq~"$_"~ do?
by Tux (Canon) on May 27, 2019 at 20:51 UTC

    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
Re^2: What does $_ = qq~"$_"~ do?
by mkj (Initiate) on May 28, 2019 at 03:39 UTC
    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

        Fun fact: Your coworkers will curse your name unto the end of time if you . . . erm, "code" like this in anything production (especially if you do it without leaving 30 foot high neon warning signs in a comment :). (If you do it in code for personal use you might want to talk with a professional about the deep seated self loathing you're manifesting.)

        Obvious reference to relevant Jurassic Park quote . . .

        The cake is a lie.
        The cake is a lie.
        The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11100595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found