Cari di Perl 
    Perl Tutorial
Daftar Isi
(Sebelumnya) Perl debugging tutorialTutorial on pack and unpack (Berikutnya)
Tutorials

Tutorial on opening things in Perl

Daftar Isi

NAME

perlopentut - tutorial on opening things in Perl

DESCRIPTION

Perl has two simple, built-in ways to open files: the shell way forconvenience, and the C way for precision. The shell way also has 2- and3-argument forms, which have different semantics for handling the filename.The choice is yours.

Open à la shell

Perl's open function was designed to mimic the way command-lineredirection in the shell works. Here are some basic examplesfrom the shell:

  1. $ myprogram file1 file2 file3
  2. $ myprogram < inputfile
  3. $ myprogram > outputfile
  4. $ myprogram >> outputfile
  5. $ myprogram | otherprogram
  6. $ otherprogram | myprogram

And here are some more advanced examples:

  1. $ otherprogram | myprogram f1 - f2
  2. $ otherprogram 2>&1 | myprogram -
  3. $ myprogram <&3
  4. $ myprogram >&4

Programmers accustomed to constructs like those above can take comfortin learning that Perl directly supports these familiar constructs usingvirtually the same syntax as the shell.

Simple Opens

The open function takes two arguments: the first is a filehandle,and the second is a single string comprising both what to open and howto open it. open returns true when it works, and when it fails,returns a false value and sets the special variable $! to reflectthe system error. If the filehandle was previously opened, it willbe implicitly closed first.

For example:

  1. open(INFO, "datafile") || die("can't open datafile: $!");
  2. open(INFO, "< datafile") || die("can't open datafile: $!");
  3. open(RESULTS,"> runstats") || die("can't open runstats: $!");
  4. open(LOG, ">> logfile ") || die("can't open logfile: $!");

If you prefer the low-punctuation version, you could write that this way:

  1. open INFO, "< datafile" or die "can't open datafile: $!";
  2. open RESULTS,"> runstats" or die "can't open runstats: $!";
  3. open LOG, ">> logfile " or die "can't open logfile: $!";

A few things to notice. First, the leading < is optional.If omitted, Perl assumes that you want to open the file for reading.

Note also that the first example uses the || logical operator, and thesecond uses or, which has lower precedence. Using || in the latterexamples would effectively mean

  1. open INFO, ( "< datafile" || die "can't open datafile: $!" );

which is definitely not what you want.

The other important thing to notice is that, just as in the shell,any whitespace before or after the filename is ignored. This is good,because you wouldn't want these to do different things:

  1. open INFO, "<datafile"
  2. open INFO, "< datafile"
  3. open INFO, "< datafile"

Ignoring surrounding whitespace also helps for when you read a filenamein from a different file, and forget to trim it before opening:

  1. $filename = <INFO>; # oops, \n still there
  2. open(EXTRA, "< $filename") || die "can't open $filename: $!";

This is not a bug, but a feature. Because open mimics the shell inits style of using redirection arrows to specify how to open the file, italso does so with respect to extra whitespace around the filename itselfas well. For accessing files with naughty names, see Dispelling the Dweomer.

There is also a 3-argument version of open, which lets you put thespecial redirection characters into their own argument:

  1. open( INFO, ">", $datafile ) || die "Can't create $datafile: $!";

In this case, the filename to open is the actual string in $datafile,so you don't have to worry about $datafile containing charactersthat might influence the open mode, or whitespace at the beginning ofthe filename that would be absorbed in the 2-argument version. Also,any reduction of unnecessary string interpolation is a good thing.

Indirect Filehandles

open's first argument can be a reference to a filehandle. As ofperl 5.6.0, if the argument is uninitialized, Perl will automaticallycreate a filehandle and put a reference to it in the first argument,like so:

  1. open( my $in, $infile ) or die "Couldn't read $infile: $!";
  2. while ( <$in> ) {
  3. # do something with $_
  4. }
  5. close $in;

Indirect filehandles make namespace management easier. Since filehandlesare global to the current package, two subroutines trying to openINFILE will clash. With two functions opening indirect filehandleslike my $infile, there's no clash and no need to worry about futureconflicts.

Another convenient behavior is that an indirect filehandle automaticallycloses when there are no more references to it:

  1. sub firstline {
  2. open( my $in, shift ) && return scalar <$in>;
  3. # no close() required
  4. }

