Esteemed Monks,

I have run into a perplexing problem that I hope someone can explain to me. On page 82 of the camel, it states that using a null filehandle (i.e. while (<>){do something}) will evaluate the contents of @ARGV as a list of filenames, or default to STDIN if there are no values in @ARGV.

I am writing a script that takes a filename to process on the command line during testing, and takes input from STDIN in production. This seems like the pefect time to take advantage of the behavior of the null filehandle. However, upon making the changes in my code to take advantage of this, my output changed drastically. Running on my laptop, using AS Perl v5.8.0, I am short ~10 lines. Running in the production environment, Perl 5.6.0 on Solaris, I am missing ~75 lines. When I change the code back to use explicit file handles, it works like a charm.

I also wrote a little sub, as you will see in my code, that prints each line processed to the console as well as to the output file. All lines are output correctly to the console window, but not to my output file.

I am posting my code below.
#!/usr/bin/perl use strict; use warnings; #since first page gets treated like the end of a chapter #we start with end_chapter being true. my $end_chapter = 1; #flags #for title my $title_fixed = 0; #for media type removal my $media_killed = 0; #time is used as filename to reduce possibility of files being overwri +tten. my $time = time(); #line required to redirect output to postprocessor (ie perfectbinder) my $to_pp = "<</OutputType(postprocessor)>>setpagedevice \n"; #line to force chapterization my $chapter = "true [] /110pProcs /ProcSet findresource /setchapters g +et exec \n"; #file declarations #my $outfile = "/var/spool/drop_box/autoq/$time.print"; #my $outfile = "./outputfile"; my $outfile = "c:/Quint.out";#only used for testing. uncomment previou +s line for production #my $filearg = $ARGV[0];#only used for testing. in production file com +es on STDIN #alias $infile to STDIN - this line is used for production #my $infile = \*STDIN; open (OUT, ">".$outfile) or die "Can't create temp file!!!!!!!!! $!"; #open (my $infile, "<".$filearg); my $line; while ($line = <>){ #handle chapter endings - currently denoted by null OutputType $line = chapterize($line); #print "End Chapter Setting: $end_chapter\n"; if ($end_chapter) { printline($line); handleSeps(); } else { printline($line); } } sub del_KDKHost { #if this the KDKHost line, delete it my $line = shift; if ($line =~ m/^%KDKHost:/){ $line = ""; print "Killed KDKHost Line\n"; } return $line; } sub make_title { #take XRXtitle line and create standard PS Title comment my $line = shift; if ($line =~ m!^%XRXtitle:!){ my @parts = split (/: /, $line); my $title = pop (@parts); $title =~ s!(\r\n)!!; $line = "%%Title: ($title)\n"; } $title_fixed = 1; return $line; } sub chapterize { #if OutputType is null, this is the end of a book. #replace the OutputType line with the perfectbinder command my $line = shift; if ($line =~ m!^<</OutputType \(\)>>setpagedevice!) { $line = $chapter; $end_chapter = 1; } return $line; } sub handleSeps { #if we just made a chapter, or this is the beginning of the file #we have to make the next 2 pages come out of the top exit my $counter= 0; my $file = shift; my $line; #print "Entered HandleSeps routine \n"; #uncomment for debug while ($line = <>) { #if its the KDKHost line, delete it #we have to do it here because #it is always in the header #and the header is processed as part of the #chapterization process. $line = del_KDKHost($line); $line = make_title($line); #delete media calls. These will always be at top of file as we +ll unless ($media_killed){ $line = kill_media($line, "pinky", "UNIVERSALID"); } #if we have started a new page #increment page counter and if we are on the 3rd page #since chapter break, insert line for output to perfect binder if ($counter <= 3) { if ($line =~ m!%%BeginPageSetup!){ $counter++; if ($counter==3){ #print "Begin Chapter - $linecount \n"; $line .= "$to_pp"; $counter = 0; $end_chapter = 0; printline($line); return; } } #if it is the OutputType line for this page, change to top + output elsif ($line =~ m!<</OutputType\(Stacker\)>>setpagedevice! +){ #print "Stacker Line \n"; $line =~ s/Stacker/top/; } } printline($line); } } sub kill_media { #sub takes line and a list of paper types to remove #and kills unwanted media calls. #We do this because jobs come with DocumentMediaReuqired #set to all possible media types, even if they aren't #actually used in this document. my $line = shift; my $media; if ($line =~ m!^%%DocumentMedia:!){ $media_killed=1; printline($line); while ($line = <>){ foreach $media (@_) { if ($line =~ m!$media!){ $line = ""; print "Killed media type: $media\n"; last; } elsif ($line !~ m!^%%\+!){ print "Returning Line: $line\n"; return $line; } } printline($line); } } else { return $line; } } sub printline { my $out_line = shift; print OUT $out_line; #print "$out_line\n" }
Thanks very much for any insight,
digger

In reply to Using null filehandle by digger

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.