You are having problems with cryptocontext. glob behaves differently according to whether you use it in scalar or list context.

Your code as it stands calls glob in scalar context ($p = ...). This primes glob to set up an iterator that may return a different filename each time you call it, for filenames that match the pattern, until no more do, at which time it returns undef.

The second time you call glob with '002', you're getting the undef back that signals that nothing else matches the glob set up for '001'. By that time, the glob has been exhausted, so when you call it the next time with '003', you get something back. And so forth.

You can change your one-liner to get an array back, and just use the first element returned:

perl -pale 'print $F[0]; @p=<test/*_$F[0].tst>; s/^/$p[0] /' test.list

You may wish to iterate over @p in a loop, and deal with all that match (I cannot know if this is an issue for you or not).

Otherwise, if you only care about ever dealing with one entry, you can force a list assignment with parentheses:

perl -pale 'print $F[0]; ($p) = <test/*_$F[0].tst>; s/^/$p /' test.lis +t

update: oh bleagh, while I was testing all this out, you found the right answer anyway. So shoot me. Oh well, I hope someone finds this instructive.

• another intruder with the mooring in the heart of the Perl


In reply to Re: Problem with one-liner using file globs by grinder
in thread Problem with one-liner using file globs by graff

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.