Indirect filehandles also make it easy to pass filehandles to and returnfilehandles from subroutines:

  1. for my $file ( qw(this.conf that.conf) ) {
  2. my $fin = open_or_throw('<', $file);
  3. process_conf( $fin );
  4. # no close() needed
  5. }
  6. use Carp;
  7. sub open_or_throw {
  8. my ($mode, $filename) = @_;
  9. open my $h, $mode, $filename
  10. or croak "Could not open '$filename': $!";
  11. return $h;
  12. }

Pipe Opens

In C, when you want to open a file using the standard I/O library,you use the fopen function, but when opening a pipe, you use thepopen function. But in the shell, you just use a different redirectioncharacter. That's also the case for Perl. The open call remains the same--just its argument differs.

If the leading character is a pipe symbol, open starts up a newcommand and opens a write-only filehandle leading into that command.This lets you write into that handle and have what you write show up onthat command's standard input. For example:

  1. open(PRINTER, "| lpr -Plp1") || die "can't run lpr: $!";
  2. print PRINTER "stuff\n";
  3. close(PRINTER) || die "can't close lpr: $!";

If the trailing character is a pipe, you start up a new command and open aread-only filehandle leading out of that command. This lets whatever thatcommand writes to its standard output show up on your handle for reading.For example:

  1. open(NET, "netstat -i -n |") || die "can't fork netstat: $!";
  2. while (<NET>) { } # do something with input
  3. close(NET) || die "can't close netstat: $!";

What happens if you try to open a pipe to or from a non-existentcommand? If possible, Perl will detect the failure and set $! asusual. But if the command contains special shell characters, such as> or *, called 'metacharacters', Perl does not execute thecommand directly. Instead, Perl runs the shell, which then tries torun the command. This means that it's the shell that gets the errorindication. In such a case, the open call will only indicatefailure if Perl can't even run the shell. See How can I capture STDERR from an external command? in perlfaq8 to see how to cope withthis. There's also an explanation in perlipc.

If you would like to open a bidirectional pipe, the IPC::Open2library will handle this for you. Check out Bidirectional Communication with Another Process in perlipc

perl-5.6.x introduced a version of piped open that executes a processbased on its command line arguments without relying on the shell. (Similarto the system(@LIST) notation.) This is safer and faster than executinga single argument pipe-command, but does not allow special shellconstructs. (It is also not supported on Microsoft Windows, Mac OS Classicor RISC OS.)

Here's an example of open '-|', which prints a random Unixfortune cookie as uppercase:

  1. my $collection = shift(@ARGV);
  2. open my $fortune, '-|', 'fortune', $collection
  3. or die "Could not find fortune - $!";
  4. while (<$fortune>)
  5. {
  6. print uc($_);
  7. }
  8. close($fortune);

And this open '|-' pipes into lpr:

  1. open my $printer, '|-', 'lpr', '-Plp1'
  2. or die "can't run lpr: $!";
  3. print {$printer} "stuff\n";
  4. close($printer)
  5. or die "can't close lpr: $!";

The Minus File

Again following the lead of the standard shell utilities, Perl'sopen function treats a file whose name is a single minus, "-", in aspecial way. If you open minus for reading, it really means to accessthe standard input. If you open minus for writing, it really means toaccess the standard output.

If minus can be used as the default input or default output, what happensif you open a pipe into or out of minus? What's the default command itwould run? The same script as you're currently running! This is actuallya stealth fork hidden inside an open call. See Safe Pipe Opens in perlipc for details.

Mixing Reads and Writes

It is possible to specify both read and write access. All you do isadd a "+" symbol in front of the redirection. But as in the shell,using a less-than on a file never creates a new file; it only opens anexisting one. On the other hand, using a greater-than always clobbers(truncates to zero length) an existing file, or creates a brand-new oneif there isn't an old one. Adding a "+" for read-write doesn't affectwhether it only works on existing files or always clobbers existing ones.

  1. open(WTMP, "+< /usr/adm/wtmp")
  2. || die "can't open /usr/adm/wtmp: $!";
  3. open(SCREEN, "+> lkscreen")
  4. || die "can't open lkscreen: $!";
  5. open(LOGFILE, "+>> /var/log/applog")
  6. || die "can't open /var/log/applog: $!";

