I am rewriting the split utility in perl, mostly just to do it (plagiarizing a version in awk in the gawk manual).It requires creating new files to write to every thousand lines (or so) of input.

I have read the relevant passages in perl cookbook and learning perl, but I am unsure how to deal with the anonymous fileglob produced from an indirect filehandle. How do I close the file?

The code is unfinished, but you can see where I am unsure at the end of it:

# split-unix.pl -- do split in perl # # usage:split [-num] file [outname] my $outfile = "x"; #default my $limit = 1000; my $readfile; my $s1= "a"; if (scalar(@ARGV) > 3){ usage; } if($ARGV[0] =~/^[0-9]+/){ $limit = -$ARGV[0]; shift @ARGV; } $readfile = $ARGV[0]; shift @ARGV; if (defined $ARGV[0]){ $outfile = $ARG[0]; } $outfile.=$s1; open ($write, ">", $outfile) or die "$0: can't open $outfile $!"; open ($fh, "<", $readfile) or die "$0: can't open $readfile $!"; while(<>){ if ($. < $limit){ print; } else{ $outfile++; $write=""; # I think this answers my question open ($write, ">", $outfile) or die "$0: can't open $outfile $!"; } }
cleaned up the syntax errors, but now problem is error:"can't open -100 No file or directory ..."

In reply to dynamic filenames, indirect filehandles by jjohhn

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.