My quick & dirty getfile subroutine. And a one liner too, courtesy of Merlyn (with some modifications) ... not sure which would be faster.
Added one after reading Best Practices. I don't know how fast it would be, or if it would be faster. I haven't even syntax checked this. I just wanted to get this down during a quick stop.
Minor fix to the last sample
sub getfile { my $filename = shift; open my $FH, "<$filename" or die "Unable to open $filename: $!"; return wantarray ? map /(.*)/, <$FH> : do { local $/ ; <$FH> }; }
sub getfile { local *ARGV ; @ARGV = shift ; wantarray ? map /(.*)/, <> + : do { local $/ ; <> } }
sub getfile { my $filename = shift; open my $FH, "<$filename" or die "Unable to open $filename: $!"; sysread $FH, my $contents, -s $FH; return wantarray ? split m{$/}, $contents : $contents; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: getfile( $filename )
by merlyn (Sage) on Jul 29, 2005 at 02:10 UTC | |
by harleypig (Monk) on Jul 29, 2005 at 03:46 UTC | |
by merlyn (Sage) on Jul 29, 2005 at 03:55 UTC | |
by japhy (Canon) on Jul 29, 2005 at 12:31 UTC | |
by merlyn (Sage) on Jul 29, 2005 at 16:10 UTC | |
by harleypig (Monk) on Jul 29, 2005 at 18:03 UTC | |
by greenFox (Vicar) on Aug 03, 2005 at 07:55 UTC | |
by harleypig (Monk) on Jul 29, 2005 at 07:55 UTC | |
Re: getfile( $filename )
by xdg (Monsignor) on Jul 29, 2005 at 11:22 UTC | |
by harleypig (Monk) on Jul 29, 2005 at 18:11 UTC | |
by QM (Parson) on Aug 03, 2005 at 20:25 UTC | |
by harleypig (Monk) on Aug 06, 2005 at 16:04 UTC | |
by QM (Parson) on Aug 07, 2005 at 16:12 UTC |
Back to
Cool Uses for Perl