The first one won't create a new file, and the second one will alwaysclobber an old one. The third one will create a new file if necessaryand not clobber an old one, and it will allow you to read at any pointin the file, but all writes will always go to the end. In short,the first case is substantially more common than the second and thirdcases, which are almost always wrong. (If you know C, the plus inPerl's open is historically derived from the one in C's fopen(3S),which it ultimately calls.)

In fact, when it comes to updating a file, unless you're working ona binary file as in the WTMP case above, you probably don't want touse this approach for updating. Instead, Perl's -i flag comes tothe rescue. The following command takes all the C, C++, or yacc sourceor header files and changes all their foo's to bar's, leavingthe old version in the original filename with a ".orig" tackedon the end:

  1. $ perl -i.orig -pe 's/\bfoo\b/bar/g' *.[Cchy]

This is a short cut for some renaming games that are reallythe best way to update textfiles. See the second question in perlfaq5 for more details.

Filters

One of the most common uses for open is one you nevereven notice. When you process the ARGV filehandle using<ARGV>, Perl actually does an implicit open on each file in @ARGV. Thus a program called like this:

  1. $ myprogram file1 file2 file3

can have all its files opened and processed one at a timeusing a construct no more complex than:

  1. while (<>) {
  2. # do something with $_
  3. }

If @ARGV is empty when the loop first begins, Perl pretends you've openedup minus, that is, the standard input. In fact, $ARGV, the currentlyopen file during <ARGV> processing, is even set to "-"in these circumstances.

You are welcome to pre-process your @ARGV before starting the loop tomake sure it's to your liking. One reason to do this might be to removecommand options beginning with a minus. While you can always roll thesimple ones by hand, the Getopts modules are good for this:

  1. use Getopt::Std;
  2. # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o
  3. getopts("vDo:");
  4. # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o}
  5. getopts("vDo:", \%args);

Or the standard Getopt::Long module to permit named arguments:

  1. use Getopt::Long;
  2. GetOptions( "verbose" => \$verbose, # --verbose
  3. "Debug" => \$debug, # --Debug
  4. "output=s" => \$output );
  5. # --output=somestring or --output somestring

Another reason for preprocessing arguments is to make an emptyargument list default to all files:

  1. @ARGV = glob("*") unless @ARGV;

You could even filter out all but plain, text files. This is a bitsilent, of course, and you might prefer to mention them on the way.

  1. @ARGV = grep { -f && -T } @ARGV;

If you're using the -n or -p command-line options, youshould put changes to @ARGV in a BEGIN{} block.

Remember that a normal open has special properties, in that it mightcall fopen(3S) or it might called popen(3S), depending on what itsargument looks like; that's why it's sometimes called "magic open".Here's an example:

  1. $pwdinfo = `domainname` =~ /^(\(none\))?$/
  2. ? '< /etc/passwd'
  3. : 'ypcat passwd |';
  4. open(PWD, $pwdinfo)
  5. or die "can't open $pwdinfo: $!";

This sort of thing also comes into play in filter processing. Because<ARGV> processing employs the normal, shell-style Perl open,it respects all the special things we've already seen:

  1. $ myprogram f1 "cmd1|" - f2 "cmd2|" f3 < tmpfile

That program will read from the file f1, the process cmd1, standardinput (tmpfile in this case), the f2 file, the cmd2 command,and finally the f3 file.

Yes, this also means that if you have files named "-" (and so on) inyour directory, they won't be processed as literal files by open.You'll need to pass them as "./-", much as you would for the rm program,or you could use sysopen as described below.

One of the more interesting applications is to change files of a certainname into pipes. For example, to autoprocess gzipped or compressedfiles by decompressing them with gzip:

  1. @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc $_ |" : $_ } @ARGV;

