jjohhn has asked for the wisdom of the Perl Monks concerning the following question:

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 ..."

Replies are listed 'Best First'.
Re: dynamic filenames, indirect filehandles
by rupesh (Hermit) on May 19, 2005 at 04:07 UTC
    IMHO, I'm not sure what you're trying to achieve here, but here are a few pointers:

    • Always  use strict;
    • Why confuse yourself with the ARGuments? Why not use $ARGV[0], $ARGV[1]..etc?
    • open FH, '<', '<path of file>';
    • Why are you appending a file handle ($outfile)with  $sl ?
    • Indent


    Update:Use split;

    Cheers,
    Rupesh.
      Thanks rupesh
      I don't mean to append the filehandle, I mean to append the name ("path") of the file. Incrementing the name is just what I want to achieve. This is the core of my question: For a 20,000 line $readfile, I want to produce 20 separate $outfiles, files, named (for example) "outa", "outb", etc.
      Why are you appending a file handle ($outfile)with $sl ?

      So that he can say $outfile++ and generate unique file names for each split file.

      Lou

Re: dynamic filenames, indirect filehandles
by jdporter (Paladin) on May 19, 2005 at 04:14 UTC
    if($ARGV[0] =~/^[0-9]+/){
    I think that should have a dash in it:
    if($ARGV[0] =~/^-[0-9]+/){
      I tried that; my program print the error:
      C:\scripts>perl split-unix.pl -100 tinysat tmp Can't open -100: No such file or directory at split-unix.pl line 27.
      <-- contents of the file-->
      Can't open tmp: No such file or directory at split-unix.pl line 28, <> line 16.
      It also writes an empty file "tmpa".
        The problem is solved; thanks for your patience with my ill-formed question that turned out not to be the problem at all.
        # split-unix.pl -- do split in perl # # usage:split [-num] file [outname] use strict; my $outfile = "x"; #default my $limit = 1000; my $readfile; my $s1= "a"; if($ARGV[0] =~/^-?[0-9]+/){ $limit = 0- $ARGV[0]; shift @ARGV; } $readfile = $ARGV[0]; shift @ARGV; if (defined $ARGV[0]){ $outfile = $ARGV[0]; } $outfile.=$s1; open (OUT, ">", $outfile) or die "$0: can't open $outfile $!"; open (IN, "<", $readfile) or die "$0: can't open $readfile $!"; print OUT $limit; #=pod while(<IN>){ if ($. > $limit ){ close OUT; $outfile++; open(OUT, ">", $outfile) or die "$0: can't open $outfile $!"; } print OUT "$_"; } #=cut close OUT;