Vladimir Lemberg wrote:
Hi,
Hello,
I have a problem with assigning value to string. I'll be really grateful if
someone will help me.
use strict;
use warnings;
use Cwd;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;
####code fragment####
foreach (@dirs){
chdir $_;
my $curdir = cwd();
my $vsn = glob ("*.vsn");
my $ver = glob ("*.ver");
print BLUE "\n[$_]\n";
opendir(DIR, "$curdir") or die RED "Unable to open $curdir: $!";
while (defined (my $file = readdir DIR)) {
next if -d "$curdir/$file" or $file eq "zone.cfg" or $file eq $vsn or $file eq $ver;
#compressing
system($program, $ARGV[0], $ARGV[1], $file) if $ARGV[0] eq "-c";
#decompressing
system($program, $ARGV[0], $file) if $ARGV[0] eq "-d";
}
closedir(DIR);
chdir $cwd;
}
The problem is that on each even iteration of loop, strings my $vsn and $ver
cannot be initialized.
perldoc perlop
[snip]
A (file)glob evaluates its (embedded) argument only when it is starting
a new list. All values must be read before it will start over. In
list context, this isn't important because you automatically get them
all anyway. However, in scalar context the operator returns the next
value each time it's called, or "undef" when the list has run out. As
with filehandle reads, an automatic "defined" is generated when the
glob occurs in the test part of a "while", because legal glob returns
(e.g. a file called 0) would otherwise terminate the loop. Again,
"undef" is returned only once. So if you're expecting a single value
from a glob, it is much better to say
($file) = <blurch*>;
than
$file = <blurch*>;
because the latter will alternate between returning a filename and
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
returning false.
^^^^^^^^^^^^^^^
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>