Category Archives: files & filehandles

Read a few lines from a file

How would you get more than one line from a file? In the original Effective Perl blog that Joseph set up to support the first edition of Effective Perl Programming, he shows two possible techniques. One uses a foreach: my @lines; for (my $i = 0; $i < 10; $i++) { my $line = <STDIN>; [...]

Avoid modifying scalars connected to string filehandles

Since Perl 5.8, you can treat a string as a file (Item 54. Open filehandles to and from strings). You can open a filehandle, read from the string, write to the string, and most of the other things that you can do with a file. There are some gotchas though, when you deal with that [...]

Effective Perl free sample chapter: Files and Filehandles

Addison-Wesley converted our chapter on “Files and Filehandles” to HTML and put it online for as a free sample chapter. I selected this chapter as the free sample because it was the most fun to write but also the most valuable to new Perl programmers. Filehandles are the way you interact with the world, and [...]

Memory-map files instead of slurping them

The conventional wisdom for slurping a file into a Perl program is to actually load the file into a program. We showed some of these in Item 53: Consider different ways of reading from a stream. There are several idioms for doing it, from doing it yourself: my $text = do { local( @ARGV, $/ [...]