scain has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Fileglob in scalar context question
by japhy (Canon) on Jun 30, 2001 at 00:23 UTC | |
|
Re: Fileglob in scalar context question
by John M. Dlugosz (Monsignor) on Jun 30, 2001 at 00:40 UTC | |
by tye (Sage) on Jun 30, 2001 at 00:49 UTC | |
by John M. Dlugosz (Monsignor) on Jun 30, 2001 at 02:50 UTC | |
by tye (Sage) on Jun 30, 2001 at 08:23 UTC | |
by John M. Dlugosz (Monsignor) on Jul 01, 2001 at 05:59 UTC | |
by scain (Curate) on Jun 30, 2001 at 01:04 UTC |