Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you just want to slurp input to output, this should do the trick. </p> <pre><code>use Carp (); { #Lexical For FileHandle and $/ open my $fh, '&lt;' , '/path/to/file.txt' or Carp::croak("File Open Failed"); local $/ = undef; print scalar &lt;$fh&gt;; close $fh or Carp::carp("File Close Failed"); } </code></pre> <p>I guess in response to the "Does Perl have a PHP ReadFile Equivelant" , and I guess my answer would be "But it doesn't really need one". </p> <p>I've used PHP's manual File IO controls and they're a pain, Perls are just so easy to use by comparison that shelling out for a one-size-fits-all function seems over-kill. </p> <p>Also, you might want to look at <code>X-SendFile</code> support, and basically send a header to your webserver to tell it what file to send: <a href="http://john.guen.in/past/2007/4/17/send_files_faster_with_xsendfile/" rel="nofollow noreferrer">http://john.guen.in/past/2007/4/17/send_files_faster_with_xsendfile/</a> ( assuming of course it has permissions enough to access the file, but the file is just NOT normally accessible via a standard URI ) </p> <p><em>Edit</em> Noted, it <em>is</em> better to do it in a loop, I tested the above code with a hard-drive and it does implicitly try store the whole thing in an invisible temporary variable and eat all your ram. </p> <h2>Alternative using blocks</h2> <p>The following improved code reads the given file in blocks of 8192 chars, which is much more memory efficient, and gets a throughput respectably comparable with my disk raw read rate. ( I also pointed it at /dev/full for fits and giggles and got a healthy 500mb/s throughput, and it didn't eat all my rams, so that must be good ) </p> <pre><code>{ open my $fh , '&lt;', '/dev/sda' ; local $/ = \8192; # this tells IO to use 8192 char chunks. print $_ while defined ( $_ = scalar &lt;$fh&gt; ); close $fh; } </code></pre> <h2>Applying jrockways suggestions</h2> <pre><code>{ open my $fh , '&lt;', '/dev/sda5' ; print $_ while ( sysread $fh, $_ , 8192 ); close $fh; } </code></pre> <p>This literally doubles performance, ... and in some cases, gets me better throughput than DD does O_o. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload