Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Input and Output > Filehandles - Pg. 91

my @items = qw( wilma dino pebbles ); my $format = "The items are:\n" . ("%10s\n" x @items); ## print "the format is >>$format<<\n"; # for debugging printf $format, @items; This uses the x operator (which you learned about in Chapter 2) to replicate the given string a number of times given by @items (which is being used in a scalar context). In this case, that's 3 , since there are 3 items, so the resulting format string is the same as if you wrote it as "The items are:\n%10s\n%10s\n%10s\n" . And the output prints each item on its own line, right-justified in a 10-character column, under a heading line. Pretty cool, huh? But not cool enough because you can even combine these: printf "The items are:\n".("%10s\n" x @items), @items; Note that here you have @items being used once in a scalar context, to get its length, and once in a list context, to get its contents. Context is important. Filehandles A filehandle is the name in a Perl program for an I/O connection between your Perl process and the outside world. That is, it's the name of a connection, not necessarily the name of a file. Indeed, Perl has evolved that there might not even be a file behind that filehandle. Before Perl 5.6, all filehandle names were barewords, and Perl 5.6 added the ability to store a filehandle reference in a normal scalar variable. We'll show you the bareword versions first since Perl still uses those for its special filehandles, and catch up with the scalar variable versions later in this chapter. You name these filehandles just like other Perl identifiers: letters, digits, and under- scores (but not starting with a digit). The bareword filehandles don't have any prefix character, so Perl might confuse them with present or future reserved words, or with labels, which you'll see in Chapter 10. Once again, as with labels, the recommendation from Larry is that you use all uppercase letters in the name of your filehandle--not only does it stand out better, but it also guarantees that your program won't fail when Perl introduces a future (always lowercase) reserved word. But there are also six special filehandle names that Perl already uses for its own pur- poses: STDIN , STDOUT , STDERR , DATA , ARGV , and ARGVOUT . 24 Although you may choose any filehandle name you like, you shouldn't choose one of those six unless you intend to use that one's special properties. 25 24. Some people hate typing in all caps, even for a moment, and will try spelling these in lowercase, like stdin . Perl may even let you get away with that from time to time, but not always. The details of when these work and when they fail are beyond the scope of this book. But the important thing is that programs that rely upon this kindness will one day break, so it is best to avoid lowercase here. 25. In some cases, you could (re)use these names without a problem. But your maintenance programmer may think that you're using the name for its built-in features, and thus may be confused. Filehandles | 91