in reply to Re: uninitialized values for arrays suddenly appear
in thread uninitialized values for arrays suddenly appear

OK here's the lot of it. Sorry if it is too big.
Thx for looking at it!
jg
#!/usr/local/bin/perl -w # Define Variables # $basedir = '/home/mysite/www/'; $baseurl = 'http://www.mysite.com/'; @files = ('*.htm','*.html','*/*.html'); $title = "The Silver Machine"; $title_url = 'http://mysite.com/'; $search_url = 'http://www.mysite.com/search.html'; # Done # ###################################################################### +######## # Parse Form Search Information &parse_form; # Get Files To Search Through &get_files; # Search the files &search; # Print Results of Search &return_html; sub parse_form { # Get the input read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } } sub get_files { chdir($basedir); foreach $file (@files) { $ls = `ls $file`; @ls = split(/\s+/,$ls); foreach $temp_file (@ls) { if (-d $file) { $filename = "$file$temp_file"; if (-T $filename) { push(@FILES,$filename); } } elsif (-T $temp_file) { push(@FILES,$temp_file); } } } } sub search { @terms = split(/\s+/, $FORM{'terms'}); foreach $FILE (@FILES) { open(FILE,"$FILE"); @LINES = <FILE>; close(FILE); $string = join(' ',@LINES); $string =~ s/\n//g; if ($FORM{'boolean'} eq 'AND') { foreach $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if (!($string =~ /$term/i)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } elsif ($FORM{'case'} eq 'Sensitive') { if (!($string =~ /$term/)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } } } elsif ($FORM{'boolean'} eq 'OR') { foreach $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if ($string =~ /$term/i) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } elsif ($FORM{'case'} eq 'Sensitive') { if ($string =~ /$term/) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } } } if ($string =~ /<title>(.*)<\/title>/i) { $titles{$FILE} = "$1"; } else { $titles{$FILE} = "$FILE"; } } } sub return_html { open(FH,"../top.html") || die "couldn't open file $!\n"; my @html=<FH>; close(FH); print @html; print "\n <center>\n <h1>Search Results</h1>\n </center>\n"; print "Here ya go, they're in no particular order:<p><hr size=7 wid +th=75%><p>\n"; print "<ul>\n"; foreach $key (keys %include) { if ($include{$key} eq 'yes') { print "<li class=bullets><span class=bullets><a href=\"$baseu +rl$key\">$titles{$key}</a></span></li>\n"; } } print "</ul>\n"; print "\n"; print "</ul><br><hr size=7 width=75%><P>\n"; print "<ul>\n<li class=bullets><span class=bullets><a href=\"$searc +h_url\">Back to Search Page</a></span></li>\n"; print "<li class=bullets><span class=bullets><a href=\"$title_url\" +>$title</a></span></li>\n"; print "</ul>\n"; print "<hr size=7 width=75%>\n"; print "</body>\n</html>\n"; }

