Leon wrote:
> 
> Could somebody help me to shorten the working script I wrote marked script 1
> below, as I realised that I could not do the following :-
> ------
> open FILE, 'cookie.txt' or die "$!\n";
> open SAME_FILE, '>cookie.txt' or die "$!\n";
> while (<FILE>) {
>          print SAME_FILE;
> };
> ------
> Since I cannot do the above so basically this is what I did :-
> My question : Is this the best and only way?
> 
> open FILE, 'cookie.txt' or die "$!\n";
> open FILE2, '>cookie1.txt' or die "$!\n";
> print FILE2 while <FILE>;
> 
> open FILE2, 'cookie1.txt' or die "$!\n";
> open FILE, '>cookie.txt' or die "$!\n";
> print FILE while <FILE2>;
> ------
> 
> What I wanted to do is :-
> while (<#reading a file lets say 'cookie.txt'>) {
>        if (the test is true) {
>            change the default $_;
>            print the default $_ into the same file 'cookie.txt';
>            $switch = 1;
>        }else {
>             print the default $_ into the same file 'cookie.txt';
>        };
> );
> if ($switch != 1) {
>     print to the file 'cookie.txt' all the things here;
> }; # end of what I wanted to do.
> 
> --- working script 1 ----
> -- how do I shorten this script ----
> 
> [snip code]


#!d:\perl\bin\perl.exe -w
use strict;
use CGI::Carp qw(fatalsToBrowser);

print "content-type: text/plain\n\n";
my %FORM = ( membername => 'dindy' );

# initialization
my $id = 123456;

{ # limit these changes to a small scope
local $^I = '.bak';  # back-up cookie.txt to cookie.txt.bak
local @ARGV = 'cookie.txt';

while ( <> ) {
    if ( / $FORM{'membername'}$/ ) {
        s/^\S+ /$id /;
        }
    print;
    }
} # end of limited scope

__END__


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to