Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

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

I'm new to Perl myself, so this explanation might not only be insufficient but also wrong. That mandatory warning aside..

Map takes a list and returns a list. The best way I've found to understand map, is to remember that you have to read the list argument backwards; that is, the list it modifies goes at the end, and the list you get, at the start. In this case, both lists are @ARGV, which might complicate your understanding somewhat. Basically, it takes each element in @ARGV, transforms them with whatever it's in the block (in this case), and returns the transformed elements as a new list to be set in @ARGV again.

So, let's take a look inside the block. I've never used File::DosGlob, but from skimming over the documentation, I assume it ports the glob function from *nix to a DOS shell/cmd. So we are using it to expand the wildcard, returning a list of elements that match.

Then,

my @g = File::DosGlob::glob($_) if /[*?]/;

Checks whenever $_ has a wildcard in it, and if it does, calls the DosGlob glob function, which returns a list of files to be set in @g.

The last line of the code tells the block which values send over to the new list being generated by map, like in your usual function. Here it's a ternary operator; if @g is true (has nonzero elements), evaluates @g, and thus sends that to map. If @g is false (there were no wildcards in $_), it evaluates whatever is after the :, which is $_, and sends that to the map.

Here's hoping this helps/I didn't massively screw up.


In reply to Re^3: Perl Rename by Hugmeir
in thread Perl Rename by Mad_Mac

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 examining the Monastery: (8)
As of 2024-03-28 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found