Replies are listed 'Best First'.
(Ovid) Re(3): uninitialized values for arrays suddenly appear
by Ovid (Cardinal) on Sep 08, 2001 at 00:37 UTC

    The following is a quick and dirty reworking of your code to make it parse under strict and it also uses CGI.pm. It's not guaranteed to work, but if you can cobble it together, it's going to be much easier to maintain. In addition to the 'use CGI or die' post that was mentioned, you might want to read 'use strict' is not Perl to gain insight as to why I eliminated (most) of your global variables.

    #!/usr/local/bin/perl -w # Define Variables # use strict; use CGI qw/:standard/; my $basedir = '/home/mysite/www/'; my $baseurl = 'http://www.mysite.com/'; my @files = ('*.htm','*.html','*/*.html'); my $title = "The Silver Machine"; my $title_url = 'http://mysite.com/'; my $search_url = 'http://www.mysite.com/search.html'; # Done # ###################################################################### +######## # Parse Form Search Information # if it's a single form value, map the value as a scalar, other wise, +the value is # and arrayref my %FORM = map { $_, @{[ param($_) ]} > 1 ? [ param($_) ] : param($_) +} param(); # Get Files To Search Through my $files = &get_files; # Search the files my ( $include, $titles ) = search( $files ); # Print Results of Search return_html( $include, $titles ); sub get_files { my @FILES; chdir($basedir); foreach my $file (@files) { my $ls = `ls $file`; my @ls = split(/\s+/,$ls); foreach my $temp_file (@ls) { if (-d $file) { my $filename = "$file$temp_file"; if (-T $filename) { push(@FILES,$filename); } } elsif (-T $temp_file) { push(@FILES,$temp_file); } } } return \@FILES; } sub search { my @FILES = @{$_[0]}; my ( %include, %titles ); my @terms = split(/\s+/, $FORM{'terms'}); foreach my $FILE (@FILES) { my $string; open(FILE,"$FILE") or die "Can't open $FILE for reading: $!"; { local $/; $string = <FILE>; } close(FILE); $string =~ s/\n//g; if ($FORM{'boolean'} eq 'AND') { foreach my $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if (!($string =~ /$term/i)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } elsif ($FORM{'case'} eq 'Sensitive') { if (!($string =~ /$term/)) { $include{$FILE} = 'no'; last; } else { $include{$FILE} = 'yes'; } } } } elsif ($FORM{'boolean'} eq 'OR') { foreach my $term (@terms) { if ($FORM{'case'} eq 'Insensitive') { if ($string =~ /$term/i) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } elsif ($FORM{'case'} eq 'Sensitive') { if ($string =~ /$term/) { $include{$FILE} = 'yes'; last; } else { $include{$FILE} = 'no'; } } } } if ($string =~ /<title>(.*?)<\/title>/i) { $titles{$FILE} = "$1"; } else { $titles{$FILE} = "$FILE"; } } return ( \%include, \%titles ); } sub return_html { my %include = %{$_[0]}; my %titles = %{$_[1]}; open(FH,"../top.html") || die "couldn't open file $!\n"; my @html=<FH>; close(FH); print @html; print "\n <center>\n <h1>Search Results</h1>\n </center>\n"; print "Here ya go, they're in no particular order:<p><hr size=7 wid +th=75%><p>\n"; print "<ul>\n"; foreach my $key (keys %include) { if ($include{$key} eq 'yes') { print "<li class=bullets><span class=bullets><a href=\"$baseu +rl$key\">$titles{$key}</a></span></li>\n"; } } print "</ul>\n"; print "\n"; print "</ul><br><hr size=7 width=75%><P>\n"; print "<ul>\n<li class=bullets><span class=bullets><a href=\"$searc +h_url\">Back to Search Page</a></span></li>\n"; print "<li class=bullets><span class=bullets><a href=\"$title_url\" +>$title</a></span></li>\n"; print "</ul>\n"; print "<hr size=7 width=75%>\n"; print "</body>\n</html>\n"; }

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Yay, got it working!! The final bug was that when opening an html file for appending it had to be preceded by the content-type header so I added a line to print the header and now the client's menus and masthead image are beautifully incorporated and the results page looks just like his other pages! Thanks Ovid and mandog ande all the other very kind and helpful monks!!!
      peas
      jg
      Could someone explain to me how to track errors from a script on my server? The errors these scripts produce in local tests have to do with expecting form data and searching directories on my server. I assume I could rewrite the var values to search my desktop but I am entirely unsure if that would suffice. I believe I need to test in situ on my server, no?
      TIA
      jg
        If you're on a different machine and testing from the command line, then sure you'd have to create corresponding directories, and it may be worthwhile. You can supply the form parameters in several ways, IMO its easiest to put them in a file (makes them easy to change later) like this (often you can just cut n paste from the URL browser window):
        param1=value1 param2=value2 etc=etc
        You can even leave them as the actual query string joined by '&' or ';', but to me its easier to edit when you separate them as above. Then you run your script with the CGI debug parameter on with this file as STDIN:
        ./my_cgi_script <my_parameters
        To me its easier to get it working from the command line first, than constantly having to check the web logs (yes you can use FatalsToBrowser, but it doesn't help if your script dies before headers get output).
Re: Re: Re: uninitialized values for arrays suddenly appear
by runrig (Abbot) on Sep 08, 2001 at 00:13 UTC
    To reiterate what Ovid said: use CGI or die;.
    Don't use '$ENV{CONTENT_LENGTH}' and read and split to get your parameters.
    Do use the CGI param function (or method).
    Do use strict;
Re(3): uninitialized values for arrays suddenly appear
by dragonchild (Archbishop) on Sep 08, 2001 at 00:06 UTC
    1. Add use strict; to the top of your script, under the shebang line.
    2. Fix the errors that pop up.
    3. Fix the logic that depends on those errors.
    4. Come back to us if the error you originally posted about is still there..

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      OK I am using strict; and warnings; from now on. Under those conditions here is what happens- I get error #1 about 15 times because I am testing locally and the script is designed to search my site's files. Error #2 occurs for every var. What I guess I need to know now is how to record the errors the script produces when in situ on my server. Please advise me on how to do that, I assume that would need to be a sub in the script which writes the file for me? TIA
      jg
      error #1 "Variable "$FILE" is not imported at smappend.pl line 87."
      error #2 "Global symbol "$basedir" requires explicit package name at smappend.pl line 8."