wiz has asked for the wisdom of the Perl Monks concerning the following question:

My problem lies in this code. It seems to work fine on the command prompt, but once on my server, it doesn't seem to work. It gives no output when you search.

#!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(), start_html("Street Print CD Catalog"), h1("StreetPrint CD Catalog"); ## if the script has been run if (param()) { ##get the color chomp(my $color = param("color")); ## get the pattern chomp(my $pattern = param("pattern")); ## show the 2 received variables print p($color, br(), $pattern); my (%dbcolor, %dbpattern, %dbcd, @name); ## open my DB file open (DBFILE, "form.db") or die "$!"; my $i = "0"; while ($name[$i] = <DBFILE>) { ## put the first line of 4 in name $dbcolor{$name[$i]} = <DBFILE>; ## put the second line of 4 in $ +dbcolor, referenced with the name ## of the picture $dbpattern{$name[$i]}= <DBFILE>;## put the third line of 4 in $d +bpattern, referenced with the name ## of the picture $dbcd{$name[$i]}= <DBFILE>; ## get the cd number of the picture $i++; } close (DBFILE); foreach (@name) { ## if either color or pattern match on the file, show all results that + do if (($dbcolor{$_} eq $color) or ($dbpattern{$_} eq $pattern)) { print p($_, br(), $dbcolor{$_}, br(), $dbpattern{$_}, br(), $d +bcd{$_}, br()); } } } else { print start_form();## create the form print p("What Color do you want? ", popup_menu("color", ["Any", 'Brick +', 'Terracotta','Burnt Sienna','Hunter Green','Safety Blue','White', +'Bedrock','Slate','Sierra'])); print p("What Pattern do you want? ", popup_menu("pattern", ["Any", 'O +ffset Brick','Herringbone','Herringbone Diagonal','Ashlar Slate','Bri +tish Cobble','Frisco Cobble','Random Stone','Eurofan'])); print p(submit("Find"), reset("Reset")); print end_form; } print end_html;

Now the file that is being run here and the DB file is being run here.



----------------------------
Wiz, The VooDoo Doll
Head Master of 12:30 Productions
----------------------------

Replies are listed 'Best First'.
Re: CGI, Newlines, and If Statements
by arturo (Vicar) on Jul 18, 2001 at 22:46 UTC

    One suggestion of a general nature I have is to make your matches more tolerant, or provide a list of 'approved' things to search on (in the form of a drop-down list). Don't test for strict equality, test for (case-insensitive) patterns:

    foreach my $name (@names) { if ($dbpattern{$name} =~ /$pattern/i or $dbcolor{$name} =~ /$color/ +i) { # print out the record } }

    That deals with issues like funky newline problems, etc. (which this version of your code doesn't deal with, although I recall you had put some code in at some point). Although, as I recommend below, you should change the way you store your data, you'll need to chomp each variable:

    $i =0; while (my $name[$i] = <DBFILE>) { chomp $name[$i]; my $color = <DBFILE>; my $pattern = <DBFILE>; my $dbcd = <DBFILE>; chomp ($color, $pattern, $dbcd); #yep, all at once! # now go on as before ... $i++; }

    I'd also recommend you change the way the information is stored in your flat file; multiline records are relatively more error-prone than single-line records (e.g. what if you accidentally leave out a single line in the file somewhere in the middle? Every record below that will be messed up). Your code could be made more compact if you stored the information as, say, pipe-separated records (just for example: pick a character you know won't end up in the data).

    superpicture|TerraCotta|Offset Brick|5

    You'd read in a record using something as simple as a split:

    chomp $line; my @record = split /\|/, $line, 4; # $record[0] is name, 1 => color 2 => pattern, etc.

    and you woulnd't have the problem that a messed up line would mess up everything afterwards.

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: CGI, Newlines, and If Statements
by rucker (Scribe) on Jul 18, 2001 at 21:50 UTC
    Is anything in your suexec_log or error_log?
Re: CGI, Newlines, and If Statements
by wiz (Scribe) on Jul 18, 2001 at 23:09 UTC
    This code works! *does a little dance* thanks arturo
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); print header(), start_html("Street Print CD Catalog"), h1("StreetPrint CD Catalog"); if (param()) { chomp(my $color = param("color")); chomp(my $pattern = param("pattern")); print p($color, br(), $pattern); open (DBFILE, "<form.db") or die "$!"; while (my $line = <DBFILE>) { chomp $line; my @record = split /\|/, $line, 4; # $record[0] is name, 1 => color 2 => pattern, etc. if ($record[1] =~ /$color/i or $record[2] =~ /$pattern/i) { print p($record[0], br(), $record[1], br(), $record[2], br(), $rec +ord[3]); } } close (DBFILE); } else { print start_form(); print p("What Color do you want? ", popup_menu("color", ["Any", 'Brick +', 'Terracotta','Burnt Sienna','Hunter Green','Safety Blue','White', +'Bedrock','Slate','Sierra'])); print p("What Pattern do you want? ", popup_menu("pattern", ["Any", 'O +ffset Brick','Herringbone','Herringbone Diagonal','Ashlar Slate','Bri +tish Cobble','Frisco Cobble','Random Stone','Eurofan'])); print p(submit("Find"), reset("Reset")); print end_form; } print end_html;

    ----------------------------
    Wiz, The VooDoo Doll
    Head Master of 12:30 Productions
    ----------------------------
Re: CGI, Newlines, and If Statements
by rrwo (Friar) on Jul 19, 2001 at 01:24 UTC

    (Misreading of your code deleted)

    Brief comment: If you don't indent your code, typos that lead to scope errors will be easily missed. Or misread.

Re: CGI, Newlines, and If Statements
by wiz (Scribe) on Jul 18, 2001 at 21:58 UTC
    The code runs fine, it gives no errors it just doesn't search properly, therefore if i choose Terracotta or Offset Brick or both, it doesn't show the search.
    ----------------------------
    Wiz, The VooDoo Doll
    Head Master of 12:30 Productions
    ----------------------------
      It looks like you're missing some chomps in the while loop.