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

I am fairly new to PERL. I have this code that searches a directory called /webpics which contains like several other subdirectories. How do I recursively search these. below is what I have. it only works for one subdirectory in the directory /webpics.
#!/usr/bin/perl -w use CGI; use strict; use product qw(:productInfo); use Cwd; $|++; my $q = new CGI; my $sku = $q->param('sku'); my $path_to_images = "/webpics/originals/"; my $path_to_images = "/webpics/itemthumb/"; my $path_to_images = "/webpics/itemmed/"; print $q->header(); #check for sku, description associated with it #then print the image if ($sku){ my $firstDigits; if ($sku =~ /^\d{3}/) {$firstDigits = $&} my %product = getProductInfo($sku); $path_to_images .= $firstDigits . "/" . $sku . ".jpg"; if (-e $path_to_images && values %product) { print <<HTML; <center> <table width="200" border="1"> <tr align="center" valign="middle"> <td> <img src="/webpics/originals/$sku.jpg"> </td> <td> <img src="/webpics/originals/$firstDigits/$sku.jpg"> </td> <td><img src="/webpics/itemthumb/$firstDigits/$sku.jpg"></td> </tr> </table> <table width="400" height="100"> <tr> <td align="left"> Sku Number: <b>$sku</b><br> Item Number: <b>$product{'item_number'}</b><br> Minimum Sell: <b>$product{'min_amount'}</b><br> Description: <b>$product{'description'}</b><br> Retail: <b>\$$product{'retail'}</b><br> </td> </tr> </table> </center> HTML } else{ print <<HTML; <center> <font color="red">Sorry, No Products Avaliable with that Sku Number.</ +font><br> </center> HTML } } print scalar localtime, $q->end_html

Replies are listed 'Best First'.
Re: help with search script
by davido (Cardinal) on Nov 06, 2004 at 16:40 UTC

    Frankly one of the easiest ways to recursively search a set of directories is to use the pre-fabricated solution of File::Find. Then whatever verification and action you want can be performed in the wanted() sub.


    Dave

Re: help with search script
by steves (Curate) on Nov 06, 2004 at 16:45 UTC

    If you know all the directories, as your code seems to indicate, a simple way would be to define them as an array like this:

    my @paths_to_images = ("/webpics/originals/", "/webpics/itemthumb/", "/webpics/itemmed/");
    Then loop over them like this:
    foreach my $dir (@paths_to_images) { # Call the code you have for one directory now }
    You could either take the code you have an put it into the for loop block, or make what you have a sub and call it where my comment is.

    For more complete directory recursion, use something like File::Find or roll your own using opendir/readdir/closedir.

Re: help with search script
by dimar (Curate) on Nov 06, 2004 at 18:23 UTC

    Here is a sample of (windows) code that recursively reads starting from a start directory, and saves all the file information in a table format using native perl data structures. Here we use File::Find; File::Basename; and Data::Dumper to do all the heavy lifting.

    HTH.

Re: help with search script
by TedPride (Priest) on Nov 08, 2004 at 07:18 UTC
    For Mac:
    use strict; use warnings; my ($dir, $file, $path); my ($script) = $0 =~ /([^:]+)$/; my @dirs = ':'; while ($dir = shift(@dirs)) { opendir(DIR, $dir); while ($file = readdir(DIR)) { next if $file eq $script; $path = "$dir$file"; if (-d $path) { push (@dirs, "$path:"); next; } ######### # Open each $path and process ######### } }
    Place in the main directory of the section you want processed.

    EDIT: I could probably have simplified this, but it was a cut and paste from something I wrote back several years ago. Have fun.

Re: help with search script
by calypso (Initiate) on Nov 08, 2004 at 16:18 UTC
    thanks everyone. That was such great help. Its working great. Now I gotta figure out the regex part of it. :)...