Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To expound on demerphq's response with an example:

Given a hash, like so:

my %test; $test{a} = 1; $test{b} = 2;
When you call a subroutine with an array or hash, it gets flattened into a list. For a hash, that means giving all of its keys and corresponding values. So this:
something(%test);
is the same as this:
something(a, 1, b, 2);
(Note that when hashes are flattened, the keys may come out in any order, so it could have also been something(b, 2, a, 1);.)

The other side of this situation is that you can fill a hash directly with a list, instead of doing each key one-at-a-time. When you assign to a hash with a list, each odd item becomes a key, and the corresponding even item is its value. So we could have filled the hash like this:

%test = (a, 1, b, 2);
and it would be exactly the same. Note that this is also the same as the more familiar:
%test = (a => 1, b => 2);
as the => is just a type of comma that auto-quotes its left side operand. Now you can see what happens:
something(%test); # is like something(a, 1, b, 2);
And inside the sub:
my %hash = @_; # is like %hash = (a, 1, b, 2);
And so it turns out that you're just doing a basic copy. The hash inside the sub will be a copy of the one you passed in.


In reply to Re: Re: Perl Internals: Hashes by kelan
in thread Perl Internals: Hashes by Kozz

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 imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found