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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Is there a reference dereference happening?

Yes - the curlies directly around the map create a hash reference from the list that map returns (see number 3 in Making References), and then the outer %{ } dereferences that hash reference (see number 2 in Using References).

So the inner pair is a code block.

Nope, it's a hashref constructor, but since it can also look like a block, Perl sometimes has to "guess" which one it is, and AFAIK that's where the warning is coming from.

If I need to return an array, as opposed to a list or a hash, what would that syntax be?

The same kind of reference-dereference operation, just in this case create an arrayref with [...] and dereference it with @{...}:

print Dumper @{ [ map {"<$_>"} qw/a b c/ ] }; __END__ $VAR1 = '<a>'; $VAR2 = '<b>'; $VAR3 = '<c>';

Although I think this is needed less often than the %{{...}} trick when it comes to passing stuff to functions, since most functions impose list context on their arguments anyway. However, it is sometimes used as a trick to interpolate things into strings that don't normally interpolate, so for example:

sub foo { return "World!" } print "Hello, ", foo(), "\n"; # prints "Hello, World!" print "Hello, foo()!\n"; # prints "Hello, foo()!" print "Hello, @{[ foo() ]}\n"; # prints "Hello, World!"

While in this example the first print looks cleaner than the last, the trick can sometimes be useful when interpolating things into long heredocs, for example.


In reply to Re: Syntax for casting map to a hash or an array by haukex
in thread Syntax for casting map to a hash or an array by cbeckley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found