Murphy, Ged (Bolton) wrote:
In order to use the array @files in the below code outside of the
subroutine, so I need to declare @files globally?
You could but it is not really necessary.
I'm getting errors at the moment: Global symbol "@files" requires explicit
package name
That is because you have to declare it somewhere. :-)
If so, where is the correct place to declare them in terms of neatness? At
the start of the program? above the subroutine?
It is usually best to use the smallest possible scope.
#!/usr/bin/perl
use strict;
use warnings;
#use Image::Magick::Thumbnail;
# set directory to read images from
my $dir = "/ged/code/photo/vx";
# read all files from given directory
opendir DH, $dir or die "Cannot open $dir: $!";
foreach my $file (readdir DH) {
unless ($file =~ /^\./) {
push my(@files), $file;
}
}
For your simple example I would do this:
my @files = grep !/^\./, readdir DH;
Or perhaps:
my @files = grep /^[^.]/, readdir DH;
closedir DH;
#check files are in the array
print $_ foreach @files;
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>