package magic_columator; use strict; use Carp; our $TABS_MODE = undef; 1; sub test_fetchrow { my $this = shift; my @row = $this->fetchrow; print Dumper([$this->{format}, @row]), "\n"; } sub fetchrow { my $this = shift; my $fh = $this->{IN}; if( my $line = <$fh> ) { my @a = (); if( $TABS_MODE ) { @a = split(/$TABS_MODE/, $line); } else { @a = unpack($this->{format}, $line); } s/^\s+// for @a; s/\s+$// for @a; return @a; } return (); } sub new { my $class = shift; my $this = bless {}, $class; my $file = shift; croak "couldn't find file \"$file\"" unless -f $file; $this->{file} = $file; $this->find_seps; open $this->{IN}, $this->{file}; return $this; } sub find_seps { my $this = shift; open IN, $this->{file} or die "couldn't open $this->{file} for read: $!"; my @spaces = (); while() { my $x = 0; while( m/(.)/g ) { my $c = $1; $x ++; push @spaces, 1 while @spaces < $x; $spaces[$x-1] = 0 unless $c eq ' '; } } close IN; $this->{format} = ""; my $x = 0; for my $c (@spaces) { $x ++; if( $c ) { if( $x>1 ) { $this->{format} .= "A$x"; $x = 0; } $this->{format} .= " "; } } if( $x ) { $this->{format} .= "A$x"; } } #### use magic_columator; while(my @row = $col->fetchrow) { # do stuff }