MLUG: Re: [MLUG - DISCUSSION] Dumb Perl Question
Re: [MLUG - DISCUSSION] Dumb Perl Question
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Tue, 7 May 2002, Davis, Ryan Wiley (UMC-Student) wrote:

> Okay,
>
> So I am looking for a bit of perl to put into a CGI and I am kindof lost
> on exactly how to do it. Let me describe.
>
> I want the perl to read the filenames in a certain sub directory of my
> web account. There are a few files 10-30 in this directory. I would then
> like the perl code to output (into HTMLof course) the listing of said
> directory and a link to access all these files.
>
> I'm currently messing with different ways of doing this, but wanted to
> know if any of you monkeys out there have already done something like
> this and have a quick and easy solution.

I don't have "known good" Perl code, but you can use DBI with the
DBD::ExampleP driver to make your file system act like a database. It's
"rows" are file entries, it's "columns" are the different things
that fstat returns about a file, and it's "tables" are directories.

Fun, huh? [little known answer, I see. How I know it is coming up.]

It's a trick I read about in a Perl book. The problem is that the book
discusses perl 5.002 (!!!!), so the example code I'm taking this from is
a REALLY OLD version of DBI, but ExampleP still comes with the newest
versions of DBI, so I'm extrapolating to recent versions (something
greater than DBI 1.00 and/or perl 5.004)...

I think this is the correct code, YMMV.

  use DBI 1.15;

  $dbh = DBI->connect("dbi:ExampleP", undef, undef, {RaiseError => 0});
  # I don't THINK username and password are required for ExampleP.

  $sth = $dbh->prepare('select * from ?');
  # Selects all possible data from a directory.
  # I don't know how complicated SQL passed to DBD::ExampleP can
  # be without breaking it, so be careful.

  $files = $sth->selectall_hashref('/usr');
  # returns an arrayref with it's values being hashrefs.
  #   The keys of the hashref are column names.
  #   The values are the data. (an individual file in this case)
  # This call was introduced in DBI 1.15 (note "use" above)
  # The usage is correct for 1.15 to 1.19 AFAIK -
  #   1.20, I'm told, (looking at search.cpan.org) changes how
  #   this routine is used. I've never used it, so I don't know
  #   how to change it.
  # If you're stuck with lower than 1.15 (current is 1.21), look for
  #   some other way to figure out what the column names are.
  #   I know two column names are name and atime.

  @files = @$files;

  foreach $file (@files) {
    %file = %$file;
    ... # do your thing!

  }


-- 
Curtis Jewell
http://www.curtisjewell.com/          EMAIL:PROTECTED
http://curtis.livejournal.com/        EMAIL:PROTECTED
http://new-york.utica.mission.net/    EMAIL:PROTECTED

--
To unsubscribe, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/