OT:Perl on Windows
Alex Beamish
talexb-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Fri Mar 11 17:37:48 UTC 2005
On Fri, 11 Mar 2005 12:19:19 -0500, William O'Higgins
<william.ohiggins-H217xnMUJC0sA/PxXw9srA at public.gmane.org> wrote:
> On Fri, Mar 11, 2005 at 11:42:52AM -0500, Alex Beamish wrote:
> >However, your comment that 'your program' (whatever that is) cannot
> >read the file, nor is it able to create an output file, suggests
> >something is not right. Can you explain?
>
> I'll try. Here's the example program:
>
> #/usr/bin/perl -w
>
> $input = $ARGV[0];
> $output = 'test.txt';
>
> open(TXT, "$input") or die "Cannot open $input for some reason, perhaps:
> $!\n"
>
> if (-e $output) {
> print "$output already exists, try a different name.\n";
> } else {
> open(OUT, ">$output") or die "Cannot open this file: $!\n";
> }
>
> print OUT "test";
>
> while (<TXT>) {
> $data .= $_;
> }
>
> print $data;
>
> print OUT $data;
>
> I get this error on run:
>
> syntax error ... near ") {"
>
> *and* test.txt doesn't get created, but there are no complaints.
> Something is wrong, but I'm just not sure what. Any suggestions?
Well, a couple. ;)
First of all, I highly recommend you purchase a good book about
learning Perl. O'Reilly has several excellent books, and one of them
is called Learning Perl. It'll give you a good basic understanding of
the language.
Second, I can recommend Perl Monks (perlmonks.org) as a great on-line
community. Fools are not always suffered gladly, so concentrate on
lots of reading and very little writing when you first arrive.
Third, always use 'strict' -- it will catch your errors at compile
time rather than at run time.
Fourth, when you report an error, a line number is always handy. It
turns out that you forgot a semi-colon at the end of one of your
lines. The fixed up program is as follows:
------------------------------
#/usr/bin/perl -w
# Usage: test.pl [input_file]
use strict;
my $input = shift;
my $output = 'test.txt';
my $data;
# Check that the output file exists, and exit if it does.
if (-e $output) {
print "$output already exists, try a different name.\n";
exit;
} else {
open(OUT, ">$output") or die "Cannot open this file: $!\n";
}
# Write out a line of test output to the output file.
print OUT "test\n";
# Open the input file.
open(TXT, "$input") or
die "Cannot open $input for some reason, perhaps:$!";
# Read the entire input file into a scalar.
while (<TXT>) {
$data .= $_;
}
# Dump the entire file (contained in the scalar) to STDOUT
print $data;
# Same thing, but dump file to the output file.
print OUT $data;
close ( OUT ) or
die "Unable to close output file: $!";
------------------------------
and when I run it ..
------------------------------
[alex at rand dev]$ perl -w tlug-mar11.pl wtime.sh
#!/bin/sh
while wtime.pl; do
sleep 120
done
[alex at rand dev]$ cat test.txt
test
#!/bin/sh
while wtime.pl; do
sleep 120
done
------------------------------
It works fine.
I won't go over all of the improvements .. just compare your code to
mine and see where I've made some changes.
Alex
--
The Toronto Linux Users Group. Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml
More information about the Legacy
mailing list