----- Original Message ----- From: "Kipp, James" <[EMAIL PROTECTED]> To: "'Todd W'" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Friday, October 25, 2002 12:13 PM Subject: RE: Splitting A large data file
> This is an interesting approach. Of course my Perl OOP skills have > atrophied, so I have a few questions if you don't mind. > > > > #!/usr/bin/perl -w > > > > use strict; > > > > # call new() with named args found in init() to override defaults > > my( $fSpliter ) = Text::FileSplitter->new(); > > Is Text:: a pre existing class (from CPAN or something) or are we > originating this class? > If this is our own new class, I should have $PERLIB\text\FileSplitter.pm ? The class name is just something I made up and I wrote the entire package myself. Usually what I do for general purpose modules I write is find the path to a directory called /site_perl in the @INC array. On my system its called /usr/lib/perl5/site_perl. I then put a directory called "My" in that directory. Then I put all the custom extension modules I write in there. There are a couple problems with this code, the largest one being if you run it from a directory that you do not have write permissions to, it will generate a ton of warnings about writing to an unopened filehandle. Thats because when we opened the output files for writing, we didnt check to see if the file actually opened. Make sure you read the inline comments below > Override defaults like this? > my( $fSpliter ) = Text::FileSplitter->new( file=>'split.txt', > record_length=>50 ); Yeah you got it. Below is what the driver file would look like: #!/usr/bin/perl -w use strict; use My::Text::FileSplitter; my($FSplitter) = My::Text::FileSplitter->new( file => 'split.txt', # override the default filename splitfile.txt output_prefix => 'split', # this would be the default if you omitted it file_count => 10, # split the file into 10 smaller files record_length => 1024, # the size in bytes of each record ); $FSplitter->split(); # actually split the file print( "Done!\n" ); # all done Everything below here is in the file called /usr/lib/perl5/site_perl/My/Text/FileSplitter.pm package My::Text::FileSplitter; # ^^^ note how I changed this. the rest is the same. use strict; use IO::File; sub new { my($class, %args) = @_; my($self) = bless( { %args }, $class ); $self->init(); return( $self ); } sub init { my($self) = shift(); my($filehandles) = []; $self->{ file } ||= './splitfile.txt'; $self->{ output_prefix } ||= ( ($self->{ file } =~ /(\w+)/) and $1 ); $self->{ file_count } ||= 5; $self->{ record_length } ||= 10; $self->{ fh } = IO::File->new( "< $self->{ file }" ) or die("open $self->{ file }: $!"); foreach ( 1 .. $self->{ file_count } ) { push( @{ $filehandles }, IO::File->new("> $self->{ output_prefix }.$_") ); } $self->{ ofh } = $filehandles; } sub split { my($self) = shift(); my($buffer); my($counter) = 0; while ( sysread $self->{ fh }, $buffer, $self->{ record_length } ) { $self->{ ofh }[ $counter % $self->{ file_count } ]->print( $buffer ); $counter++; } } 1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]