Hello O Great and Powerful Monks,

I humbly come to you with a question from a co-worker. He came to me as the resident Perl (nearly) monk. He presented the following text code (minus use strict;, which I admonished him for). His goal was to write code that would go into several directories and get the one and only file in each directory that would satisfy the file glob. So, for example, in this code, the <test.txt> would be replaced with </home/scott/manydirs/$i/*test.txt>.

#!/usr/bin/perl -w use strict; my $EXON_IN; for (my $i=0; $i < 5; $i++){ $EXON_IN = <test.txt>; print "$i, -$EXON_IN-\n"; }
which produces this output:
[scott@blast test]$ ./test.pl 0, -test.txt- Use of uninitialized value in concatenation (.) or string at ./test.pl + line 7. 1, -- 2, -test.txt- Use of uninitialized value in concatenation (.) or string at ./test.pl + line 7. 3, -- 4, -test.txt-
Initially I said "context, context, context", as he did not think assigning a fileglob to a scalar should be any different than assigning to an array when there is only one result. I knew better on that score, but couldn't explain the above behavior. I read the relevent section of perlop2, but an explaination for this alternating behavior eludes me. My co-worker fixed this by assigning to an array, then only using the 0th element, but I would like to return with a clear reason for the behavior.

Submitted humbly for your consideration,

Scott


In reply to Fileglob in scalar context question by scain

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.