Joshua Colson wrote:
>
> I have this code that is acting strange... does anyone know why.
>
> open( CONFIG, "+<config.file" ) or die "Cannot open config: $!";
>
> while( <CONFIG> ) {
> print;
> print CONFIG;
> }
>
> close( CONFIG );
>
> The first time I run the script, it reads the file, which is one line, and
> duplicates that line.
> Now, correct me if I'm wrong, but I would think that each time this is run,
> it would double the size of the file. But it doesn't, it just reads the
> first line, prints it to STDOUT, but not the file again, and exits. I have
> the sticky bit set on the script, and I don't know if that is what is
> causing it. Any Ideas?
Each time you read and write to a file the current position in the file
changes (perldoc -f seek perldoc -f tell) When you have a one line file
the current position points to the end of that line and the second line
is output there. If you have a two line file the first line is read in,
the pointer moves to the end of that line and the second line is
over-written.
#!/usr/bin/perl -w
use strict;
use Fcntl ':seek';
open CONFIG, '+<config.file' or die "Cannot open config: $!";
my @lines = <CONFIG>;
seek CONFIG, 0, SEEK_SET;
print CONFIG $_, $_ for @lines;
close CONFIG;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]