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

I'm an intermediate programmer, but new Perl programmer. I am trying to implement a very simple search mechanism from a website that would just search for a part number from a text file and return pertinent information in the file related to that number. I want to try implement this as a CGI script using Perl. Is this a reasonable thing to do in Perl - or would this be better handled with a different menthod?
  • Comment on Using Perl to Implement a simple Searchable Web Database

Replies are listed 'Best First'.
Re: Using Perl to Implement a simple Searchable Web Database
by cjf (Parson) on May 28, 2002 at 17:29 UTC
    Is this a reasonable thing to do in Perl

    Sounds very reasonable and perfectly suited to Perl, but we'll need a bit more background info. What 'pertinent information' are you looking for exactly? How is the data structured? etc.

    In addition, you might want to consider using an existing solution such as Perlfect Search.

      Sorry for the vagueness On the web server is a CSV Excel file, which is basically a spreadsheet that is readable as a text file. It seperates columns using a comma, and rows by going to the next line. The first element in each line is a unique part number. Information that follows it (seperated by commas) would be things like price, location, etc. The user on the website would type in a part number and the page would return the price, location, etc. information. This may seem like a very simple problem, but I have never used Perl in a practical application yet, so I'd like to use it here. Thanks, Doug
        There are modules available to parse CSV files. But you might want to consider preprocessing the file into a DBM file, or a database, depending on the size of the file.

        Abigail

        This should get you started...

        #!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; my $number_to_find = $q->param("number"); open DATA, "data.txt" or die "Can't open file: $!"; while (<DATA>) { chomp; my @fields = split /,/; if ($fields[0] eq $number_to_find) { print_to_browser(@fields); } } close DATA; sub print_to_browser { my @data = @_; print $q->header("text/html"), $q->start_html("Error"), $q->h1("Relevant Data"), $q->p(@data), $q->end_html; }

        That's definately not the prettiest code, but it should give you an idea of what you need to do. You'll probably also want to read some of the CGI.pm docs and do a search or two on cpan for "CSV" to find some relevant modules.

Re: Using Perl to Implement a simple Searchable Web Database
by Aristotle (Chancellor) on May 28, 2002 at 17:29 UTC
    Your description is rather vague in fact, but even if there's a better solution than Perl for you I'm certain you won't make a big mistake by using Perl for your task.

    Makeshifts last the longest.