develooper Front page | perl.perl5.porters | Postings from October 2003

Re: new slurp module

Thread Previous | Thread Next
Uri Guttman
October 23, 2003 12:05
Re: new slurp module
Message ID:
>>>>> "A" == Abigail  <> writes:

  A> Running test with 1 bytes
  A>            Rate     cat   slurp      do    open sysread
  A> cat       745/s      --    -97%    -98%    -98%    -99%
  A> slurp   24139/s   3138%      --    -32%    -32%    -62%
  A> do      35275/s   4632%     46%      --     -1%    -45%
  A> open    35759/s   4697%     48%      1%      --    -44%
  A> sysread 64056/s   8493%    165%     82%     79%      --

  A>     cmpthese -10 => {
  A>         slurp   =>  '$::r1 = read_file $::file;',
  A>         do      =>  '$::r2 = do {local (@ARGV, $/) = $::file; <>};',
  A>         open    =>  'open my $fh => $::file or die; undef $/; $::r3 = <$fh>;',
  A>         sysread =>  'open my $fh => $::file or die;
  A>                      sysread $fh => $::r4, -s $::file;',
  A>         cat     =>  '$::r5 = `cat $::file`;',
  A>     };

  A> Frankly, I don't see much reason for a File::Slurp addition to the
  A> core. The current idioms to slurp in a whole file are small (in chars),
  A> and faster than File::Slurp. Granted, File::Slurp has a write_file
  A> method, but I think reading entire files at once is much more common
  A> than writing them.

you trade off some speed for simpler api.

run the script in extras/. it does most if not all of the
tests you run as well as other variant calls. the default api is slower
than the common idioms but the passing the data via 'buf_ref' is the
fastest. so you need to use the options to optimize the call for
speed. it would be easy to write a wrapper with a simple api that always
did the 'buf_ref' style call (see the append_file() wrapper).

  A> You don't have to remember multiple idioms, one is enough. As you can
  A> see, the current idioms are short enough to be not significant harder
  A> to remember than 'use File::Slurp; $text = read_file $file'. Unless you
  A> mean you have to remember multiple idioms because you are a maintainer
  A> and you have to maintain code written by different people, using different
  A> idioms. In that case, the last thing you want is to have to remember yet
  A> another idiom (the current idioms don't go away).

and how many newbies never figure out the common slurp idioms? and
anyone who maintains could would find use File::Slurp; $text = read_file
$file' to be much better code. it also provides error handling options
that are trickier to code in some of the slurp idioms.


Uri Guttman  ------  --------
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About