Or, if you have the GET program installed from LWP,you can fetch URLs before processing them:

  1. @ARGV = map { m#^\w+://# ? "GET $_ |" : $_ } @ARGV;

It's not for nothing that this is called magic <ARGV>.Pretty nifty, eh?

Open à la C

If you want the convenience of the shell, then Perl's open isdefinitely the way to go. On the other hand, if you want finer precisionthan C's simplistic fopen(3S) provides you should look to Perl'ssysopen, which is a direct hook into the open(2) system call.That does mean it's a bit more involved, but that's the price of precision.

sysopen takes 3 (or 4) arguments.

  1. sysopen HANDLE, PATH, FLAGS, [MASK]

The HANDLE argument is a filehandle just as with open. The PATH isa literal path, one that doesn't pay attention to any greater-thans orless-thans or pipes or minuses, nor ignore whitespace. If it's there,it's part of the path. The FLAGS argument contains one or more valuesderived from the Fcntl module that have been or'd together using thebitwise "|" operator. The final argument, the MASK, is optional; ifpresent, it is combined with the user's current umask for the creationmode of the file. You should usually omit this.

Although the traditional values of read-only, write-only, and read-writeare 0, 1, and 2 respectively, this is known not to hold true on somesystems. Instead, it's best to load in the appropriate constants firstfrom the Fcntl module, which supplies the following standard flags:

  1. O_RDONLY Read only
  2. O_WRONLY Write only
  3. O_RDWR Read and write
  4. O_CREAT Create the file if it doesn't exist
  5. O_EXCL Fail if the file already exists
  6. O_APPEND Append to the file
  7. O_TRUNC Truncate the file
  8. O_NONBLOCK Non-blocking access

Less common flags that are sometimes available on some operatingsystems include O_BINARY, O_TEXT, O_SHLOCK, O_EXLOCK,O_DEFER, O_SYNC, O_ASYNC, O_DSYNC, O_RSYNC,O_NOCTTY, O_NDELAY and O_LARGEFILE. Consult your open(2)manpage or its local equivalent for details. (Note: starting fromPerl release 5.6 the O_LARGEFILE flag, if available, is automaticallyadded to the sysopen() flags because large files are the default.)

Here's how to use sysopen to emulate the simple open calls we hadbefore. We'll omit the || die $! checks for clarity, but make sureyou always check the return values in real code. These aren't quitethe same, since open will trim leading and trailing whitespace,but you'll get the idea.

To open a file for reading:

  1. open(FH, "< $path");
  2. sysopen(FH, $path, O_RDONLY);

To open a file for writing, creating a new file if needed or else truncatingan old file:

  1. open(FH, "> $path");
  2. sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT);

To open a file for appending, creating one if necessary:

  1. open(FH, ">> $path");
  2. sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT);

To open a file for update, where the file must already exist:

  1. open(FH, "+< $path");
  2. sysopen(FH, $path, O_RDWR);

And here are things you can do with sysopen that you cannot do witha regular open. As you'll see, it's just a matter of controlling theflags in the third argument.

To open a file for writing, creating a new file which must not previouslyexist:

  1. sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT);

To open a file for appending, where that file must already exist:

  1. sysopen(FH, $path, O_WRONLY | O_APPEND);

To open a file for update, creating a new file if necessary:

  1. sysopen(FH, $path, O_RDWR | O_CREAT);

To open a file for update, where that file must not already exist:

  1. sysopen(FH, $path, O_RDWR | O_EXCL | O_CREAT);

To open a file without blocking, creating one if necessary:

  1. sysopen(FH, $path, O_WRONLY | O_NONBLOCK | O_CREAT);

Permissions à la mode

If you omit the MASK argument to sysopen, Perl uses the octal value0666. The normal MASK to use for executables and directories shouldbe 0777, and for anything else, 0666.

Why so permissive? Well, it isn't really. The MASK will be modifiedby your process's current umask. A umask is a number representingdisabled permissions bits; that is, bits that will not be turned onin the created file's permissions field.

For example, if your umask were 027, then the 020 part woulddisable the group from writing, and the 007 part would disable othersfrom reading, writing, or executing. Under these conditions, passingsysopen 0666 would create a file with mode 0640, since 0666 & ~027is 0640.

You should seldom use the MASK argument to sysopen(). That takesaway the user's freedom to choose what permission new files will have.Denying choice is almost always a bad thing. One exception would be forcases where sensitive or private data is being stored, such as with mailfolders, cookie files, and internal temporary files.

Obscure Open Tricks

Re-Opening Files (dups)

