in reply to Print contents of a range of text files to browser (was: text files)

Here is how i would do it (hey, this isn't homework is it?)
use strict; use CGI qw(:header); use CGI::Carp qw(fatalsToBrowser); use File::Find; use File::Basename; my $start = 3; my $end = 40; my $dir = 'files'; @ARGV = (); die "start must be less than end" if $start >= $end; die "no dir $dir here" unless -d $dir; find sub { my $numb = (fileparse($_,'.txt'))[0]; return unless $numb =~ /^\d+$/; push @ARGV, $File::Find::name if $numb >= $start and $numb <= $end; }, $dir; die "no .txt files found in $dir" unless @ARGV; print header; print while <>;
UPDATE:
I should explain that File::Find works recursively, so any sub directories in the parent directory that match will be printed as well. Also, from here it is rather trivial to use CGI.pm's param() method to accept arguments from the cgi script. Don't forget to add -T (taint mode) and 'scrub' these arguments before using them. That i leave as an excercise. ;)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
  • Comment on (jeffa) Re: Print contents of a range of text files to browser (was: text files)
  • Download Code

Replies are listed 'Best First'.
Re: Re: Print contents of a range of text files to browser
by cjf (Parson) on Jun 16, 2002 at 18:29 UTC
    Don't forget to add -T (taint mode) and 'scrub' these arguments before using them. That I leave as an excercise.

    Lesson 4, Part 2 of Ovid's CGI course has a bit on this for the interested.

Re: (jeffa) Re: Print contents of a range of text files to browser (was: text files)
by Anonymous Monk on Jun 17, 2002 at 00:02 UTC
    It is not homework, but thanks for your help. The text files are log files and users will have the choice to pick the usage between dates raging like the example. It will be from a form and the files will be listed on two select menus with all the file names.