On Wed, 10 Dec 2003 00:51:29 -0000,
Lenny Challis <> wrote:
>
> I would love to accept any criticism's. It is one of my first programs, but
> don't miss anything. One thing I hate is getting into bad habits, so I would
> like to pick them up now.
>
> Anyway, here goes.
> Thanks alot,
> Lenny Challis.
>
> ___________________________
>
> #!/usr/bin/perl -w
>
> #this program takes in the input (html) file as command lines first argument
> and output cgi file as second
>
> use strict;
Warnings and strictures are good
>
> my $ofile = shift @ARGV;
> my $cfile = shift @ARGV; #get the two filenames
>
> open INPUT, "<", $ofile or die "Can't open $ofile: $!\n";
> open OUTPUT, ">", $cfile or die "Can't open $cfile: $!\n"; #open
> both files
Watch your line wrapping, I realise it's probably a posting to usenet
artifact, but even so some of the following lines are far too long, in
my opinion anyway.
I'd put single quotes aroung the < and >, since they don't need
double quote features. I'd leave off the \n on the die output so
that perl will add the line number information (which may of course
not be wanted in your case).
>
> print OUTPUT "\#!/usr/bin/perl\n\n";
> print OUTPUT "print \"Content-type:text/html\\n\\n\";\n"; #print
> the shebang and content tags
# isn't special and doesn't need to be escaped in a double quoted string.
Why no warnings and strictures in the generated code?
If you are putting "s in a string then it is usually best to use a different
quote character than ". Saves on the escaping, something like:
print OUTPUT 'print "Content-type:text/html\n\n";', "\n";
or if you really must have it as one string (but I'd rather avoid the
\\s):
print OUTPUT qq{print "Content-type:text/html\\n\\n";\n};
>
> while (<INPUT>)
> {
> chomp($_); #getting rid of the \n's makes the code
> alot more understandable
> $_ =~ s/"/\\"/g; #first, escape
> all quotes
If you are using $_ there is not need to mention it, just use
s/"/\\"/g, if you want to be explicit about the variable than don't
use $_, read into $line or something...
> $_ = "print \"" . $_ . "\\n\";\n"; #add print function
> and newlines to $_
> print OUTPUT $_; #add line to the output
> file
You seem to be producing an output file that is a CGI script that will
output the input file when run. However, if there are \ characters in the
input file things could go wrong.
Try running it on a file containing
\"
in it somewhere to see the problem.
And what about files containing things like:
Bananas only $1.99 a kilo, save $$. email:
You need to escape a lot more characters than just ".
Something like (completely untested):
s/\\/\\\\/g; #this must be done first, before we add \s
s/"/\\"/g;
s/\$/\\\$/g;
s/@/\\\@/g; # I prefer \@ in the pattern, but it isn't needed
s/\t/\\t/g;
s/\r/\\r/g;
print qq{print "$_\\n";\n};
> }
>
> print "Completed."; #let them know its
> done
You should close the OUTPUT file, and check for failure. After all
writes can actually fail (the disk might fill up, for example) and
it's good to check that the output file closed successfully (you could
check on all the print calls, but that would be ugly and unwarranted
in this case).
The loop could be replaced with:
print OUTPUT "print <<'HTML';\n";
print OUTPUT while (<INPUT>);
print OUTPUT "HTML\n";
Assuming the string 'HTML' isn't found on a line by itself in the input
and that the final line of input does end with a newline character.
But that probably misses the point of introducing some perl features.
As an aside, for those who might know:
Why is
print "@";
OK, but
print "$";
isn't?
To me the first should be illegal (but I'm clearly wrong).
--
Sam Holden