Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Ben is absolutely right - pass a hash *by reference* - that is, pass a reference to a hash instead of the hash itself.

One concept that Ben didn't mention that I think is worth acknowledging is that when parameters are passed to a subroutine, that argument list is "flattened" out into a list of scalars - arrays are flattened out into a list of scalars, and hashes are flattened out into a list of scalars - the result is one big list of scalars getting passed to the subroutine. This isn't a problem unless you pass an array or hash first, and *then* other parameter(s) - the array or hash that receives the 1st argument in the subroutine will likely "swallow up" all the arguments being passed in, since it can't tell from the one big "list" of scalars coming in which ones are intended for it and which ones are intended for other parameters.

To avoid confusion when passing "things" to subroutines, pass each parameter as a scalar - this means

* passing scalars as themselves * passing a "reference to an array" (which is itself a scalar) instead of the array itself * passing a "reference to a hash" (which is itself a scalar) instead of the hash itself
Then if you really want to work with a "copy" of the array or hash (instead of just using the reference to refer to elements in the array or hash) whose reference was passed into the subroutine, you can create a copy like this:
sub do_something($my_scalar, $my_arrayref, $my_hashref) { my @my_array = @$my_arrayref; my %my_hash = %$my_hashref; ### more code here ### }

In reply to Re^2: Setting variables inside a hash: newbie question by hmerrill
in thread Setting variables inside a hash: newbie question by techgirl

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-14 07:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found