1: =head1 DESCRIPTION
2:
3: Hello Monks,
4: I figure the only way I'm going to get better at perl is
5: by writing code and getting comments on it, so here is a
6: module to scratch a itch I had.
7:
8: I've got lots of Mp3s. More that one CDs worth anyway.
9: So I needed something to separate them into CD sized directories.
10: So here is my latest offering.
11:
12:
13: package Mp3::Backup;
14: use strict;
15:
16: BEGIN {
17: use Exporter ();
18: use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
19: $VERSION = 0.01;
20: @ISA = qw (Exporter);
21: #Give a hoot don't pollute, do not export more than needed by default
22: @EXPORT = qw ();
23: @EXPORT_OK = qw ();
24: %EXPORT_TAGS = ();
25: }
26:
27: use File::Find;
28: use File::Copy;
29: use Data::Dumper;
30:
31:
32: sub new {
33: my $class = shift;
34:
35: my $ref = {
36: MP3DIR => shift, # The directory to backup
37: BACKUPDIR => shift, # Where to backup the mp3s
38: MAXSIZE => (shift) * 1024 * 1024, # Size in kilobyes each disk should be
39: DEBUG => shift || 0, # Set debug status;
40: FILELIST => {}, # Hash of all mp3s
41: };
42:
43: my $self = bless ($ref, ref ($class) || $class);
44:
45: return ($self);
46: }
47:
48: # The sub builds the hash of mp3s to be backed up
49: sub buildFileList {
50: my $class = shift;
51:
52: find(
53: sub {
54:
55: return unless ($File::Find::name =~ /\.mp3$/); # Only mp3s
56: $class->{FILELIST}{$_}{PATH} = $File::Find::name;
57: $class->{FILELIST}{$_}{SIZE} = -s $File::Find::name;
58: },
59: $class->{MP3DIR}
60:
61: );
62:
63: $class->_dump if ($class->{DEBUG} == 1); # Just to check what you are getting
64:
65: }
66:
67: # Private method
68: sub _dump {
69: my $class = shift;
70: print Dumper $class->{FILELIST}; # Print to STDOUT the contents of the hash
71: }
72:
73: # Build backup
74: sub fullBackup {
75: my $class = shift;
76: my $size = 0;
77: my $name = 'Disc';
78: my $count = 1;
79: my $flag = 1;
80: my $dirname = '';
81:
82: my $list = $class->{FILELIST};
83: my $maxsize = $class->{MAXSIZE};
84:
85:
86: die("This is no directory named $class->{BACKUPDIR}\n") unless -d $class->{BACKUPDIR}; # Die if no directory
87:
88: foreach ( sort keys %$list) {
89:
90: if ($flag) { # There has got to be a better way to do this that seting a flag
91: $dirname = "$class->{BACKUPDIR}/$name$count"; # Create new directory name
92: mkdir $dirname; # Create the directory
93: print 'Creating new dir: '.$dirname."\n" if $class->{DEBUG}; # Be more verbose
94: $flag = 0;
95: open LIST, ">$dirname/index.txt" || die ("Cannot open file for writing: $!"); # Create index of Mp3s
96: }
97:
98: copy ($class->{FILELIST}{$_}{PATH},$dirname); # Copy file to new directoy
99: $size += $class->{FILELIST}{$_}{SIZE}; # Add total kilobyes in directory
100: print LIST $_."\n";
101:
102: if ($size >= $maxsize) { # Set up for new directory
103: $flag = 1;
104: $count++;
105: $size = 0;
106: close LIST;
107: }
108:
109: }
110: }
111:
112: 1; #this line is important and will help the module return a true value
113: __END__
114: