in reply to Re^2: Overwriting temp file
in thread Overwriting temp file
I would but it's large, I'll modify the code in the post.
Well it doesn't have to be real live realsize data, it just has to match your regexes, ie
#!/usr/bin/perl -- #~ 2011-09-21-11:45:38PDT by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail Main( @ARGV ); exit( 0 ); sub Main { if ( @_ == 1 ) { NotDemoMeaningfulName( @_, 'temp', \&DiddleSomeTemp ); } else { Demo(); print '#' x 33, "\n", Usage(); } } ## end sub Main sub NotDemoMeaningfulName { my ( $inputFile, $outputFile, $callBack ) = @_; my $flag = 0; my $outMode = '+>'; open my ($inFh), '<', $inputFile; open my ($outFh), $outMode, $outputFile; while (<$inFh>) { if (/^#/) { if ( not $flag ) { $flag++; close $outFh; open $outFh, $outMode, $outputFile; } else { seek $outFh, 0, 0; $callBack->($outFh); close $outFh; open $outFh, $outMode, $outputFile; } ## end else [ if ( not $flag ) ] } else { print $outFh $_; } } ## end while (<$inFh>) close $inFh; close $outFh; } ## end sub NotDemoMeaningfulName sub Usage { <<"__USAGE__"; $0 $0 dataFile perl ${\__FILE__} perl ${\__FILE__} dataFile __USAGE__ } ## end sub Usage sub DiddleSomeTemp { warn "Hey diddle diddle @_\n"; my $f = shift; warn "#$_" while <$f>; } sub Demo { my ( $Input, $WantedOutput ) = DemoData(); my $Output; require Test::More; NotDemoMeaningfulName( $Input, \$Output, sub { Test::More::is( $Output, $WantedOutput->[0], ' NotDemoMeaningfulName Works Aas Designed' ); shift @$WantedOutput; }, ); Test::More::done_testing(); } ## end sub Demo sub DemoData { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=1 my $One = <<'__One__'; # A B C # D E F # __One__ my @Two = ( #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=2 <<'__Two__', A B C blah __Two__ #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=3 <<'__Two__', D E F blah __Two__ ); return \$One, \@Two; } ## end sub DemoData __END__ $ perl pm.927193.pl not ok 1 - NotDemoMeaningfulName Works Aas Designed # Failed test ' NotDemoMeaningfulName Works Aas Designed' # at pm.927193.pl line 76. # got: 'A # B # C # ' # expected: 'A # B # C blah # ' not ok 2 - NotDemoMeaningfulName Works Aas Designed # Failed test ' NotDemoMeaningfulName Works Aas Designed' # at pm.927193.pl line 76. # got: 'D # E # F # ' # expected: 'D # E # F blah # ' 1..2 ################################# pm.927193.pl pm.927193.pl dataFile perl pm.927193.pl perl pm.927193.pl dataFile # Looks like you failed 2 tests of 2.
As you can see, the logic is sound, temp gets overwritten, pretty much what you already have
Note the expected data doesn't match on purpose ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Overwriting temp file
by Jeri (Scribe) on Sep 21, 2011 at 20:30 UTC | |
by Anonymous Monk on Sep 21, 2011 at 22:40 UTC | |
|
Re^4: Overwriting temp file
by Jeri (Scribe) on Sep 21, 2011 at 20:04 UTC | |
by Khen1950fx (Canon) on Sep 21, 2011 at 22:57 UTC | |
|
Re^4: Overwriting temp file
by Jeri (Scribe) on Sep 22, 2011 at 14:07 UTC | |
by Anonymous Monk on Sep 22, 2011 at 14:21 UTC |