Unable to print directory names to file using Perl
I'm trying to run this Perl script but it is not running as required. It is supposed to store the values of folders name which are in the format of date( example : 11-03-23)
I have some folders placed at this location in my account:
/hqfs/datastore/files11-02-23 11-02-17 11-04-21
I'm storing these in "processed_dirs.dat" file.
But in the output: I got "pst12345678"
in processed_dirs.dat
And when I printed $dh
, I got GLOB(0x12345)
some thing like this:
Please help me in getting the right output.
#!/usr/bin/perl
use strict;
use warnings;
use Storable;
# This script to be run 1 time only. Sets up 'processed' directories hash.
# After this script is run, ready to run the daily script.
my $dir = '/hqfs/datastore/files'; # or what ever directory the date-directories are stored in
opendir my $dh, $dir or die "Opening failed for directory $dir $!";
my @dir = grep {-d && /^\d\d-\d\d-\d\d$开发者_Go百科/ && $_ le '11-07-25'} readdir $dh;
closedir $dh or die "Unable to close $dir $!";
my %processed = map {$_ => 1} @dir;
store \%processed, 'processed_dirs.dat';
You are missing an argument for -d. Try -d "$dir/$_" && ...
. (Unless the current directory is always going to be the directory you are reading.)
There is almost no reason to ever use store instead of Storable::nstore.
Why were you trying to print dh?
$dh
is a directory handle object. There's nothing useful you can get by printing it.
The output of Storable::store
is not intended to be human-readable. If you're expecting something readable in processed_dirs.dat
, don't... you will need to use Storable::retrieve
to fetch it back out through perl, or Data::Dumper
to print out the variable in a readable format.
This implementation works and gives you accurate information.
#!/usr/bin/perl
my $dir = '/Volumes/Data/Alex/';
opendir $dh , $dir
or die "Cannot open dir: $!";
my @result = ();
foreach ( readdir $dh )
{
if ( ! /^\d{2}-\d{2}-\d{2}$/ ) { next; } else { push @result , $_; }
}
精彩评论