for i in `ls ${image_path}*.jpg`
I wonder what the thinking is that gets people into this twisted level of code. Maybe it's just seeing it somewhere else, then cargoculting it without thinking.

So, given that this is likely the case, let me say, Don't do it this way.

Here's what you're asking your poor system to do:

  1. Shell, please fork
  2. Child shell, figure out what matches ${image_path}*.jpg
  3. Child shell, now pass that list to an ls that you forked
  4. ls, please go look up this list of names that I've already handed you. (And, by the way, if any of those are directories, expand them, even though that isn't what I want, so it'll be a bug I have to fix later.) And spit those names out one by one, with a newline.
  5. (Parent) shell, take the output of that child process, and create elements of a list, newline delimited (which breaks if any of the names contain whitespace such as newline, but that's another bug to be fixed later).
  6. Shell, start looping through that list, doing the following...
Now, let's see how we could have written it, properly:
for i in ${image_path}*.jpg
Yeah, that's it. Simple. Now look what we told the system to do:
  1. Shell, figure out what matches ${image_path}*.jpg
  2. Shell, start looping through that list, doing the following...
Not only is it faster and shorter to type, it's now also whitespace safe, newline safe, and directory safe!

So, why does the other form get cargo-culted so much? It's like the useless use of cat that I keep seeing all the time. My guess is that it's like that in a bad book or a bad FAQ, and then it gets copied and pasted from one person to the next, like a magical formula that nobody understands and yet repeats, as if the magic words will open up the door if uttered in the proper moonlight.

Just curious, I guess. Remember, this isn't personal... I'm just trying to figure out your thinking so that I can work out a counter-meme to it to help everyone at once, instead of one at a time like I keep ending up doing. Call me lazy. {grin}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


In reply to •Re: Time for Some Movie Magick by merlyn
in thread Time for Some Movie Magick by abitkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.