
rGeoffrey
User
Jul 18, 2000, 7:50 AM
Post #4 of 8
(892 views)
|
|
Re: Nobody can install this one!
[In reply to]
|
Can't Post
|
|
The first place to start is the lines with open. In some cases the code uses an unless block to print an error message on failure, and in other cases it does absolutely nothing (which is bad). Unless you have a compelling reason to be different, open should always be followed by 'or die'. If things work as they should, the open will do it's job, the or will short circuit, and the die will never happen. When things go badly, this will give some hints as to why. I also noticed that the four times the subroutine &afdrukken was called, it was always after the open, slurp file, close, and foreach lines. So I have provided a new subroutine &afdrukken_Wrapper to take the file name and do those lines. One potential problem with my code is I hold the files open until after the foreach loops. If the files could be in use by others this puts them at greater risk of strange things happening. But in most cases this is not an issue. I am too lazy to actually test it (but I did to a perl -c and the syntax check is good) so I don't know if I deserve the medal either. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #!/usr/local/bin/perl #Koen's Perl Individual Home Page v1.1 #Feel free to modify this script but leave my name and url in it #http://users.bart.nl/~kamphuys/software/individualhomepage.htm #home.pl - serves a homepage consisting of a default list or a configured #list, based on the existence of a cookie #Set snippet path - #######your input is required $path = 'http://questserver.hypermart.net/secure/cgi-bin/idv/snips'; $naam = "prefs"; @nvcookie = split(/;/, $ENV{'HTTP_COOKIE'}); foreach $nv (@nvcookie) { ($namecookie, $valuecookie) = split(/=/, $nv); $namecookie =~ s/ //g; $input{$namecookie} = $valuecookie; } sub afdrukken_Wrapper { my ($file) = @_; open (DATA, $file) or die "unable to open $file; $!\n"; foreach (<DATA> ) { afdrukken($_); } close DATA; } sub afdrukken { my ($regel1) = @_; if ($regel1 =~ /^\#LITERAL_CATYES/) { if ($input{$naam} =~ /cat/) { $regel1 =~ s/\#LITERAL_CATYES //; print $regel1; } } elsif ($regel1 =~ /^\#LITERAL_CATNO/) { if ($input{$naam} !~ /cat/) { $regel1 =~ s/\#LITERAL_CATNO //; print $regel1; } } else { print $regel1 if ($regel1 !~ /^\#/); } } #end sub afdrukken print "Content-Type: text/html\nPragma: no-cache\n\n"; &afdrukken_Wrapper ("$path/first.snp"); if ($input{$naam} !~ /cat/) { #er zijn geen categorieen gevraagd - default-instellingen worden opgehaald open (INVOER, "$path/default.cfg") or die "unable to open $path/default.cfg; $!\n"; foreach $regel (<INVOER> ) { if ($regel !~ /^\#/) { $regel =~ s/\n//; &afdrukken_Wrapper ("$path/$regel"); } } close INVOER; } else { open (INVOER, "$path/configurable.cfg") or die "unable to open $path/configurable.cfg; $!\n"; foreach $regel (<INVOER> ) { if (($regel !~ /^\#/) && ($regel =~ /^cat/i)) { ($catnummer, $catwaarde, $catomschrijving) = split (/ --> /, $regel); if ($input{$naam} =~ /$catnummer/) { &afdrukken_Wrapper ("$path/$catwaarde"); } } } close INVOER; } &afdrukken_Wrapper ("$path/last.snp"); </pre><HR></BLOCKQUOTE>
|