1package File::Find::Object::TreeCreate; 2 3use strict; 4use warnings; 5 6use File::Spec; 7 8sub new 9{ 10 my $class = shift; 11 my $self = {}; 12 bless $self, $class; 13 $self->_initialize(@_); 14 return $self; 15} 16 17sub _initialize 18{ 19} 20 21sub get_path 22{ 23 my $self = shift; 24 my $path = shift; 25 26 my @components; 27 28 if ($path =~ s{^\./}{}) 29 { 30 push @components, File::Spec->curdir(); 31 } 32 33 my $is_dir = ($path =~ s{/$}{}); 34 push @components, split(/\//, $path); 35 if ($is_dir) 36 { 37 return File::Spec->catdir(@components); 38 } 39 else 40 { 41 return File::Spec->catfile(@components); 42 } 43} 44 45sub exist 46{ 47 my $self = shift; 48 return (-e $self->get_path(@_)); 49} 50 51sub is_file 52{ 53 my $self = shift; 54 return (-f $self->get_path(@_)); 55} 56 57sub is_dir 58{ 59 my $self = shift; 60 return (-d $self->get_path(@_)); 61} 62 63sub cat 64{ 65 my $self = shift; 66 open my $in, "<", $self->get_path(@_) or 67 return 0; 68 my $data; 69 { 70 local $/; 71 $data = <$in>; 72 } 73 close($in); 74 return $data; 75} 76 77sub ls 78{ 79 my $self = shift; 80 opendir my $dir, $self->get_path(@_) or 81 return undef; 82 my @files = 83 sort { $a cmp $b } 84 grep { !(($_ eq ".") || ($_ eq "..")) } 85 readdir($dir); 86 closedir($dir); 87 return \@files; 88} 89 90sub create_tree 91{ 92 my ($self, $unix_init_path, $tree) = @_; 93 my $real_init_path = $self->get_path($unix_init_path); 94 return $self->_real_create_tree($real_init_path, $tree); 95} 96 97sub _real_create_tree 98{ 99 my ($self, $init_path, $tree) = @_; 100 my $name = $tree->{'name'}; 101 if ($name =~ s{/$}{}) 102 { 103 my $dir_name = File::Spec->catfile($init_path, $name); 104 mkdir($dir_name); 105 if (exists($tree->{'subs'})) 106 { 107 foreach my $sub (@{$tree->{'subs'}}) 108 { 109 $self->_real_create_tree($dir_name, $sub); 110 } 111 } 112 } 113 else 114 { 115 open my $out, ">", File::Spec->catfile($init_path, $name); 116 print {$out} +(exists($tree->{'contents'}) ? $tree->{'contents'} : ""); 117 close($out); 118 } 119 return 0; 120} 1211; 122 123