MYDan
view release on metacpan or search on metacpan
lib/MYDan/Util/File.pm view on Meta::CPAN
}
=head1 METHODS
=head3 munge( %param )
seek: 'set' or 'end'
offset: number of lines from seek position
length: number of lines to remove
regex: a regular expression
lazy: default to end of file if regex match failed
line: a set of lines
=cut
sub munge
{
my ( $self, %param ) = splice @_;
my $list = $self->{list};
my $match = $param{lazy};
my $line = $param{line} || [];
my $seek = lc( $param{seek} || 'curr' );
my $length = $param{length} || 0;
my $curr = ( $param{offset} || 0 ) +
( $seek eq 'set' ? 0 : $seek eq 'end' ? @$list : $self->{curr} );
if ( my $regex = $param{regex} )
{
$regex = eval $regex unless ref $regex;
confess 'invalid regex definition' if ref $regex ne 'Regexp';
while ( $curr < @$list )
{
last if $list->[$curr] =~ $regex && ( $match = 1 );
$curr ++;
}
}
else { $match = 1 }
if ( $match )
{
my @list = splice @$list, $curr, $length, ref $line ? @$line : $line;
$self->{curr} = @list && $curr ? $curr - @list : $curr;
}
return $self;
}
=head3 commit( %param )
path: alternative path to commit
backup: back up original file
=cut
sub commit
{
my ( $self, %param ) = splice @_;
my $path = $param{path} || $self->{path}; $path = readlink if -l $path;
my $temp = $self->{temp};
my $error = "$temp -> $path: failed to";
confess "$error chown" unless chown @$self{ qw( uid gid ) }, $temp;
confess "$error chmod" unless chmod $self->{mode}, $temp;
system "cp $path $path.bk" if $param{backup} && -e $path;
confess "$error mv" if system "mv $temp $path";
return $self;
}
sub DESTROY
{
my $self = shift;
untie @{ $self->{list} };
unlink $self->{temp} if -f $self->{temp};
}
1;
( run in 0.581 second using v1.01-cache-2.11-cpan-71847e10f99 )