diff options
author | Bas Zoetekouw <bas> | 2008-09-30 19:30:03 +0000 |
---|---|---|
committer | Bas Zoetekouw <bas> | 2008-09-30 19:30:03 +0000 |
commit | 324421a41d575e97d2e7b2f51297e19771e9e4ef (patch) | |
tree | 390f6a399452a3e7ffd0b07b6c7b99948964ffea /Perl/Local | |
parent | b8bbe054324844ae6cdfc1fcd64dc66bc7a1326c (diff) |
Move read_file() to Local::Util.
CVS version numbers
check_trans.pl: 1.71 -> 1.72
Perl/Local/Util.pm: 1.1 -> 1.2
Diffstat (limited to 'Perl/Local')
-rw-r--r-- | Perl/Local/Util.pm | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/Perl/Local/Util.pm b/Perl/Local/Util.pm index 87501fb6383..137764b6b0d 100644 --- a/Perl/Local/Util.pm +++ b/Perl/Local/Util.pm @@ -22,6 +22,7 @@ scripts, and for which we don't want to add dependencies on external modules. package Local::Util; use 5.008; +use Carp; use strict; use warnings; @@ -30,7 +31,7 @@ BEGIN { use base qw( Exporter ); our $VERSION = sprintf "%d", q$Revision$ =~ /(\d+)/g; - our @EXPORT_OK = qw( uniq ); + our @EXPORT_OK = qw( uniq read_file ); our %EXPORT_TAGS = ( 'all' => [@EXPORT_OK] ); } @@ -61,6 +62,31 @@ sub uniq (@) map { $h{$_}++ == 0 ? $_ : () } @_; } +=item read_file + +Slurps an entire file and returns the contents of the file as a scalar, or +undef if an error occured (actual error will be in $!). + +This function is mean as a light-weight replacement for File::Slurp. + +=cut +sub read_file +{ + my $filename = shift + or croak("Local::Util::read_file: no file specified"); + + # slurp mode + local $/ = undef; + + # read the file + open( my $fd, '<', $filename ) or return; + my $text = <$fd>; + close( $fd ); + + return $text; +} + + 42; __END__ |