Sometimes you already have a filehandle open, and want to make anotherhandle that's a duplicate of the first one. In the shell, we place anampersand in front of a file descriptor number when doing redirections.For example, 2>&1 makes descriptor 2 (that's STDERR in Perl)be redirected into descriptor 1 (which is usually Perl's STDOUT).The same is essentially true in Perl: a filename that begins with anampersand is treated instead as a file descriptor if a number, or as afilehandle if a string.

  1. open(SAVEOUT, ">&SAVEERR") || die "couldn't dup SAVEERR: $!";
  2. open(MHCONTEXT, "<&4") || die "couldn't dup fd4: $!";

That means that if a function is expecting a filename, but you don'twant to give it a filename because you already have the file open, youcan just pass the filehandle with a leading ampersand. It's best touse a fully qualified handle though, just in case the function happensto be in a different package:

  1. somefunction("&main::LOGFILE");

This way if somefunction() is planning on opening its argument, it canjust use the already opened handle. This differs from passing a handle,because with a handle, you don't open the file. Here you have somethingyou can pass to open.

If you have one of those tricky, newfangled I/O objects that the C++folks are raving about, then this doesn't work because those aren't aproper filehandle in the native Perl sense. You'll have to use fileno()to pull out the proper descriptor number, assuming you can:

  1. use IO::Socket;
  2. $handle = IO::Socket::INET->new("www.perl.com:80");
  3. $fd = $handle->fileno;
  4. somefunction("&$fd"); # not an indirect function call

It can be easier (and certainly will be faster) just to use realfilehandles though:

  1. use IO::Socket;
  2. local *REMOTE = IO::Socket::INET->new("www.perl.com:80");
  3. die "can't connect" unless defined(fileno(REMOTE));
  4. somefunction("&main::REMOTE");

If the filehandle or descriptor number is preceded not just with a simple"&" but rather with a "&=" combination, then Perl will not create acompletely new descriptor opened to the same place using the dup(2)system call. Instead, it will just make something of an alias to theexisting one using the fdopen(3S) library call. This is slightly moreparsimonious of systems resources, although this is less a concernthese days. Here's an example of that:

  1. $fd = $ENV{"MHCONTEXTFD"};
  2. open(MHCONTEXT, "<&=$fd") or die "couldn't fdopen $fd: $!";

If you're using magic <ARGV>, you could even pass in as acommand line argument in @ARGV something like "<&=$MHCONTEXTFD",but we've never seen anyone actually do this.

Dispelling the Dweomer

Perl is more of a DWIMmer language than something like Java--where DWIMis an acronym for "do what I mean". But this principle sometimes leadsto more hidden magic than one knows what to do with. In this way, Perlis also filled with dweomer, an obscure word meaning an enchantment.Sometimes, Perl's DWIMmer is just too much like dweomer for comfort.

If magic open is a bit too magical for you, you don't have to turnto sysopen. To open a file with arbitrary weird characters init, it's necessary to protect any leading and trailing whitespace.Leading whitespace is protected by inserting a "./" in front of afilename that starts with whitespace. Trailing whitespace is protectedby appending an ASCII NUL byte ("\0") at the end of the string.

  1. $file =~ s#^(\s)#./$1#;
  2. open(FH, "< $file\0") || die "can't open $file: $!";

This assumes, of course, that your system considers dot the currentworking directory, slash the directory separator, and disallows ASCIINULs within a valid filename. Most systems follow these conventions,including all POSIX systems as well as proprietary Microsoft systems.The only vaguely popular system that doesn't work this way is the"Classic" Macintosh system, which uses a colon where the rest of ususe a slash. Maybe sysopen isn't such a bad idea after all.

If you want to use <ARGV> processing in a totally boringand non-magical way, you could do this first:

  1. # "Sam sat on the ground and put his head in his hands.
  2. # 'I wish I had never come here, and I don't want to see
  3. # no more magic,' he said, and fell silent."
  4. for (@ARGV) {
  5. s#^([^./])#./$1#;
  6. $_ .= "\0";
  7. }
  8. while (<>) {
  9. # now process $_
  10. }

But be warned that users will not appreciate being unable to use "-"to mean standard input, per the standard convention.

Paths as Opens

You've probably noticed how Perl's warn and die functions canproduce messages like:

  1. Some warning at scriptname line 29, <FH> line 7.

That's because you opened a filehandle FH, and had read in seven recordsfrom it. But what was the name of the file, rather than the handle?

If you aren't running with strict refs, or if you've turned them offtemporarily, then all you have to do is this:

  1. open($path, "< $path") || die "can't open $path: $!";
  2. while (<$path>) {
  3. # whatever
  4. }

Since you're using the pathname of the file as its handle,you'll get warnings more like

  1. Some warning at scriptname line 29, </etc/motd> line 7.

Single Argument Open

Remember how we said that Perl's open took two arguments? That was apassive prevarication. You see, it can also take just one argument.If and only if the variable is a global variable, not a lexical, youcan pass open just one argument, the filehandle, and it will get the path from the global scalar variable of the same name.

  1. $FILE = "/etc/motd";
  2. open FILE or die "can't open $FILE: $!";
  3. while (<FILE>) {
  4. # whatever
  5. }

Why is this here? Someone has to cater to the hysterical porpoises.It's something that's been in Perl since the very beginning, if notbefore.

Playing with STDIN and STDOUT

One clever move with STDOUT is to explicitly close it when you're donewith the program.

  1. END { close(STDOUT) || die "can't close stdout: $!" }

If you don't do this, and your program fills up the disk partition dueto a command line redirection, it won't report the error exit with afailure status.

You don't have to accept the STDIN and STDOUT you were given. You arewelcome to reopen them if you'd like.

  1. open(STDIN, "< datafile")
  2. || die "can't open datafile: $!";
  3. open(STDOUT, "> output")
  4. || die "can't open output: $!";

And then these can be accessed directly or passed on to subprocesses.This makes it look as though the program were initially invokedwith those redirections from the command line.

It's probably more interesting to connect these to pipes. For example:

  1. $pager = $ENV{PAGER} || "(less || more)";
  2. open(STDOUT, "| $pager")
  3. || die "can't fork a pager: $!";

This makes it appear as though your program were called with its stdoutalready piped into your pager. You can also use this kind of thingin conjunction with an implicit fork to yourself. You might do thisif you would rather handle the post processing in your own program,just in a different process:

  1. head(100);
  2. while (<>) {
  3. print;
  4. }
  5. sub head {
  6. my $lines = shift || 20;
  7. return if $pid = open(STDOUT, "|-"); # return if parent
  8. die "cannot fork: $!" unless defined $pid;
  9. while (<STDIN>) {
  10. last if --$lines < 0;
  11. print;
  12. }
  13. exit;
  14. }

This technique can be applied to repeatedly push as many filters on youroutput stream as you wish.

Other I/O Issues

These topics aren't really arguments related to open or sysopen,but they do affect what you do with your open files.

Opening Non-File Files

When is a file not a file? Well, you could say when it exists butisn't a plain file. We'll check whether it's a symbolic link first,just in case.

  1. if (-l $file || ! -f _) {
  2. print "$file is not a plain file\n";
  3. }

What other kinds of files are there than, well, files? Directories,symbolic links, named pipes, Unix-domain sockets, and block and characterdevices. Those are all files, too--just not plain files. This isn'tthe same issue as being a text file. Not all text files are plain files.Not all plain files are text files. That's why there are separate -fand -T file tests.

To open a directory, you should use the opendir function, thenprocess it with readdir, carefully restoring the directory name if necessary:

  1. opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
  2. while (defined($file = readdir(DIR))) {
  3. # do something with "$dirname/$file"
  4. }
  5. closedir(DIR);

If you want to process directories recursively, it's better to use theFile::Find module. For example, this prints out all files recursivelyand adds a slash to their names if the file is a directory.

  1. @ARGV = qw(.) unless @ARGV;
  2. use File::Find;
  3. find sub { print $File::Find::name, -d && '/', "\n" }, @ARGV;

This finds all bogus symbolic links beneath a particular directory:

  1. find sub { print "$File::Find::name\n" if -l && !-e }, $dir;

As you see, with symbolic links, you can just pretend that it iswhat it points to. Or, if you want to know what it points to, thenreadlink is called for:

  1. if (-l $file) {
  2. if (defined($whither = readlink($file))) {
  3. print "$file points to $whither\n";
  4. } else {
  5. print "$file points nowhere: $!\n";
  6. }
  7. }

Opening Named Pipes

Named pipes are a different matter. You pretend they're regular files,but their opens will normally block until there is both a reader anda writer. You can read more about them in Named Pipes in perlipc.Unix-domain sockets are rather different beasts as well; they'redescribed in Unix-Domain TCP Clients and Servers in perlipc.

When it comes to opening devices, it can be easy and it can be tricky.We'll assume that if you're opening up a block device, you know whatyou're doing. The character devices are more interesting. These aretypically used for modems, mice, and some kinds of printers. This isdescribed in How do I read and write the serial port? in perlfaq8It's often enough to open them carefully:

  1. sysopen(TTYIN, "/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY)
  2. # (O_NOCTTY no longer needed on POSIX systems)
  3. or die "can't open /dev/ttyS1: $!";
  4. open(TTYOUT, "+>&TTYIN")
  5. or die "can't dup TTYIN: $!";
  6. $ofh = select(TTYOUT); $| = 1; select($ofh);
  7. print TTYOUT "+++at\015";
  8. $answer = <TTYIN>;

With descriptors that you haven't opened using sysopen, such assockets, you can set them to be non-blocking using fcntl:

  1. use Fcntl;
  2. my $old_flags = fcntl($handle, F_GETFL, 0)
  3. or die "can't get flags: $!";
  4. fcntl($handle, F_SETFL, $old_flags | O_NONBLOCK)
  5. or die "can't set non blocking: $!";

Rather than losing yourself in a morass of twisting, turning ioctls,all dissimilar, if you're going to manipulate ttys, it's best tomake calls out to the stty(1) program if you have it, or else use theportable POSIX interface. To figure this all out, you'll need to read thetermios(3) manpage, which describes the POSIX interface to tty devices,and then POSIX, which describes Perl's interface to POSIX. There arealso some high-level modules on CPAN that can help you with these games.Check out Term::ReadKey and Term::ReadLine.

Opening Sockets

What else can you open? To open a connection using sockets, you won't useone of Perl's two open functions. See Sockets: Client/Server Communication in perlipc for that. Here's an example. Once you have it, you can use FH as a bidirectional filehandle.

  1. use IO::Socket;
  2. local *FH = IO::Socket::INET->new("www.perl.com:80");

For opening up a URL, the LWP modules from CPAN are just whatthe doctor ordered. There's no filehandle interface, butit's still easy to get the contents of a document:

  1. use LWP::Simple;
  2. $doc = get('http://www.cpan.org/');

Binary Files

On certain legacy systems with what could charitably be called terminallyconvoluted (some would say broken) I/O models, a file isn't a file--atleast, not with respect to the C standard I/O library. On these oldsystems whose libraries (but not kernels) distinguish between text andbinary streams, to get files to behave properly you'll have to bend overbackwards to avoid nasty problems. On such infelicitous systems, socketsand pipes are already opened in binary mode, and there is currently noway to turn that off. With files, you have more options.

Another option is to use the binmode function on the appropriatehandles before doing regular I/O on them:

  1. binmode(STDIN);
  2. binmode(STDOUT);
  3. while (<STDIN>) { print }

Passing sysopen a non-standard flag option will also open the file inbinary mode on those systems that support it. This is the equivalent ofopening the file normally, then calling binmode on the handle.

  1. sysopen(BINDAT, "records.data", O_RDWR | O_BINARY)
  2. || die "can't open records.data: $!";

Now you can use read and print on that handle without worryingabout the non-standard system I/O library breaking your data. It's nota pretty picture, but then, legacy systems seldom are. CP/M will bewith us until the end of days, and after.

On systems with exotic I/O systems, it turns out that, astonishinglyenough, even unbuffered I/O using sysread and syswrite might dosneaky data mutilation behind your back.

  1. while (sysread(WHENCE, $buf, 1024)) {
  2. syswrite(WHITHER, $buf, length($buf));
  3. }

Depending on the vicissitudes of your runtime system, even these callsmay need binmode or O_BINARY first. Systems known to be free ofsuch difficulties include Unix, the Mac OS, Plan 9, and Inferno.

File Locking

In a multitasking environment, you may need to be careful not to collidewith other processes who want to do I/O on the same files as youare working on. You'll often need shared or exclusive lockson files for reading and writing respectively. You might justpretend that only exclusive locks exist.

Never use the existence of a file -e $file as a locking indication,because there is a race condition between the test for the existence ofthe file and its creation. It's possible for another process to createa file in the slice of time between your existence check and your attemptto create the file. Atomicity is critical.

Perl's most portable locking interface is via the flock function,whose simplicity is emulated on systems that don't directly support itsuch as SysV or Windows. The underlying semantics may affect howit all works, so you should learn how flock is implemented on yoursystem's port of Perl.

File locking does not lock out another process that would like todo I/O. A file lock only locks out others trying to get a lock, notprocesses trying to do I/O. Because locks are advisory, if one processuses locking and another doesn't, all bets are off.

By default, the flock call will block until a lock is granted.A request for a shared lock will be granted as soon as there is noexclusive locker. A request for an exclusive lock will be granted assoon as there is no locker of any kind. Locks are on file descriptors,not file names. You can't lock a file until you open it, and you can'thold on to a lock once the file has been closed.

Here's how to get a blocking shared lock on a file, typically usedfor reading:

  1. use 5.004;
  2. use Fcntl qw(:DEFAULT :flock);
  3. open(FH, "< filename") or die "can't open filename: $!";
  4. flock(FH, LOCK_SH) or die "can't lock filename: $!";
  5. # now read from FH

You can get a non-blocking lock by using LOCK_NB.

  1. flock(FH, LOCK_SH | LOCK_NB)
  2. or die "can't lock filename: $!";

This can be useful for producing more user-friendly behaviour by warningif you're going to be blocking:

  1. use 5.004;
  2. use Fcntl qw(:DEFAULT :flock);
  3. open(FH, "< filename") or die "can't open filename: $!";
  4. unless (flock(FH, LOCK_SH | LOCK_NB)) {
  5. $| = 1;
  6. print "Waiting for lock...";
  7. flock(FH, LOCK_SH) or die "can't lock filename: $!";
  8. print "got it.\n"
  9. }
  10. # now read from FH

To get an exclusive lock, typically used for writing, you have to becareful. We sysopen the file so it can be locked before it getsemptied. You can get a nonblocking version using LOCK_EX | LOCK_NB.

  1. use 5.004;
  2. use Fcntl qw(:DEFAULT :flock);
  3. sysopen(FH, "filename", O_WRONLY | O_CREAT)
  4. or die "can't open filename: $!";
  5. flock(FH, LOCK_EX)
  6. or die "can't lock filename: $!";
  7. truncate(FH, 0)
  8. or die "can't truncate filename: $!";
  9. # now write to FH

Finally, due to the uncounted millions who cannot be dissuaded fromwasting cycles on useless vanity devices called hit counters, here'show to increment a number in a file safely:

  1. use Fcntl qw(:DEFAULT :flock);
  2. sysopen(FH, "numfile", O_RDWR | O_CREAT)
  3. or die "can't open numfile: $!";
  4. # autoflush FH
  5. $ofh = select(FH); $| = 1; select ($ofh);
  6. flock(FH, LOCK_EX)
  7. or die "can't write-lock numfile: $!";
  8. $num = <FH> || 0;
  9. seek(FH, 0, 0)
  10. or die "can't rewind numfile : $!";
  11. print FH $num+1, "\n"
  12. or die "can't write numfile: $!";
  13. truncate(FH, tell(FH))
  14. or die "can't truncate numfile: $!";
  15. close(FH)
  16. or die "can't close numfile: $!";

IO Layers

In Perl 5.8.0 a new I/O framework called "PerlIO" was introduced.This is a new "plumbing" for all the I/O happening in Perl; for themost part everything will work just as it did, but PerlIO also broughtin some new features such as the ability to think of I/O as "layers".One I/O layer may in addition to just moving the data also dotransformations on the data. Such transformations may includecompression and decompression, encryption and decryption, and transformingbetween various character encodings.

Full discussion about the features of PerlIO is out of scope for thistutorial, but here is how to recognize the layers being used:

  • The three-(or more)-argument form of open is being used and thesecond argument contains something else in addition to the usual'<', '>', '>>', '|' and their variants,for example:

    1. open(my $fh, "<:crlf", $fn);
  • The two-argument form of binmode is being used, for example

    1. binmode($fh, ":encoding(utf16)");

For more detailed discussion about PerlIO see PerlIO;for more detailed discussion about Unicode and I/O see perluniintro.

SEE ALSO

The open and sysopen functions in perlfunc(1);the system open(2), dup(2), fopen(3), and fdopen(3) manpages;the POSIX documentation.

AUTHOR and COPYRIGHT

Copyright 1998 Tom Christiansen.

This documentation is free; you can redistribute it and/or modify itunder the same terms as Perl itself.

Irrespective of its distribution, all code examples in these files arehereby placed into the public domain. You are permitted andencouraged to use this code in your own programs for fun or for profitas you see fit. A simple comment in the code giving credit would becourteous but is not required.

HISTORY

First release: Sat Jan 9 08:09:11 MST 1999

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Perl debugging tutorialTutorial on pack and unpack (Berikutnya)