Quick Navigation Bar program control :: input/output and file i/o :: records and references [ toc | forums ] |
Note: If the document URL does not begin with https://randu.org/tutorials/perl/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
$input = <STDIN> @linput = <STDIN>where $input would store the input line. You can even store STDIN into a list as @linput line shows. You can continuously read in STDIN by:
while (<>) { print $_; }The above example shows the diamond operator in action. The diamond operator is the shorthand for <STDIN>, and we are printing from the scratch list @_.
sprintf
and
printf
in Perl to format your text! Very powerful!open(infile, "/some/directory/file.in"); open(outfile, ">/some/directory/file.out"); while (This block of code basically copies the contents of file.in into file.out line by line. Notice the > symbol to signify a write-clobber. You can use >> to append to a file. Don't forget to close your output file handles!) { print outfile "$_\n"; } close(outfile);
open(outf, ">>log.out"); flock(outf, 2); seek(outf, 0, 2); # ... some code close(outf);We are opening a log.out file for appending, and immediately lock the file with 2, which is LOCK_EX for an exclusive lock. This is typically used for writing. You can flock files for reading using 0 for LOCK_SH or a shared lock. The
seek
seeks to
the end of the file if there was writing done to the log.out before
the flock was called. seek
takes in the filehandle,
offset, and whence arguments. 0 is the beginning of the file, 1
is the current position in the file, 2 is for the end of the file.
Note that closing any filehandles automatically frees all file
locks.die
. For example:
open (inf, "/some/directory/file.in") || die "cannot open: $!";So what does this do? Perl tries to open file.in OR it calls die with the string. The
$!
contains the most
recent system error, so it will append a useful tag to the output
of die. You could even make a dienice subroutine that could be more
helpful. You can exit a Perl program immediately by calling
exit;
.||
as
a control structure! It's basically saying open this file or
die! You can even use &&
.$filename = "hooray.index"; if (-e $filename) { open(outf, ">>hooray.index"); } else { open(outf, ">>hooray-hooray.index"); }
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat ($filename);The stat function provides all these fields about the particular file $filename. Refer to Wall Pgs. 800-801 for more information about
stat
. You can also get information about a file
using the File::stat package:
use File::stat; $sb = stat($filename); print "$sb->size\n";This would print the size of the file. More on File::stat in the Larry Wall book.
@ARGV
.use Getopt::Std; getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect. getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts getopts('oif:'); # -o & -i are boolean flags, -f takes an argument # Sets opt_* as a side effect. getopts('oif:', \%opts); # options as above. Values in %optsThis example is taken from the man pages. getopt works much in the same way as getopt in C, but simpler. In the last example,
getopts
will take in -o, -i, -f with -f having arguments.
The hash %opts
contains the arguments, with the switches
as the keys for the hash.use
keyword followed by the module.
Notice: Please do not replicate or copy these pages and
host them elsewhere. This is to ensure that the latest version can always
be found here.
Disclaimer: The document author has published these pages
with the hope that it may be useful to others. However, the document
author does not guarantee that all information contained on these
webpages are correct or accurate. There is no warranty, expressed or
implied, of merchantability or fitness for any purpose. The author does
not assume any liability or responsibility for the use of the information
contained on these webpages.
If you see an error, please send an email to the address below indicating
the error. Your feedback is greatly appreciated and will help to
continually improve these pages.