R
Robbie Hatley
Greetings, group. This is the first time I've posted here,
or in any Perl-related forum for that matter. I'm a rank
beginner at Perl. I'm trying to teaching myself the language
from reading books (incl. one with a camel on the cover, and
one with a llama; not that anyone here would recongize those,
of course), trying my own Perl hacks, and struggling
with compiler errror messages.
The training program I'm currently trying to write is a
duplicate-file finding/removing program. A rather elaborate
program when written in C++. An engineer friend told me it
would be much simpler in Perl. I had to tell him, "Cool, but
I don't know Perl". He seemed offended and said "Your loss.".
So... pasted below is my first (incomplete) attempt at writing
a real Perl program.
I have hundreds of questions about Perl, but for now I'll
ask this group just two questions:
1:
How do I get a more-detailed directory listing than is offered
by the readdir function? Is their any way to goad that
function into coughing-up file-type (file or directory or link),
size in bytes, mod-time, mod-date, attribtutes, etc.? Or do I
have to use some other approach to get that data?
2:
Is there a better way to emulate the C++ concept of a "list of
structs" than what I'm doing below? (I'm using an array of refs
to hashes.)
Here's my program at it's current state of development:
################################################################################
##########
# dedup3.perl
# Duplicate file finding/erasing program.
# Written by Robbie Hatley, as a "learn Perl" excercise.
# Plan: Recursively decend directory tree starting from current working
directory,
# and make a master list of all files encountered on this branch. Order the
list by size.
# Within each size group, compare each file, from left to right, to all the
files to
# its right. If a duplicate pair is found, alert user and get user input. Give
user
# these choices:
# 1. Erase left file
# 2. Erase right file
# 3. Ignore this pair of duplicate files and move to next
# 4. Quit
# If user elects to delete a file, delete it, then move to next duplicate file
pair.
################################################################################
##########
use strict;
use warnings;
use Cwd;
# Not valid Perl; how do I do this???
# struct FileRecord
# {
# std::string Date;
# std::string Time;
# std::string Type;
# long int Size;
# std::string Attr;
# std::string Name;
# };
#
# std::list<rhdir::FileRecord> FileList;
#
# TODO: How do I extract size, mod-time, mod-date, type (file or dir),
# attributes, etc. and store in an array of structs (or Perl equiv)???
#
# Try an array of hashes?
my $CurDir = getcwd();
print "CWD = ", $CurDir, "\n";
opendir(Dot, ".") or die "Can\'t open the directory!!!";
my @LocalFiles;
my $FileName;
my $FileRecord;
while ($FileName=readdir(Dot))
{
$FileRecord=
{
"Date" => "Unknown",
"Time" => "Unknown",
"Type" => "Unknown",
"Size" => 42,
"Attr" => "Unknown",
"Name" => $FileName
};
push @LocalFiles, $FileRecord;
}
closedir(Dot);
foreach $FileRecord (@LocalFiles)
{
print($$FileRecord{"Name"}, "\n");
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
or in any Perl-related forum for that matter. I'm a rank
beginner at Perl. I'm trying to teaching myself the language
from reading books (incl. one with a camel on the cover, and
one with a llama; not that anyone here would recongize those,
of course), trying my own Perl hacks, and struggling
with compiler errror messages.
The training program I'm currently trying to write is a
duplicate-file finding/removing program. A rather elaborate
program when written in C++. An engineer friend told me it
would be much simpler in Perl. I had to tell him, "Cool, but
I don't know Perl". He seemed offended and said "Your loss.".
So... pasted below is my first (incomplete) attempt at writing
a real Perl program.
I have hundreds of questions about Perl, but for now I'll
ask this group just two questions:
1:
How do I get a more-detailed directory listing than is offered
by the readdir function? Is their any way to goad that
function into coughing-up file-type (file or directory or link),
size in bytes, mod-time, mod-date, attribtutes, etc.? Or do I
have to use some other approach to get that data?
2:
Is there a better way to emulate the C++ concept of a "list of
structs" than what I'm doing below? (I'm using an array of refs
to hashes.)
Here's my program at it's current state of development:
################################################################################
##########
# dedup3.perl
# Duplicate file finding/erasing program.
# Written by Robbie Hatley, as a "learn Perl" excercise.
# Plan: Recursively decend directory tree starting from current working
directory,
# and make a master list of all files encountered on this branch. Order the
list by size.
# Within each size group, compare each file, from left to right, to all the
files to
# its right. If a duplicate pair is found, alert user and get user input. Give
user
# these choices:
# 1. Erase left file
# 2. Erase right file
# 3. Ignore this pair of duplicate files and move to next
# 4. Quit
# If user elects to delete a file, delete it, then move to next duplicate file
pair.
################################################################################
##########
use strict;
use warnings;
use Cwd;
# Not valid Perl; how do I do this???
# struct FileRecord
# {
# std::string Date;
# std::string Time;
# std::string Type;
# long int Size;
# std::string Attr;
# std::string Name;
# };
#
# std::list<rhdir::FileRecord> FileList;
#
# TODO: How do I extract size, mod-time, mod-date, type (file or dir),
# attributes, etc. and store in an array of structs (or Perl equiv)???
#
# Try an array of hashes?
my $CurDir = getcwd();
print "CWD = ", $CurDir, "\n";
opendir(Dot, ".") or die "Can\'t open the directory!!!";
my @LocalFiles;
my $FileName;
my $FileRecord;
while ($FileName=readdir(Dot))
{
$FileRecord=
{
"Date" => "Unknown",
"Time" => "Unknown",
"Type" => "Unknown",
"Size" => 42,
"Attr" => "Unknown",
"Name" => $FileName
};
push @LocalFiles, $FileRecord;
}
closedir(Dot);
foreach $FileRecord (@LocalFiles)
{
print($$FileRecord{"Name"}, "\n");
}
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant