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

I am trying to create a webpage that will allow users to view data from a text file. The text files are named using this format: yymmdd_center.txt It correctly displays the print statement "Report for..." but it doesn't show me the text file. Here is the code...
#!/usr/bin/perl -w # # Retrieve form selections & display the results # use strict; use File::Find; use CGI; my $q = new CGI; my $request = $ENV{"Query String"}; # Assign input to variables my $start_month= $q->param('month'); my $start_day= $q->param('day'); my $start_year= $q->param('year'); my $start_hour= $q->param('hour'); my $start_min= $q->param('min'); my $test_site= $q->param('center'); my $string="0"; print $q->header("text/html\n\n"), $q->start_html(-title=>"Data Report"), "Report for $test_site from $start_hour:$start_min $start_month/$s +tart_day/$start_year.\n"; #Find the file (yymmdd_site format) undef$/; find (sub { return if($_ =~ /^\./); return unless($_ =~ /\.txt/i); stat $Find::File::name; return if -d; return unless -r; open(FILE, "<$Find::file::name") or return; $string = <FILE>; close(FILE); return unless($string =~ /\$start_year$start_month$start_day$t +est_site\E/i); print $string; }, '/(name of my directory tree) $q->end_html;

Replies are listed 'Best First'.
Re: Trouble finding/reading from text file
by jethro (Monsignor) on Jun 30, 2008 at 17:58 UTC
    Note that nothing is printed unless your regex is true. Obvious conclusion, your regex doesn't return true. To test that you could do one or both of the following easy things:

    a) copy the regex into a test script and try to match a line it should be matching. Try to experiment with the regex and the line until you find out what is matched and how it should be changed to match the correct line.

    b) insert print statements into your code to show you on the web page what the variables $start_year ... $test_site and $string contain (i.e. print "start_year=$start_year:test_site=$test_site:...\n";. The ':' is there to show you where the strings end). Compare that with what you expected the variables to contain.

    PS: Please post your code using <code>-tags. The code as you posted it doesn't run without syntax errors, probably a problem of copying to this website and using the wrong tag. "my request" should be "my $request". On the second to last line the line seems to be cut off at the end. Similar the "Report for..." line is missing a ';'

Re: Trouble finding/reading from text file
by pc88mxer (Vicar) on Jun 30, 2008 at 17:06 UTC
    (Disregard what I said about only reading the first line -- missed your undef $/)

    Update: Also noticed something strange with your regular expression:

    ... /\$start_year$start_month$start_day$test_site\E/i); ^^? ^^?
    Perhaps you are missing a Q after the first slash.