Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl open() documentation use two different FILEHANDLE styles?

The documentation for the open function shows the syntax of open() as:

  • open FILEHANDLE,EXPR
  • open FILEHANDLE,MODE,EXPR
  • open FILEHANDLE,MODE,EXPR,LIST
  • open FILEHANDLE,MODE,REFERENCE
  • open FILEHANDLE

Down in the examples they have places where a normal $-prefixed variable is used for the file handle:

open(my $fh, "<", "input.txt")

and also examples where a bareword is used:

open(FOO, "|tr '[a-z]' '[A-Z]'");

One question is what is the name of each style as in "I am using __ for a filehandle" in each case? The other is, why did they start using bare words for open() in the documentation? It appears to be the later uses all don't involve normal filename open()s. Is the $-prefixed form not acceptable in those instances?

like image 894
Scooter Avatar asked Jun 29 '13 02:06

Scooter


People also ask

What is Perl filehandle?

A filehandle is an internal Perl structure that associates with a file name. Perl File handling is important as it is helpful in accessing file such as text files, log files or configuration files. Perl filehandles are capable of creating, reading, opening and closing a file.

How many arguments does open take?

Most often, open gets invoked with three arguments: the required FILEHANDLE (usually an empty scalar variable), followed by MODE (usually a literal describing the I/O mode the filehandle will use), and then the filename that the new filehandle will refer to.

What does <> do in Perl?

It's an operator. Specifically, the readline operator. There's a reference to it as the "angle operator" in perlvar, although there isn't actually any such operator.

How do I close a file in Perl?

To close a file handle, and therefore disassociate the file handle from the corresponding file, you use the close function. This flushes the file handle's buffers and closes the system's file descriptor. If no FILEHANDLE is specified, then it closes the currently selected filehandle.

What are the three basic filehandles in Perl?

The three basic FileHandles in Perl are STDIN, STDOUT, and STDERR, which represent Standard Input, Standard Output, and Standard Error devices respectively. File Handling is usually done through the open function. FileHandle- The reference to the file, that can be used within the program or until its closure.

How to open any new file in Perl?

There are following two functions with multiple forms, which can be used to open any new or existing file in Perl. Here FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file. Following is the syntax to open file.txt in read-only mode.

How to read or write a file using a filehandle variable?

Through a filehandle variable, you can read from the file or write to the file depending on how you open the file. You use open () function to open files. The open () function has three arguments: Mode: you can open a file for reading, writing or appending. Filename: the path to the file that is being opened.

What is the difference between filehandle new_from_FD and filehandle open?

FileHandle::new_from_fd creates a FileHandle like new does. It requires two parameters, which are passed to FileHandle::fdopen; if the fdopen fails, the FileHandle object is destroyed. Otherwise, it is returned to the caller. FileHandle::open accepts one parameter or two. With one parameter, it is just a front end for the built-in open function.


1 Answers

The bareword form is, essentially, just historical legacy for backward compatibility. Using a lexical variable is pretty much always The Right Thing To Do in new code.

→ incidentally, $x is a lexical scalar variable, where FOO is, as you said, called a bareword

Details/Digression

Just for completeness, as @Joe_Z pointed out in the comments, the lexical filehandle objects are “relatively new,” as a part of the rather major rewrite between Perl 5.005 and 5.6 (they even gained whole orders of magnitude in that version number…).

However, technically, the bareword FOO (or, e.g. STDIN) is interpreted in a separate namespace just for filehandles. Since there isn't a sigil (like $ @ % &) for the filehandle namespace, there are only two ways to reference a filehandle in that namespace:

  • You can refer to it in the indirect-object slot of certain functions, like print, who will (behind the scenes) infer that a bareword must refer to a filehandle, for historical reasons;
  • You can use a typeglob, like *FOO, which refers to “anything in any namespace which happens to be bound to the symbol FOO.

Note that in some languages, like C or Scheme, a single symbol has no type sigils, and so all symbols can be bound only in one way (e.g. one cannot have a variable named printf and a function named printf in C … generally), whereas in Perl or (e.g.) Common Lisp, the same symbol foo can be bound to many different things; the distinction is that Perl actually requires you to use sigils to disambiguate “which foo you mean” in most contexts. $foo, @foo = @foo[ $x .. $y], $foo[ $n ], %foo = @foo{ $k1, $k2 } = $foo{ $k }, &foo and the like.

By using barewords as filehandles, though, you lose some abilities:

Significantly, in order to bind them locally or lexically (instead of globally), you need to bind every symbol in every namespace, because there is no sigil available. Thus, my $foo and my @foo can live in two different scratchpads (scopes), where perhaps one outlives the other; but my *foo would include both of these, as well as the filehandle foo (and potentially other obscure corner-cases, like format specifiers, although I wouldn't swear to it).

It's also immensely difficult to pass a bareword-style filehandle into a function, and the like.

Basically, barewords inherit all of the downsides of global scope and have none of the advantages of lexical variables.

perldoc perldata has a nice section on Typeglobs and Filehandles which probably explains these things a bit more clearly, as well. I don't have my copy handy, but I believe the Camel goes into more detail on the subject, as well.

like image 147
BRPocock Avatar answered Nov 15 '22 20:11

BRPocock