Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: writtings files to a new folder

by MaxKlokan (Monk)
on Jan 24, 2007 at 09:40 UTC ( #596225=note: print w/replies, xml ) Need Help??

in reply to writtings files to a new folder

For a beginner my solution is perhaps too cryptic, but I am going to give it anyway, just in case somebody finds it interesting.
You can do it all with a single command if you use grep, which can be used to apply a block of statements to all elements in a list :
use File::Copy; grep { (move("old/$_", "new/.") or warn "$_ => $!") if -f } @files;
The default variable $_ is set to each element of the list and the operator -f can work with it.
$! is the error message if an error has indeed occurred.

Replies are listed 'Best First'.
Re^2: writtings files to a new folder
by jbert (Priest) on Jan 24, 2007 at 10:56 UTC
    If you're going to use dense loop constructs like this, 'map' is a slightly better choice than grep for expressing your intentions. Or probably even better:
    do { some; series of; commands; which can use $_; } foreach @files;
    But this is only if you have an aversion to newlines (which I can relate to), since this clearly the same as:
    foreach (@files) { some; series of; commands; which can use $_; }
      Thinking of it, I myself would probably use do {...} foreach @list; as you suggest, because I find it more readable.
      At first I found that particular use of grep intriguing. Then again, I wonder whether it was a good idea to add such a (potentially confusing) functionality to grep, since foreach works perfectly fine.
        In general, you shouldn't use grep or map for side-effects.

        The purpose of grep is to filter a list into another (possibly shorter) list of elements of the original list. The grep block should really do nothing except evaluate to true or false.

        The purpose of map is to transform a list into another list of the same length1, with the transformation being specified by the return value of the map block.

        The purpose of 'for/foreach' is to loop over a list and perform an action (perhaps with side-effects such as printing) for each element.

        All of these involve looping over the list and, since arbitrary code can be used in the grep or map block, all can be considered as variants on 'for', but with different return values. If you aren't using the return value, you should use for.

        All of the 'should' and 'purpose' above relate to communicating your intent to other coders, of course the code would work whichever construct you used for your loop.

        1OK, because perl flattens lists, you can change the number of list items by having your block evaluate to a list itself, which is useful to construct a hash from a list of keys, for example.


        Any way you can make the code more legible (maintainable) is a good thing. I'm not saying that the grep usage is a bad thing, as I don't know enough Perl idioms to recognize whether it is or not. But the do { ... } foreach @list; is pretty darned easy to read and recognize what the code does.

        Someone (Kernighan?) once said something to the effect of: Debugging is harder than coding. If you write the cleverest code you can, then by definition, you can't debug it.

        If only I could learn to live by that motto ... my life would be a lot simpler.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://596225]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2020-10-29 04:02 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (268 votes). Check out past polls.