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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

As has already been explained for (LIST) aliases the iterator variable to the apropriate member of the list, just as @_ is actually a list of aliases to the passed parameters. (map{} and grep{} do the same thing as for incidentally, while/until does not.)

This is easily overlooked and can result in strange things happening by oversight. For instace replace @look_for with qw(look for) and you'll get an error. Same if you replace it with (1,2,3). (The workaround is to write for (my @temp=qw(...)). You might think these examples are obvious but they can be less so when the action is at a distance when combined with the behaviour of @_:

sub one { two(@_); } sub two { s/(.)\1/$1/ for @_ } one(qw(aa bb cc))

One interesting and subtly obvious (once you think about it) example of where this doesn't appear to happen is when iterating over the keys of hash. In this case the iterator is not aliased to the actual key but to a copy of the key.(otherwise very odd things could happen to the hash, furthermore the keys of a hash dont live in SV's so need to be placed in one before they can be accessed in Perl land)

If you really want @look_for to contain aliases to $name, $nerd etc, then you can do two things, use a reference to @_ to create an array ref of aliases, or use the module Array::RefElem

sub alias_array { \@_ } my $look_for = alias_array($name,$nerd,$noodle,$froodle,); foreach my $item (@$look_for) { $item =~ s/$i_seek/ /g; }


<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: array confusion by demerphq
in thread array confusion by Anonymous Monk

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 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? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-01-16 19:49 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (49 votes). Check out past polls.