1N/A=head1 NAME
1N/A
1N/Aperlfaq5 - Files and Formats ($Revision: 1.30 $, $Date: 2003/11/23 08:07:46 $)
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThis section deals with I/O and the "f" issues: filehandles, flushing,
1N/Aformats, and footers.
1N/A
1N/A=head2 How do I flush/unbuffer an output filehandle? Why must I do this?
1N/A
1N/APerl does not support truly unbuffered output (except
1N/Ainsofar as you can C<syswrite(OUT, $char, 1)>), although it
1N/Adoes support is "command buffering", in which a physical
1N/Awrite is performed after every output command.
1N/A
1N/AThe C standard I/O library (stdio) normally buffers
1N/Acharacters sent to devices so that there isn't a system call
1N/Afor each byte. In most stdio implementations, the type of
1N/Aoutput buffering and the size of the buffer varies according
1N/Ato the type of device. Perl's print() and write() functions
1N/Anormally buffer output, while syswrite() bypasses buffering
1N/Aall together.
1N/A
1N/AIf you want your output to be sent immediately when you
1N/Aexecute print() or write() (for instance, for some network
1N/Aprotocols), you must set the handle's autoflush flag. This
1N/Aflag is the Perl variable $| and when it is set to a true
1N/Avalue, Perl will flush the handle's buffer after each
1N/Aprint() or write(). Setting $| affects buffering only for
1N/Athe currently selected default file handle. You choose this
1N/Ahandle with the one argument select() call (see
1N/AL<perlvar/$E<verbar>> and L<perlfunc/select>).
1N/A
1N/AUse select() to choose the desired handle, then set its
1N/Aper-filehandle variables.
1N/A
1N/A $old_fh = select(OUTPUT_HANDLE);
1N/A $| = 1;
1N/A select($old_fh);
1N/A
1N/ASome idioms can handle this in a single statement:
1N/A
1N/A select((select(OUTPUT_HANDLE), $| = 1)[0]);
1N/A
1N/A $| = 1, select $_ for select OUTPUT_HANDLE;
1N/A
1N/ASome modules offer object-oriented access to handles and their
1N/Avariables, although they may be overkill if this is the only
1N/Athing you do with them. You can use IO::Handle:
1N/A
1N/A use IO::Handle;
1N/A open(DEV, ">/dev/printer"); # but is this?
1N/A DEV->autoflush(1);
1N/A
1N/Aor IO::Socket:
1N/A
1N/A use IO::Socket; # this one is kinda a pipe?
1N/A my $sock = IO::Socket::INET->new( 'www.example.com:80' ) ;
1N/A
1N/A $sock->autoflush();
1N/A
1N/A=head2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
1N/A
1N/AUse the Tie::File module, which is included in the standard
1N/Adistribution since Perl 5.8.0.
1N/A
1N/A=head2 How do I count the number of lines in a file?
1N/A
1N/AOne fairly efficient way is to count newlines in the file. The
1N/Afollowing program uses a feature of tr///, as documented in L<perlop>.
1N/AIf your text file doesn't end with a newline, then it's not really a
1N/Aproper text file, so this may report one fewer line than you expect.
1N/A
1N/A $lines = 0;
1N/A open(FILE, $filename) or die "Can't open `$filename': $!";
1N/A while (sysread FILE, $buffer, 4096) {
1N/A $lines += ($buffer =~ tr/\n//);
1N/A }
1N/A close FILE;
1N/A
1N/AThis assumes no funny games with newline translations.
1N/A
1N/A=head2 How can I use Perl's C<-i> option from within a program?
1N/A
1N/AC<-i> sets the value of Perl's C<$^I> variable, which in turn affects
1N/Athe behavior of C<< <> >>; see L<perlrun> for more details. By
1N/Amodifying the appropriate variables directly, you can get the same
1N/Abehavior within a larger program. For example:
1N/A
1N/A # ...
1N/A {
1N/A local($^I, @ARGV) = ('.orig', glob("*.c"));
1N/A while (<>) {
1N/A if ($. == 1) {
1N/A print "This line should appear at the top of each file\n";
1N/A }
1N/A s/\b(p)earl\b/${1}erl/i; # Correct typos, preserving case
1N/A print;
1N/A close ARGV if eof; # Reset $.
1N/A }
1N/A }
1N/A # $^I and @ARGV return to their old values here
1N/A
1N/AThis block modifies all the C<.c> files in the current directory,
1N/Aleaving a backup of the original data from each file in a new
1N/AC<.c.orig> file.
1N/A
1N/A=head2 How do I make a temporary file name?
1N/A
1N/AUse the File::Temp module, see L<File::Temp> for more information.
1N/A
1N/A use File::Temp qw/ tempfile tempdir /;
1N/A
1N/A $dir = tempdir( CLEANUP => 1 );
1N/A ($fh, $filename) = tempfile( DIR => $dir );
1N/A
1N/A # or if you don't need to know the filename
1N/A
1N/A $fh = tempfile( DIR => $dir );
1N/A
1N/AThe File::Temp has been a standard module since Perl 5.6.1. If you
1N/Adon't have a modern enough Perl installed, use the C<new_tmpfile>
1N/Aclass method from the IO::File module to get a filehandle opened for
1N/Areading and writing. Use it if you don't need to know the file's name:
1N/A
1N/A use IO::File;
1N/A $fh = IO::File->new_tmpfile()
1N/A or die "Unable to make new temporary file: $!";
1N/A
1N/AIf you're committed to creating a temporary file by hand, use the
1N/Aprocess ID and/or the current time-value. If you need to have many
1N/Atemporary files in one process, use a counter:
1N/A
1N/A BEGIN {
1N/A use Fcntl;
1N/A my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMPDIR} || $ENV{TEMP};
1N/A my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
1N/A sub temp_file {
1N/A local *FH;
1N/A my $count = 0;
1N/A until (defined(fileno(FH)) || $count++ > 100) {
1N/A $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
1N/A # O_EXCL is required for security reasons.
1N/A sysopen(FH, $base_name, O_WRONLY|O_EXCL|O_CREAT);
1N/A }
1N/A if (defined(fileno(FH))
1N/A return (*FH, $base_name);
1N/A } else {
1N/A return ();
1N/A }
1N/A }
1N/A }
1N/A
1N/A=head2 How can I manipulate fixed-record-length files?
1N/A
1N/AThe most efficient way is using L<pack()|perlfunc/"pack"> and
1N/AL<unpack()|perlfunc/"unpack">. This is faster than using
1N/AL<substr()|perlfunc/"substr"> when taking many, many strings. It is
1N/Aslower for just a few.
1N/A
1N/AHere is a sample chunk of code to break up and put back together again
1N/Asome fixed-format input lines, in this case from the output of a normal,
1N/ABerkeley-style ps:
1N/A
1N/A # sample input line:
1N/A # 15158 p5 T 0:00 perl /home/tchrist/scripts/now-what
1N/A my $PS_T = 'A6 A4 A7 A5 A*';
1N/A open my $ps, '-|', 'ps';
1N/A print scalar <$ps>;
1N/A my @fields = qw( pid tt stat time command );
1N/A while (<$ps>) {
1N/A my %process;
1N/A @process{@fields} = unpack($PS_T, $_);
1N/A for my $field ( @fields ) {
1N/A print "$field: <$process{$field}>\n";
1N/A }
1N/A print 'line=', pack($PS_T, @process{@fields} ), "\n";
1N/A }
1N/A
1N/AWe've used a hash slice in order to easily handle the fields of each row.
1N/AStoring the keys in an array means it's easy to operate on them as a
1N/Agroup or loop over them with for. It also avoids polluting the program
1N/Awith global variables and using symbolic references.
1N/A
1N/A=head2 How can I make a filehandle local to a subroutine? How do I pass filehandles between subroutines? How do I make an array of filehandles?
1N/A
1N/AAs of perl5.6, open() autovivifies file and directory handles
1N/Aas references if you pass it an uninitialized scalar variable.
1N/AYou can then pass these references just like any other scalar,
1N/Aand use them in the place of named handles.
1N/A
1N/A open my $fh, $file_name;
1N/A
1N/A open local $fh, $file_name;
1N/A
1N/A print $fh "Hello World!\n";
1N/A
1N/A process_file( $fh );
1N/A
1N/ABefore perl5.6, you had to deal with various typeglob idioms
1N/Awhich you may see in older code.
1N/A
1N/A open FILE, "> $filename";
1N/A process_typeglob( *FILE );
1N/A process_reference( \*FILE );
1N/A
1N/A sub process_typeglob { local *FH = shift; print FH "Typeglob!" }
1N/A sub process_reference { local $fh = shift; print $fh "Reference!" }
1N/A
1N/AIf you want to create many anonymous handles, you should
1N/Acheck out the Symbol or IO::Handle modules.
1N/A
1N/A=head2 How can I use a filehandle indirectly?
1N/A
1N/AAn indirect filehandle is using something other than a symbol
1N/Ain a place that a filehandle is expected. Here are ways
1N/Ato get indirect filehandles:
1N/A
1N/A $fh = SOME_FH; # bareword is strict-subs hostile
1N/A $fh = "SOME_FH"; # strict-refs hostile; same package only
1N/A $fh = *SOME_FH; # typeglob
1N/A $fh = \*SOME_FH; # ref to typeglob (bless-able)
1N/A $fh = *SOME_FH{IO}; # blessed IO::Handle from *SOME_FH typeglob
1N/A
1N/AOr, you can use the C<new> method from one of the IO::* modules to
1N/Acreate an anonymous filehandle, store that in a scalar variable,
1N/Aand use it as though it were a normal filehandle.
1N/A
1N/A use IO::Handle; # 5.004 or higher
1N/A $fh = IO::Handle->new();
1N/A
1N/AThen use any of those as you would a normal filehandle. Anywhere that
1N/APerl is expecting a filehandle, an indirect filehandle may be used
1N/Ainstead. An indirect filehandle is just a scalar variable that contains
1N/Aa filehandle. Functions like C<print>, C<open>, C<seek>, or
1N/Athe C<< <FH> >> diamond operator will accept either a named filehandle
1N/Aor a scalar variable containing one:
1N/A
1N/A ($ifh, $ofh, $efh) = (*STDIN, *STDOUT, *STDERR);
1N/A print $ofh "Type it: ";
1N/A $got = <$ifh>
1N/A print $efh "What was that: $got";
1N/A
1N/AIf you're passing a filehandle to a function, you can write
1N/Athe function in two ways:
1N/A
1N/A sub accept_fh {
1N/A my $fh = shift;
1N/A print $fh "Sending to indirect filehandle\n";
1N/A }
1N/A
1N/AOr it can localize a typeglob and use the filehandle directly:
1N/A
1N/A sub accept_fh {
1N/A local *FH = shift;
1N/A print FH "Sending to localized filehandle\n";
1N/A }
1N/A
1N/ABoth styles work with either objects or typeglobs of real filehandles.
1N/A(They might also work with strings under some circumstances, but this
1N/Ais risky.)
1N/A
1N/A accept_fh(*STDOUT);
1N/A accept_fh($handle);
1N/A
1N/AIn the examples above, we assigned the filehandle to a scalar variable
1N/Abefore using it. That is because only simple scalar variables, not
1N/Aexpressions or subscripts of hashes or arrays, can be used with
1N/Abuilt-ins like C<print>, C<printf>, or the diamond operator. Using
1N/Asomething other than a simple scalar variable as a filehandle is
1N/Aillegal and won't even compile:
1N/A
1N/A @fd = (*STDIN, *STDOUT, *STDERR);
1N/A print $fd[1] "Type it: "; # WRONG
1N/A $got = <$fd[0]> # WRONG
1N/A print $fd[2] "What was that: $got"; # WRONG
1N/A
1N/AWith C<print> and C<printf>, you get around this by using a block and
1N/Aan expression where you would place the filehandle:
1N/A
1N/A print { $fd[1] } "funny stuff\n";
1N/A printf { $fd[1] } "Pity the poor %x.\n", 3_735_928_559;
1N/A # Pity the poor deadbeef.
1N/A
1N/AThat block is a proper block like any other, so you can put more
1N/Acomplicated code there. This sends the message out to one of two places:
1N/A
1N/A $ok = -x "/bin/cat";
1N/A print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
1N/A print { $fd[ 1+ ($ok || 0) ] } "cat stat $ok\n";
1N/A
1N/AThis approach of treating C<print> and C<printf> like object methods
1N/Acalls doesn't work for the diamond operator. That's because it's a
1N/Areal operator, not just a function with a comma-less argument. Assuming
1N/Ayou've been storing typeglobs in your structure as we did above, you
1N/Acan use the built-in function named C<readline> to read a record just
1N/Aas C<< <> >> does. Given the initialization shown above for @fd, this
1N/Awould work, but only because readline() requires a typeglob. It doesn't
1N/Awork with objects or strings, which might be a bug we haven't fixed yet.
1N/A
1N/A $got = readline($fd[0]);
1N/A
1N/ALet it be noted that the flakiness of indirect filehandles is not
1N/Arelated to whether they're strings, typeglobs, objects, or anything else.
1N/AIt's the syntax of the fundamental operators. Playing the object
1N/Agame doesn't help you at all here.
1N/A
1N/A=head2 How can I set up a footer format to be used with write()?
1N/A
1N/AThere's no builtin way to do this, but L<perlform> has a couple of
1N/Atechniques to make it possible for the intrepid hacker.
1N/A
1N/A=head2 How can I write() into a string?
1N/A
1N/ASee L<perlform/"Accessing Formatting Internals"> for an swrite() function.
1N/A
1N/A=head2 How can I output my numbers with commas added?
1N/A
1N/AThis subroutine will add commas to your number:
1N/A
1N/A sub commify {
1N/A local $_ = shift;
1N/A 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
1N/A return $_;
1N/A }
1N/A
1N/AThis regex from Benjamin Goldberg will add commas to numbers:
1N/A
1N/A s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
1N/A
1N/AIt is easier to see with comments:
1N/A
1N/A s/(
1N/A ^[-+]? # beginning of number.
1N/A \d{1,3}? # first digits before first comma
1N/A (?= # followed by, (but not included in the match) :
1N/A (?>(?:\d{3})+) # some positive multiple of three digits.
1N/A (?!\d) # an *exact* multiple, not x * 3 + 1 or whatever.
1N/A )
1N/A | # or:
1N/A \G\d{3} # after the last group, get three digits
1N/A (?=\d) # but they have to have more digits after them.
1N/A )/$1,/xg;
1N/A
1N/A=head2 How can I translate tildes (~) in a filename?
1N/A
1N/AUse the <> (glob()) operator, documented in L<perlfunc>. Older
1N/Aversions of Perl require that you have a shell installed that groks
1N/Atildes. Recent perl versions have this feature built in. The
1N/AFile::KGlob module (available from CPAN) gives more portable glob
1N/Afunctionality.
1N/A
1N/AWithin Perl, you may use this directly:
1N/A
1N/A $filename =~ s{
1N/A ^ ~ # find a leading tilde
1N/A ( # save this in $1
1N/A [^/] # a non-slash character
1N/A * # repeated 0 or more times (0 means me)
1N/A )
1N/A }{
1N/A $1
1N/A ? (getpwnam($1))[7]
1N/A : ( $ENV{HOME} || $ENV{LOGDIR} )
1N/A }ex;
1N/A
1N/A=head2 How come when I open a file read-write it wipes it out?
1N/A
1N/ABecause you're using something like this, which truncates the file and
1N/AI<then> gives you read-write access:
1N/A
1N/A open(FH, "+> /path/name"); # WRONG (almost always)
1N/A
1N/AWhoops. You should instead use this, which will fail if the file
1N/Adoesn't exist.
1N/A
1N/A open(FH, "+< /path/name"); # open for update
1N/A
1N/AUsing ">" always clobbers or creates. Using "<" never does
1N/Aeither. The "+" doesn't change this.
1N/A
1N/AHere are examples of many kinds of file opens. Those using sysopen()
1N/Aall assume
1N/A
1N/A use Fcntl;
1N/A
1N/ATo open file for reading:
1N/A
1N/A open(FH, "< $path") || die $!;
1N/A sysopen(FH, $path, O_RDONLY) || die $!;
1N/A
1N/ATo open file for writing, create new file if needed or else truncate old file:
1N/A
1N/A open(FH, "> $path") || die $!;
1N/A sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT) || die $!;
1N/A sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT, 0666) || die $!;
1N/A
1N/ATo open file for writing, create new file, file must not exist:
1N/A
1N/A sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) || die $!;
1N/A sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT, 0666) || die $!;
1N/A
1N/ATo open file for appending, create if necessary:
1N/A
1N/A open(FH, ">> $path") || die $!;
1N/A sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT) || die $!;
1N/A sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT, 0666) || die $!;
1N/A
1N/ATo open file for appending, file must exist:
1N/A
1N/A sysopen(FH, $path, O_WRONLY|O_APPEND) || die $!;
1N/A
1N/ATo open file for update, file must exist:
1N/A
1N/A open(FH, "+< $path") || die $!;
1N/A sysopen(FH, $path, O_RDWR) || die $!;
1N/A
1N/ATo open file for update, create file if necessary:
1N/A
1N/A sysopen(FH, $path, O_RDWR|O_CREAT) || die $!;
1N/A sysopen(FH, $path, O_RDWR|O_CREAT, 0666) || die $!;
1N/A
1N/ATo open file for update, file must not exist:
1N/A
1N/A sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT) || die $!;
1N/A sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT, 0666) || die $!;
1N/A
1N/ATo open a file without blocking, creating if necessary:
1N/A
1N/A sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT)
1N/A or die "can't open /foo/somefile: $!":
1N/A
1N/ABe warned that neither creation nor deletion of files is guaranteed to
1N/Abe an atomic operation over NFS. That is, two processes might both
1N/Asuccessfully create or unlink the same file! Therefore O_EXCL
1N/Aisn't as exclusive as you might wish.
1N/A
1N/ASee also the new L<perlopentut> if you have it (new for 5.6).
1N/A
1N/A=head2 Why do I sometimes get an "Argument list too long" when I use E<lt>*E<gt>?
1N/A
1N/AThe C<< <> >> operator performs a globbing operation (see above).
1N/AIn Perl versions earlier than v5.6.0, the internal glob() operator forks
1N/Acsh(1) to do the actual glob expansion, but
1N/Acsh can't handle more than 127 items and so gives the error message
1N/AC<Argument list too long>. People who installed tcsh as csh won't
1N/Ahave this problem, but their users may be surprised by it.
1N/A
1N/ATo get around this, either upgrade to Perl v5.6.0 or later, do the glob
1N/Ayourself with readdir() and patterns, or use a module like File::KGlob,
1N/Aone that doesn't use the shell to do globbing.
1N/A
1N/A=head2 Is there a leak/bug in glob()?
1N/A
1N/ADue to the current implementation on some operating systems, when you
1N/Ause the glob() function or its angle-bracket alias in a scalar
1N/Acontext, you may cause a memory leak and/or unpredictable behavior. It's
1N/Abest therefore to use glob() only in list context.
1N/A
1N/A=head2 How can I open a file with a leading ">" or trailing blanks?
1N/A
1N/ANormally perl ignores trailing blanks in filenames, and interprets
1N/Acertain leading characters (or a trailing "|") to mean something
1N/Aspecial.
1N/A
1N/AThe three argument form of open() lets you specify the mode
1N/Aseparately from the filename. The open() function treats
1N/Aspecial mode characters and whitespace in the filename as
1N/Aliterals
1N/A
1N/A open FILE, "<", " file "; # filename is " file "
1N/A open FILE, ">", ">file"; # filename is ">file"
1N/A
1N/AIt may be a lot clearer to use sysopen(), though:
1N/A
1N/A use Fcntl;
1N/A $badpath = "<<<something really wicked ";
1N/A sysopen (FH, $badpath, O_WRONLY | O_CREAT | O_TRUNC)
1N/A or die "can't open $badpath: $!";
1N/A
1N/A=head2 How can I reliably rename a file?
1N/A
1N/AIf your operating system supports a proper mv(1) utility or its
1N/Afunctional equivalent, this works:
1N/A
1N/A rename($old, $new) or system("mv", $old, $new);
1N/A
1N/AIt may be more portable to use the File::Copy module instead.
1N/AYou just copy to the new file to the new name (checking return
1N/Avalues), then delete the old one. This isn't really the same
1N/Asemantically as a rename(), which preserves meta-information like
1N/Apermissions, timestamps, inode info, etc.
1N/A
1N/ANewer versions of File::Copy export a move() function.
1N/A
1N/A=head2 How can I lock a file?
1N/A
1N/APerl's builtin flock() function (see L<perlfunc> for details) will call
1N/Aflock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004 and
1N/Alater), and lockf(3) if neither of the two previous system calls exists.
1N/AOn some systems, it may even use a different form of native locking.
1N/AHere are some gotchas with Perl's flock():
1N/A
1N/A=over 4
1N/A
1N/A=item 1
1N/A
1N/AProduces a fatal error if none of the three system calls (or their
1N/Aclose equivalent) exists.
1N/A
1N/A=item 2
1N/A
1N/Alockf(3) does not provide shared locking, and requires that the
1N/Afilehandle be open for writing (or appending, or read/writing).
1N/A
1N/A=item 3
1N/A
1N/ASome versions of flock() can't lock files over a network (e.g. on NFS file
1N/Asystems), so you'd need to force the use of fcntl(2) when you build Perl.
1N/ABut even this is dubious at best. See the flock entry of L<perlfunc>
1N/Aand the F<INSTALL> file in the source distribution for information on
1N/Abuilding Perl to do this.
1N/A
1N/ATwo potentially non-obvious but traditional flock semantics are that
1N/Ait waits indefinitely until the lock is granted, and that its locks are
1N/AI<merely advisory>. Such discretionary locks are more flexible, but
1N/Aoffer fewer guarantees. This means that files locked with flock() may
1N/Abe modified by programs that do not also use flock(). Cars that stop
1N/Afor red lights get on well with each other, but not with cars that don't
1N/Astop for red lights. See the perlport manpage, your port's specific
1N/Adocumentation, or your system-specific local manpages for details. It's
1N/Abest to assume traditional behavior if you're writing portable programs.
1N/A(If you're not, you should as always feel perfectly free to write
1N/Afor your own system's idiosyncrasies (sometimes called "features").
1N/ASlavish adherence to portability concerns shouldn't get in the way of
1N/Ayour getting your job done.)
1N/A
1N/AFor more information on file locking, see also
1N/AL<perlopentut/"File Locking"> if you have it (new for 5.6).
1N/A
1N/A=back
1N/A
1N/A=head2 Why can't I just open(FH, "E<gt>file.lock")?
1N/A
1N/AA common bit of code B<NOT TO USE> is this:
1N/A
1N/A sleep(3) while -e "file.lock"; # PLEASE DO NOT USE
1N/A open(LCK, "> file.lock"); # THIS BROKEN CODE
1N/A
1N/AThis is a classic race condition: you take two steps to do something
1N/Awhich must be done in one. That's why computer hardware provides an
1N/Aatomic test-and-set instruction. In theory, this "ought" to work:
1N/A
1N/A sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT)
1N/A or die "can't open file.lock: $!";
1N/A
1N/Aexcept that lamentably, file creation (and deletion) is not atomic
1N/Aover NFS, so this won't work (at least, not every time) over the net.
1N/AVarious schemes involving link() have been suggested, but
1N/Athese tend to involve busy-wait, which is also subdesirable.
1N/A
1N/A=head2 I still don't get locking. I just want to increment the number in the file. How can I do this?
1N/A
1N/ADidn't anyone ever tell you web-page hit counters were useless?
1N/AThey don't count number of hits, they're a waste of time, and they serve
1N/Aonly to stroke the writer's vanity. It's better to pick a random number;
1N/Athey're more realistic.
1N/A
1N/AAnyway, this is what you can do if you can't help yourself.
1N/A
1N/A use Fcntl qw(:DEFAULT :flock);
1N/A sysopen(FH, "numfile", O_RDWR|O_CREAT) or die "can't open numfile: $!";
1N/A flock(FH, LOCK_EX) or die "can't flock numfile: $!";
1N/A $num = <FH> || 0;
1N/A seek(FH, 0, 0) or die "can't rewind numfile: $!";
1N/A truncate(FH, 0) or die "can't truncate numfile: $!";
1N/A (print FH $num+1, "\n") or die "can't write numfile: $!";
1N/A close FH or die "can't close numfile: $!";
1N/A
1N/AHere's a much better web-page hit counter:
1N/A
1N/A $hits = int( (time() - 850_000_000) / rand(1_000) );
1N/A
1N/AIf the count doesn't impress your friends, then the code might. :-)
1N/A
1N/A=head2 All I want to do is append a small amount of text to the end of a file. Do I still have to use locking?
1N/A
1N/AIf you are on a system that correctly implements flock() and you use the
1N/Aexample appending code from "perldoc -f flock" everything will be OK
1N/Aeven if the OS you are on doesn't implement append mode correctly (if
1N/Asuch a system exists.) So if you are happy to restrict yourself to OSs
1N/Athat implement flock() (and that's not really much of a restriction)
1N/Athen that is what you should do.
1N/A
1N/AIf you know you are only going to use a system that does correctly
1N/Aimplement appending (i.e. not Win32) then you can omit the seek() from
1N/Athe above code.
1N/A
1N/AIf you know you are only writing code to run on an OS and filesystem that
1N/Adoes implement append mode correctly (a local filesystem on a modern
1N/AUnix for example), and you keep the file in block-buffered mode and you
1N/Awrite less than one buffer-full of output between each manual flushing
1N/Aof the buffer then each bufferload is almost guaranteed to be written to
1N/Athe end of the file in one chunk without getting intermingled with
1N/Aanyone else's output. You can also use the syswrite() function which is
1N/Asimply a wrapper around your systems write(2) system call.
1N/A
1N/AThere is still a small theoretical chance that a signal will interrupt
1N/Athe system level write() operation before completion. There is also a
1N/Apossibility that some STDIO implementations may call multiple system
1N/Alevel write()s even if the buffer was empty to start. There may be some
1N/Asystems where this probability is reduced to zero.
1N/A
1N/A=head2 How do I randomly update a binary file?
1N/A
1N/AIf you're just trying to patch a binary, in many cases something as
1N/Asimple as this works:
1N/A
1N/A perl -i -pe 's{window manager}{window mangler}g' /usr/bin/emacs
1N/A
1N/AHowever, if you have fixed sized records, then you might do something more
1N/Alike this:
1N/A
1N/A $RECSIZE = 220; # size of record, in bytes
1N/A $recno = 37; # which record to update
1N/A open(FH, "+<somewhere") || die "can't update somewhere: $!";
1N/A seek(FH, $recno * $RECSIZE, 0);
1N/A read(FH, $record, $RECSIZE) == $RECSIZE || die "can't read record $recno: $!";
1N/A # munge the record
1N/A seek(FH, -$RECSIZE, 1);
1N/A print FH $record;
1N/A close FH;
1N/A
1N/ALocking and error checking are left as an exercise for the reader.
1N/ADon't forget them or you'll be quite sorry.
1N/A
1N/A=head2 How do I get a file's timestamp in perl?
1N/A
1N/AIf you want to retrieve the time at which the file was last
1N/Aread, written, or had its meta-data (owner, etc) changed,
1N/Ayou use the B<-M>, B<-A>, or B<-C> file test operations as
1N/Adocumented in L<perlfunc>. These retrieve the age of the
1N/Afile (measured against the start-time of your program) in
1N/Adays as a floating point number. Some platforms may not have
1N/Aall of these times. See L<perlport> for details. To
1N/Aretrieve the "raw" time in seconds since the epoch, you
1N/Awould call the stat function, then use localtime(),
1N/Agmtime(), or POSIX::strftime() to convert this into
1N/Ahuman-readable form.
1N/A
1N/AHere's an example:
1N/A
1N/A $write_secs = (stat($file))[9];
1N/A printf "file %s updated at %s\n", $file,
1N/A scalar localtime($write_secs);
1N/A
1N/AIf you prefer something more legible, use the File::stat module
1N/A(part of the standard distribution in version 5.004 and later):
1N/A
1N/A # error checking left as an exercise for reader.
1N/A use File::stat;
1N/A use Time::localtime;
1N/A $date_string = ctime(stat($file)->mtime);
1N/A print "file $file updated at $date_string\n";
1N/A
1N/AThe POSIX::strftime() approach has the benefit of being,
1N/Ain theory, independent of the current locale. See L<perllocale>
1N/Afor details.
1N/A
1N/A=head2 How do I set a file's timestamp in perl?
1N/A
1N/AYou use the utime() function documented in L<perlfunc/utime>.
1N/ABy way of example, here's a little program that copies the
1N/Aread and write times from its first argument to all the rest
1N/Aof them.
1N/A
1N/A if (@ARGV < 2) {
1N/A die "usage: cptimes timestamp_file other_files ...\n";
1N/A }
1N/A $timestamp = shift;
1N/A ($atime, $mtime) = (stat($timestamp))[8,9];
1N/A utime $atime, $mtime, @ARGV;
1N/A
1N/AError checking is, as usual, left as an exercise for the reader.
1N/A
1N/ANote that utime() currently doesn't work correctly with Win95/NT
1N/Aports. A bug has been reported. Check it carefully before using
1N/Autime() on those platforms.
1N/A
1N/A=head2 How do I print to more than one file at once?
1N/A
1N/ATo connect one filehandle to several output filehandles,
1N/Ayou can use the IO::Tee or Tie::FileHandle::Multiplex modules.
1N/A
1N/AIf you only have to do this once, you can print individually
1N/Ato each filehandle.
1N/A
1N/A for $fh (FH1, FH2, FH3) { print $fh "whatever\n" }
1N/A
1N/A=head2 How can I read in an entire file all at once?
1N/A
1N/AYou can use the File::Slurp module to do it in one step.
1N/A
1N/A use File::Slurp;
1N/A
1N/A $all_of_it = read_file($filename); # entire file in scalar
1N/A @all_lines = read_file($filename); # one line perl element
1N/A
1N/AThe customary Perl approach for processing all the lines in a file is to
1N/Ado so one line at a time:
1N/A
1N/A open (INPUT, $file) || die "can't open $file: $!";
1N/A while (<INPUT>) {
1N/A chomp;
1N/A # do something with $_
1N/A }
1N/A close(INPUT) || die "can't close $file: $!";
1N/A
1N/AThis is tremendously more efficient than reading the entire file into
1N/Amemory as an array of lines and then processing it one element at a time,
1N/Awhich is often--if not almost always--the wrong approach. Whenever
1N/Ayou see someone do this:
1N/A
1N/A @lines = <INPUT>;
1N/A
1N/Ayou should think long and hard about why you need everything loaded at
1N/Aonce. It's just not a scalable solution. You might also find it more
1N/Afun to use the standard Tie::File module, or the DB_File module's
1N/A$DB_RECNO bindings, which allow you to tie an array to a file so that
1N/Aaccessing an element the array actually accesses the corresponding
1N/Aline in the file.
1N/A
1N/AYou can read the entire filehandle contents into a scalar.
1N/A
1N/A {
1N/A local(*INPUT, $/);
1N/A open (INPUT, $file) || die "can't open $file: $!";
1N/A $var = <INPUT>;
1N/A }
1N/A
1N/AThat temporarily undefs your record separator, and will automatically
1N/Aclose the file at block exit. If the file is already open, just use this:
1N/A
1N/A $var = do { local $/; <INPUT> };
1N/A
1N/AFor ordinary files you can also use the read function.
1N/A
1N/A read( INPUT, $var, -s INPUT );
1N/A
1N/AThe third argument tests the byte size of the data on the INPUT filehandle
1N/Aand reads that many bytes into the buffer $var.
1N/A
1N/A=head2 How can I read in a file by paragraphs?
1N/A
1N/AUse the C<$/> variable (see L<perlvar> for details). You can either
1N/Aset it to C<""> to eliminate empty paragraphs (C<"abc\n\n\n\ndef">,
1N/Afor instance, gets treated as two paragraphs and not three), or
1N/AC<"\n\n"> to accept empty paragraphs.
1N/A
1N/ANote that a blank line must have no blanks in it. Thus
1N/AS<C<"fred\n \nstuff\n\n">> is one paragraph, but C<"fred\n\nstuff\n\n"> is two.
1N/A
1N/A=head2 How can I read a single character from a file? From the keyboard?
1N/A
1N/AYou can use the builtin C<getc()> function for most filehandles, but
1N/Ait won't (easily) work on a terminal device. For STDIN, either use
1N/Athe Term::ReadKey module from CPAN or use the sample code in
1N/AL<perlfunc/getc>.
1N/A
1N/AIf your system supports the portable operating system programming
1N/Ainterface (POSIX), you can use the following code, which you'll note
1N/Aturns off echo processing as well.
1N/A
1N/A #!/usr/bin/perl -w
1N/A use strict;
1N/A $| = 1;
1N/A for (1..4) {
1N/A my $got;
1N/A print "gimme: ";
1N/A $got = getone();
1N/A print "--> $got\n";
1N/A }
1N/A exit;
1N/A
1N/A BEGIN {
1N/A use POSIX qw(:termios_h);
1N/A
1N/A my ($term, $oterm, $echo, $noecho, $fd_stdin);
1N/A
1N/A $fd_stdin = fileno(STDIN);
1N/A
1N/A $term = POSIX::Termios->new();
1N/A $term->getattr($fd_stdin);
1N/A $oterm = $term->getlflag();
1N/A
1N/A $echo = ECHO | ECHOK | ICANON;
1N/A $noecho = $oterm & ~$echo;
1N/A
1N/A sub cbreak {
1N/A $term->setlflag($noecho);
1N/A $term->setcc(VTIME, 1);
1N/A $term->setattr($fd_stdin, TCSANOW);
1N/A }
1N/A
1N/A sub cooked {
1N/A $term->setlflag($oterm);
1N/A $term->setcc(VTIME, 0);
1N/A $term->setattr($fd_stdin, TCSANOW);
1N/A }
1N/A
1N/A sub getone {
1N/A my $key = '';
1N/A cbreak();
1N/A sysread(STDIN, $key, 1);
1N/A cooked();
1N/A return $key;
1N/A }
1N/A
1N/A }
1N/A
1N/A END { cooked() }
1N/A
1N/AThe Term::ReadKey module from CPAN may be easier to use. Recent versions
1N/Ainclude also support for non-portable systems as well.
1N/A
1N/A use Term::ReadKey;
1N/A open(TTY, "</dev/tty");
1N/A print "Gimme a char: ";
1N/A ReadMode "raw";
1N/A $key = ReadKey 0, *TTY;
1N/A ReadMode "normal";
1N/A printf "\nYou said %s, char number %03d\n",
1N/A $key, ord $key;
1N/A
1N/A=head2 How can I tell whether there's a character waiting on a filehandle?
1N/A
1N/AThe very first thing you should do is look into getting the Term::ReadKey
1N/Aextension from CPAN. As we mentioned earlier, it now even has limited
1N/Asupport for non-portable (read: not open systems, closed, proprietary,
1N/Anot POSIX, not Unix, etc) systems.
1N/A
1N/AYou should also check out the Frequently Asked Questions list in
1N/Acomp.unix.* for things like this: the answer is essentially the same.
1N/AIt's very system dependent. Here's one solution that works on BSD
1N/Asystems:
1N/A
1N/A sub key_ready {
1N/A my($rin, $nfd);
1N/A vec($rin, fileno(STDIN), 1) = 1;
1N/A return $nfd = select($rin,undef,undef,0);
1N/A }
1N/A
1N/AIf you want to find out how many characters are waiting, there's
1N/Aalso the FIONREAD ioctl call to be looked at. The I<h2ph> tool that
1N/Acomes with Perl tries to convert C include files to Perl code, which
1N/Acan be C<require>d. FIONREAD ends up defined as a function in the
1N/AI<sys/ioctl.ph> file:
1N/A
1N/A require 'sys/ioctl.ph';
1N/A
1N/A $size = pack("L", 0);
1N/A ioctl(FH, FIONREAD(), $size) or die "Couldn't call ioctl: $!\n";
1N/A $size = unpack("L", $size);
1N/A
1N/AIf I<h2ph> wasn't installed or doesn't work for you, you can
1N/AI<grep> the include files by hand:
1N/A
1N/A % grep FIONREAD /usr/include/*/*
1N/A /usr/include/asm/ioctls.h:#define FIONREAD 0x541B
1N/A
1N/AOr write a small C program using the editor of champions:
1N/A
1N/A % cat > fionread.c
1N/A #include <sys/ioctl.h>
1N/A main() {
1N/A printf("%#08x\n", FIONREAD);
1N/A }
1N/A ^D
1N/A % cc -o fionread fionread.c
1N/A % ./fionread
1N/A 0x4004667f
1N/A
1N/AAnd then hard code it, leaving porting as an exercise to your successor.
1N/A
1N/A $FIONREAD = 0x4004667f; # XXX: opsys dependent
1N/A
1N/A $size = pack("L", 0);
1N/A ioctl(FH, $FIONREAD, $size) or die "Couldn't call ioctl: $!\n";
1N/A $size = unpack("L", $size);
1N/A
1N/AFIONREAD requires a filehandle connected to a stream, meaning that sockets,
1N/Apipes, and tty devices work, but I<not> files.
1N/A
1N/A=head2 How do I do a C<tail -f> in perl?
1N/A
1N/AFirst try
1N/A
1N/A seek(GWFILE, 0, 1);
1N/A
1N/AThe statement C<seek(GWFILE, 0, 1)> doesn't change the current position,
1N/Abut it does clear the end-of-file condition on the handle, so that the
1N/Anext <GWFILE> makes Perl try again to read something.
1N/A
1N/AIf that doesn't work (it relies on features of your stdio implementation),
1N/Athen you need something more like this:
1N/A
1N/A for (;;) {
1N/A for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE)) {
1N/A # search for some stuff and put it into files
1N/A }
1N/A # sleep for a while
1N/A seek(GWFILE, $curpos, 0); # seek to where we had been
1N/A }
1N/A
1N/AIf this still doesn't work, look into the POSIX module. POSIX defines
1N/Athe clearerr() method, which can remove the end of file condition on a
1N/Afilehandle. The method: read until end of file, clearerr(), read some
1N/Amore. Lather, rinse, repeat.
1N/A
1N/AThere's also a File::Tail module from CPAN.
1N/A
1N/A=head2 How do I dup() a filehandle in Perl?
1N/A
1N/AIf you check L<perlfunc/open>, you'll see that several of the ways
1N/Ato call open() should do the trick. For example:
1N/A
1N/A open(LOG, ">>/foo/logfile");
1N/A open(STDERR, ">&LOG");
1N/A
1N/AOr even with a literal numeric descriptor:
1N/A
1N/A $fd = $ENV{MHCONTEXTFD};
1N/A open(MHCONTEXT, "<&=$fd"); # like fdopen(3S)
1N/A
1N/ANote that "<&STDIN" makes a copy, but "<&=STDIN" make
1N/Aan alias. That means if you close an aliased handle, all
1N/Aaliases become inaccessible. This is not true with
1N/Aa copied one.
1N/A
1N/AError checking, as always, has been left as an exercise for the reader.
1N/A
1N/A=head2 How do I close a file descriptor by number?
1N/A
1N/AThis should rarely be necessary, as the Perl close() function is to be
1N/Aused for things that Perl opened itself, even if it was a dup of a
1N/Anumeric descriptor as with MHCONTEXT above. But if you really have
1N/Ato, you may be able to do this:
1N/A
1N/A require 'sys/syscall.ph';
1N/A $rc = syscall(&SYS_close, $fd + 0); # must force numeric
1N/A die "can't sysclose $fd: $!" unless $rc == -1;
1N/A
1N/AOr, just use the fdopen(3S) feature of open():
1N/A
1N/A {
1N/A local *F;
1N/A open F, "<&=$fd" or die "Cannot reopen fd=$fd: $!";
1N/A close F;
1N/A }
1N/A
1N/A=head2 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work?
1N/A
1N/AWhoops! You just put a tab and a formfeed into that filename!
1N/ARemember that within double quoted strings ("like\this"), the
1N/Abackslash is an escape character. The full list of these is in
1N/AL<perlop/Quote and Quote-like Operators>. Unsurprisingly, you don't
1N/Ahave a file called "c:(tab)emp(formfeed)oo" or
1N/A"c:(tab)emp(formfeed)oo.exe" on your legacy DOS filesystem.
1N/A
1N/AEither single-quote your strings, or (preferably) use forward slashes.
1N/ASince all DOS and Windows versions since something like MS-DOS 2.0 or so
1N/Ahave treated C</> and C<\> the same in a path, you might as well use the
1N/Aone that doesn't clash with Perl--or the POSIX shell, ANSI C and C++,
1N/Aawk, Tcl, Java, or Python, just to mention a few. POSIX paths
1N/Aare more portable, too.
1N/A
1N/A=head2 Why doesn't glob("*.*") get all the files?
1N/A
1N/ABecause even on non-Unix ports, Perl's glob function follows standard
1N/AUnix globbing semantics. You'll need C<glob("*")> to get all (non-hidden)
1N/Afiles. This makes glob() portable even to legacy systems. Your
1N/Aport may include proprietary globbing functions as well. Check its
1N/Adocumentation for details.
1N/A
1N/A=head2 Why does Perl let me delete read-only files? Why does C<-i> clobber protected files? Isn't this a bug in Perl?
1N/A
1N/AThis is elaborately and painstakingly described in the
1N/AF<file-dir-perms> article in the "Far More Than You Ever Wanted To
1N/AKnow" collection in http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz .
1N/A
1N/AThe executive summary: learn how your filesystem works. The
1N/Apermissions on a file say what can happen to the data in that file.
1N/AThe permissions on a directory say what can happen to the list of
1N/Afiles in that directory. If you delete a file, you're removing its
1N/Aname from the directory (so the operation depends on the permissions
1N/Aof the directory, not of the file). If you try to write to the file,
1N/Athe permissions of the file govern whether you're allowed to.
1N/A
1N/A=head2 How do I select a random line from a file?
1N/A
1N/AHere's an algorithm from the Camel Book:
1N/A
1N/A srand;
1N/A rand($.) < 1 && ($line = $_) while <>;
1N/A
1N/AThis has a significant advantage in space over reading the whole file
1N/Ain. You can find a proof of this method in I<The Art of Computer
1N/AProgramming>, Volume 2, Section 3.4.2, by Donald E. Knuth.
1N/A
1N/AYou can use the File::Random module which provides a function
1N/Afor that algorithm:
1N/A
1N/A use File::Random qw/random_line/;
1N/A my $line = random_line($filename);
1N/A
1N/AAnother way is to use the Tie::File module, which treats the entire
1N/Afile as an array. Simply access a random array element.
1N/A
1N/A=head2 Why do I get weird spaces when I print an array of lines?
1N/A
1N/ASaying
1N/A
1N/A print "@lines\n";
1N/A
1N/Ajoins together the elements of C<@lines> with a space between them.
1N/AIf C<@lines> were C<("little", "fluffy", "clouds")> then the above
1N/Astatement would print
1N/A
1N/A little fluffy clouds
1N/A
1N/Abut if each element of C<@lines> was a line of text, ending a newline
1N/Acharacter C<("little\n", "fluffy\n", "clouds\n")> then it would print:
1N/A
1N/A little
1N/A fluffy
1N/A clouds
1N/A
1N/AIf your array contains lines, just print them:
1N/A
1N/A print @lines;
1N/A
1N/A=head1 AUTHOR AND COPYRIGHT
1N/A
1N/ACopyright (c) 1997-2002 Tom Christiansen and Nathan Torkington.
1N/AAll rights reserved.
1N/A
1N/AThis documentation is free; you can redistribute it and/or modify it
1N/Aunder the same terms as Perl itself.
1N/A
1N/AIrrespective of its distribution, all code examples here are in the public
1N/Adomain. You are permitted and encouraged to use this code and any
1N/Aderivatives thereof in your own programs for fun or for profit as you
1N/Asee fit. A simple comment in the code giving credit to the FAQ would
1N/Abe courteous but is not required.