A total novice wishes to improve his grasp of Perl idioms. I've written a version of TAC from llama, using rob_au's notes about temporary files. I'm trying to do it without any slurping. Anyone like to critique this?
# This is TAC # reverse of cat # # Take a list of files as command line parameters # and display the files on STDOUT # last file first # displaying each file backwards # The files are concatenated in 'normal' order to a temporary file # which is then read and displayed in reverse # check the number of parameters.. my $argCount = $#ARGV+1; if ($argCount<2) { exit 0; } # open the temporary file use Fcntl; use POSIX; my $outputFile; do { $outputFile = tmpnam(); } until sysopen(OUTPUTFILE, $outputFile, O_RDWR|O_CREAT|O_EXCL, 0666); # for every file.. while (@ARGV) { # open it my $fileName = shift; open ( MYFILE, " $fileName" ) or die "Cant open file $fileName"; # and write each line to the temporary file.. foreach (<MYFILE>) { print OUTPUTFILE $_; } close MYFILE; print OUTPUTFILE "\n"; print "Done $fileName -\n"; } close OUTPUTFILE; # now read it in backwards.. my $inputFile = $outputFile; open ( INPUTFILE, "< $inputFile"); my $char; my $fromEnd=1; my $fileSize = (stat INPUTFILE)[7]; # we seek to a position a distance from the end, and read a single cha +racter # the distance from end increases till we hit the start while ($fromEnd<$fileSize+1) { seek INPUTFILE, -$fromEnd, 2; # last char read INPUTFILE, $char, 1; print $char; ++$fromEnd; } close INPUTFILE; unlink $inputFile;

In reply to Grade me! TAC by Frank_Zappa_lives

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.