x
Loading
 Loading
Hello, Guest | Login | Register

Using Filehandles with Perl

In the past two columns, I looked at using references in Perl and showed the basic syntax for creating references to arrays, hashes, scalars, and subroutines. I also described the canonical form of converting a non-reference expression into a reference, and how to use the shortcut rules to make this simpler.

In the past two columns, I looked at using references in Perl and showed the basic syntax for creating references to arrays, hashes, scalars, and subroutines. I also described the canonical form of converting a non-reference expression into a reference, and how to use the shortcut rules to make this simpler.

Let’s take a look now at filehandles and directory handles. These handles let us look at or talk to the “outside world,” a worthwhile endeavor if we want our program to have some permanent impact on the computing environment.

First, recall that a filehandle doesn’t really have a syntax to make it a full-fledged variable. You can’t assign it, use it (directly) with local() or my(), pass it to or from a subroutine, or store it into a data structure. What does that leave? Well, there are about a dozen operations that use a filehandle or a directory handle, specified by a bareword (an alphanumeric symbol sequence separated by double colons, like STDIN or MyPack::Output). For refreshers, that looks like this:

while (<INPUT>) { last unless /S/; print OUTPUT $_; } 

Now, that’s a nice ordinary chunk of code. In fact, it’s a nicely useful chunk of code that copies all of the contents on the filehandle INPUT to the filehandle OUTPUT, until the first blank line. What makes this useful is that it’s the formula for “ARPA text messages,” the structure of e-mail and Usenet postings. Both of these are transmitted…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace