=head1 DESCRIPTION Hello Monks, I figure the only way I'm going to get better at perl is by writing code and getting comments on it, so here is a module to scratch a itch I had. I've got lots of Mp3s. More that one CDs worth anyway. So I needed something to separate them into CD sized directories. So here is my latest offering. package Mp3::Backup; use strict; BEGIN { use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.01; @ISA = qw (Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw (); @EXPORT_OK = qw (); %EXPORT_TAGS = (); } use File::Find; use File::Copy; use Data::Dumper; sub new { my $class = shift; my $ref = { MP3DIR => shift, # The directory to backup BACKUPDIR => shift, # Where to backup the mp3s MAXSIZE => (shift) * 1024 * 1024, # Size in kilobyes each disk should be DEBUG => shift || 0, # Set debug status; FILELIST => {}, # Hash of all mp3s }; my $self = bless ($ref, ref ($class) || $class); return ($self); } # The sub builds the hash of mp3s to be backed up sub buildFileList { my $class = shift; find( sub { return unless ($File::Find::name =~ /\.mp3$/); # Only mp3s $class->{FILELIST}{$_}{PATH} = $File::Find::name; $class->{FILELIST}{$_}{SIZE} = -s $File::Find::name; }, $class->{MP3DIR} ); $class->_dump if ($class->{DEBUG} == 1); # Just to check what you are getting } # Private method sub _dump { my $class = shift; print Dumper $class->{FILELIST}; # Print to STDOUT the contents of the hash } # Build backup sub fullBackup { my $class = shift; my $size = 0; my $name = 'Disc'; my $count = 1; my $flag = 1; my $dirname = ''; my $list = $class->{FILELIST}; my $maxsize = $class->{MAXSIZE}; die("This is no directory named $class->{BACKUPDIR}\n") unless -d $class->{BACKUPDIR}; # Die if no directory foreach ( sort keys %$list) { if ($flag) { # There has got to be a better way to do this that seting a flag $dirname = "$class->{BACKUPDIR}/$name$count"; # Create new directory name mkdir $dirname; # Create the directory print 'Creating new dir: '.$dirname."\n" if $class->{DEBUG}; # Be more verbose $flag = 0; open LIST, ">$dirname/index.txt" || die ("Cannot open file for writing: $!"); # Create index of Mp3s } copy ($class->{FILELIST}{$_}{PATH},$dirname); # Copy file to new directoy $size += $class->{FILELIST}{$_}{SIZE}; # Add total kilobyes in directory print LIST $_."\n"; if ($size >= $maxsize) { # Set up for new directory $flag = 1; $count++; $size = 0; close LIST; } } } 1; #this line is important and will help the module return a true value __END__