put open(STDOUT, ">>trans-d.html");

instead of open(STDOUT, ">trans-d.html");

Amit Kothari
Server Operation Group
Redwood City Project
Bridgeton
314-298-2017
----------------------------------------


-----Original Message-----
From: david [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 17, 2002 11:55 AM
To: [EMAIL PROTECTED]
Subject: Re: open() command and exiting script


Jordan C. wrote:

> Hello. I have a transliterator script, and I need some help with it.
> Source is at the bottom of this message. What I need help with in
> particular is the open() command. The two relevant lines of the script
> are: open(STDOUT, ">trans-d.html"); - and - print STDOUT "$transme\n";
My
> problem is that it only saves the most recent line in trans-d.html. I
have
> tried with .txt, but decided to go with HTML so that I can refresh the
> file easily. Can someone help? I want it to keep storing the commands
> until the script exits. That is another problem, I can't figure out
how to
> do an exit without breaking an infinite loop. What I want to do is
loop
> until the user types q at the prompt. Thanks for your time :-) -
Program
> source -
> #!H:/iperl/bin/perl
> for($transme=0;$transme<=1;) {
> open(STDOUT, ">trans-d.html");
> $transme = <STDIN>;
> $transme =~ tr/abcdefghijklmnopqrstuvwxyz/fhveabcdgijklmnopqrstuwxyz/;
> print STDERR "$transme\n";
> print STDOUT "$transme\n";
> }
> - EOF -

try something like:

#!/usr/bin/perl -w
use strict;

open(OUT,'>trans-d.html') || die $!;
while(1){
        my $transme = <STDIN>;
        last if($transme =~ /^q/i);
        $transme =~
tr/abcdefghijklmnopqrstuvwxyz/fhveabcdgijklmnopqrstuwxyz/;
        print STDERR "$transme\n";
        print OUT    "$transme\n";
}
close(OUT);

__END__

the problem with your script:

1. your open(STDOUT,'>trans-d.html') statement is inside the for() loop,

everytime Perl opens your trans-d.html file for writing, it will
truncate 
the whole file. that's why you will only see the last line being print
to 
it. you can move the open() statement outside of the loop (like what i
did) 
or you can open your file in append mode like open(FILE,'>>file')

2. use the 'last' statement to exit one level of loop.

3. don't use 'STDOUT' as your file handle unless you have a a good
reason.

david

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


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

Reply via email to