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>.
which produces this output:#!/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"; }
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.[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-
Submitted humbly for your consideration,
Scott
In reply to Fileglob in scalar context question by scain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |