Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Perl Idioms Explained - ${\$obj->method} and @{[sort @list]}

use constant MATCHES => join '|' => qw/ a list of words /; sub method { return "something complex here" } my $obj = bless []; my %hash = qw/ this is a hash /; print "it's a hash - @{[ %hash ]}\n"; print "method result - ${ \$obj->method }\n"; print "a regexp object - ", qr[\b (?: ${\MATCHES} ) \b]x, $/; __output__ it's a hash - this is a hash method result - something complex here a regexp object - (?x-ism:\b (?: a|list|of|words ) \b)
At times you'll want to add something more complex to an interpolated quote-like than a simple variable, but the option of simply concatenating isn't always available (i.e here docs, qr//), or perhaps, desired. So what one can do is take advantage of the perl's ${} and @{} syntax. Given the above examples I'll explain how this can be done.
print "it's a hash - @{[ %hash ]}\n";
The notable part of the above statement is @{[ %hash ]}. Here we're creating an anonymous array containing the elements of the aptly named hash %hash. Now that we have an array reference the @{} will dereference it and the resulting array will be interpolated into the string. Simple as that!
print "method result - ${ \$obj->method }\n";
As you can probably guess what we're interested in is ${ \$obj->method }. Firstly, we call the method on an object in the standard fashion - $obj->method, then we create a reference to what has just been returned and finally we dereference the newly created reference and the resulting scalar is interpolated.
print "a regexp object - ", qr[\b (?: ${\MATCHES} ) \b]x, $/;
While the above may look more complex it's just the same as the previous example, but surrounded by a regexp string constructor. In this case we're looking at ${\MATCHES} which calls the constant MATCHES, this returns a string which we create a reference to, then promptly dereference and interpolate into the string, and just like that we have a constant in a regex!

Caveats

One must be careful with the ${\foo} because if foo returns something that isn't a simple scalar value then you might get unexpected results. Also keep in mind that this is different from passing in a simple bareword which is largely used to distinguish between variable names and the surrounding text.

Summary

A shortcut for interpolating the return of complex expressions into a string.

_________
broquaint


In reply to Perl Idioms Explained - ${\$obj->method} and @{[sort @list]} by broquaint

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2015-07-03 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (47 votes), past polls