On Sunday 19 December 2010 22:23:15 Bill Casey wrote:
> Hi
> 
> Below is a Perl script which won't compile.  My operating system is
> Windows XP Home and Perl version is 5.10.1.3.
> 

This script after I placed it in a file compiles fine here:

{{{
shlomif:~$ cat test.pl
#!/usr/bin/perl
# import_track.prl
use warnings;
use strict;

open (INFILE, "track.txt") or die $!;
open (OUTFILE, ">outtrack.plt") or die $!;

my $outstring;

while (<INFILE>)
    {
    $outstring = (" " . substr($_,0,10) . ", " . substr($_,14,10) + ",0, 
" . substr($_,28,6));
    print OUTFILE "$outstring\n";
    }

close (INFILE);
close (OUTFILE);
shlomif:~$ perl -c test.pl
test.pl syntax OK
}}}

perl-5.12.2-5mdv2011.0.i586 on Mandriva Linux Cooker, but should work fine 
with perls back to at least 5.6.x. A few notes on it, though:

> #!/usr/bin/perl
> # import_track.prl
> use warnings;
> use strict;
> 
> open (INFILE, "track.txt") or die $!;
> open (OUTFILE, ">outtrack.plt") or die $!;
> 

Use three-args open and lexical file handles:

http://perl-begin.org/tutorials/bad-elements/#open-function-style

> my $outstring;
> 

You should declare $outstring inside the loop, or maybe get rid of it 
completely and just output the expression.

> while (<INFILE>)

Using an explicit assigning «my $line = <$in_fh>» would be better here.

>     {
>     $outstring = (" " . substr($_,0,10) . ", " . substr($_,14,10) + ",0,
> " . substr($_,28,6));

You want the "." operator instead of the "+" operator and you should cut
the line into two lines in the middle.

>     print OUTFILE "$outstring\n";


This should be:

print {$out_fh} "$outstring\n";

With the curlies. See: 

http://perl-begin.org/tutorials/bad-elements/#print_to_fh

>     }
> 
> close (INFILE);
> close (OUTFILE);
> 
> When I attempt to compile I get the error messages:
> Syntax error at import_track.pl line 11, near ")   {"
> Syntax error at import_track.pl line 14, near "}"
> This appears to me to be related to the "{" and "}" but I am a beginner!
> 

Welcome aboard! See the resources on http://perl-begin.org/ .

Regards,

        Shlomi Fish

> I would appreciate help on this.
> 
> Thanks
> Bill Casey

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

Chuck Norris can make the statement "This statement is false" a true one.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to