I saw something in "Re: Re: Re: I don't use printf enough" that didn't make sense to me.

While I understand the @{[....]} and ${\....} constructs, I don't understand ${\~~....}.

Consider the following snippet:

use strict; use warnings; sub context { print wantarray ? "List\n" : "Scalar\n"; } my $string; $string = "Test @{[context()]} string"; $string = "Test ${\context()} string"; $string = "Test ${\~~context()} string"; __OUTPUT__ List List Scalar

I've seen the first two constructs before, but the third is a new one to me. How is ~~ forcing scalar context?

I've looked in perlop and find no mention of ~~. A single ~ is the bitwise negation operator. But I don't think that has anything to do with ~~. The only place I find mention of '~~' is under perlform, where it's mentioned as a "repeat the previous format character until the line is filled" format operator. But again that doesn't seem to make sense in the context of interpolating code into a string.

Of course all three of the above constructs are not terribly pretty, and should probably be avoided when it's convenient to do so. But that doesn't stop my wondering. If anyone can shed some light on what ~~ is doing, I'd love to hear.


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein

In reply to How does ~~ force string interpolation of code into scalar context? by davido

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.