summaryrefslogtreecommitdiffstats
path: root/MDK
diff options
context:
space:
mode:
authorPascal Rigaux <pixel@mandriva.com>2002-12-02 20:13:57 +0000
committerPascal Rigaux <pixel@mandriva.com>2002-12-02 20:13:57 +0000
commit15698d0e3d680ce81348237b718834bd3b72ef15 (patch)
tree300fe45b50061455ffd3503ba30f16664ec61887 /MDK
parentcbfb3ba09991893db180d37e7498077fbe5d0042 (diff)
downloadperl-MDK-Common-15698d0e3d680ce81348237b718834bd3b72ef15.tar
perl-MDK-Common-15698d0e3d680ce81348237b718834bd3b72ef15.tar.gz
perl-MDK-Common-15698d0e3d680ce81348237b718834bd3b72ef15.tar.bz2
perl-MDK-Common-15698d0e3d680ce81348237b718834bd3b72ef15.tar.xz
perl-MDK-Common-15698d0e3d680ce81348237b718834bd3b72ef15.zip
add output_with_perm(), cat_or_die()
Diffstat (limited to 'MDK')
-rw-r--r--MDK/Common/File.pm16
1 files changed, 13 insertions, 3 deletions
diff --git a/MDK/Common/File.pm b/MDK/Common/File.pm
index 1bec2f5..fd15da3 100644
--- a/MDK/Common/File.pm
+++ b/MDK/Common/File.pm
@@ -25,6 +25,10 @@ array context it returns the lines.
If the file doesn't exist, it returns undef
+=item cat_or_die(FILENAME)
+
+same as C<cat_> but dies when something goes wrong
+
=item cat__(FILEHANDLE REF)
returns the file content: in scalar context it returns a single string, in
@@ -42,6 +46,10 @@ add the LIST at the end of the file
just like C<output> but creates directories if needed
+=item output_with_perm(FILENAME, PERMISSION, LIST)
+
+same as C<output_p> but sets FILENAME permission to PERMISSION (using chmod)
+
=item mkdir_p(DIRNAME)
creates the directory (make parent directories as needed)
@@ -105,16 +113,18 @@ L<MDK::Common>
use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK);
@ISA = qw(Exporter);
-@EXPORT_OK = qw(dirname basename cat_ cat__ output output_p append_to_file linkf symlinkf renamef mkdir_p rm_rf cp_af touch all glob_ substInFile expand_symlinks openFileMaybeCompressed catMaybeCompressed);
+@EXPORT_OK = qw(dirname basename cat_ cat__ output output_p output_with_perm append_to_file linkf symlinkf renamef mkdir_p rm_rf cp_af touch all glob_ substInFile expand_symlinks openFileMaybeCompressed catMaybeCompressed);
%EXPORT_TAGS = (all => [ @EXPORT_OK ]);
sub dirname { local $_ = shift; s|[^/]*/*\s*$||; s|(.)/*$|$1|; $_ || '.' }
sub basename { local $_ = shift; s|/*\s*$||; s|.*/||; $_ }
sub cat_ { local *F; open F, $_[0] or return; my @l = <F>; wantarray() ? @l : join '', @l }
+sub cat_or_die { local *F; open F, $_[0] or die "can't read file $_[0]: $!\n"; my @l = <F>; wantarray() ? @l : join '', @l }
sub cat__ { my ($f) = @_; my @l = <$f>; wantarray() ? @l : join '', @l }
-sub output { my $f = shift; local *F; open F, ">$f" or die "output in file $f failed: $!\n"; print F foreach @_ }
-sub append_to_file { my $f = shift; local *F; open F, ">>$f" or die "output in file $f failed: $!\n"; print F foreach @_ }
+sub output { my $f = shift; local *F; open F, ">$f" or die "output in file $f failed: $!\n"; print F foreach @_; 1 }
+sub append_to_file { my $f = shift; local *F; open F, ">>$f" or die "output in file $f failed: $!\n"; print F foreach @_; 1 }
sub output_p { my $f = shift; mkdir_p(dirname($f)); output($f, @_) }
+sub output_with_perm { my ($f, $perm, @l) = @_; mkdir_p(dirname($f)); output($f, @l); chmod $perm, $f }
sub linkf { unlink $_[1]; link $_[0], $_[1] }
sub symlinkf { unlink $_[1]; symlink $_[0], $_[1] }
sub renamef { unlink $_[1]; rename $_[0], $_[1] }