File::Find question.

S

Steve P

Hello. I have this script below, and I need to only return the file
names rather than the whole path and filename and I'm a little fuzzy
on how to accomplish this. I've used find2perl to generate part of the
code. I need to read the directory, which is a sym link to a samba
mounted share, and get only the file names, which can and do vary as
well as the directory structure.
__________________________________________________________________________

#! /usr/bin/perl -w

use diagnostics;
use strict;
use File::Find ();
use Cwd;


open(OUT, ">/opt/lampp/htdocs/rfu/rfu.htm") or die "Can't open file:
$!\n";
&print_html();
&processfiles;
&printtail();
&redirect();


sub print_html {
my $curr = cwd ();
print OUT "<HTML>\n<HEAD>\n";
print OUT "<TITLE>File Listing</TITLE>\n";
print OUT "</HEAD>\n<BODY>\n";
print OUT "<P>\n";
}

sub processfiles {
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;

File::Find::find( {wanted => \&wanted, follow => 1},
'/opt/lampp/htdocs/rfu/');

sub wanted {
/^.*\.rfu\z/s &&

print OUT ("$name\n<BR>");
}
}

sub printtail {
print OUT "</BODY></HTML>\n";
close OUT;
}

sub redirect {
print 'Location: http://www.someplace.com/rfu/rfu.htm',
"$ENV{QUERY_STRING}\n\n";
}
_________________________________________________________________

Thanks in advance
Steve Powell
 
P

Paul Lalli

Hello. I have this script below, and I need to only return the file
names rather than the whole path and filename and I'm a little fuzzy
on how to accomplish this. I've used find2perl to generate part of the
code. I need to read the directory, which is a sym link to a samba
mounted share, and get only the file names, which can and do vary as
well as the directory structure.


Did you try reading the docs for File::Find?

For example, when examining the file /some/path/foo.ext you
will have:

$File::Find::dir = /some/path/
$_ = foo.ext
$File::Find::name = /some/path/foo.ext

Just use $_ instead of $File::Find::name inside the wanted function.
(find2perl is doing the odd bit of making just $name represent
$File::Find::name - just replace $name with $_)

Paul Lalli
 
T

Tad McClellan

Steve P said:
I need to only return the file
names rather than the whole path and filename


sub print_html {


Why are you specifying to circumvent prototypes when the functions
do not even _have_ prototypes?

See how to call subroutines in "perldoc perlsub", then call w/o the ampersand:

print_html();

sub wanted {
/^.*\.rfu\z/s &&


Think about where the string is that you are pattern matching against.

What is the name of the variable containing the string to be searched?

print OUT ("$name\n<BR>");
^^^^^


Use that variable name here instead of the one you have. :)
 
S

Steve P

Did you try reading the docs for File::Find?

For example, when examining the file /some/path/foo.ext you
will have:

$File::Find::dir = /some/path/
$_ = foo.ext
$File::Find::name = /some/path/foo.ext

Just use $_ instead of $File::Find::name inside the wanted function.
(find2perl is doing the odd bit of making just $name represent
$File::Find::name - just replace $name with $_)

Paul Lalli

Ah yes. I did read the docs on it, but for some reason, that little
part escaped me. Thank you for pointing that out. :)

Regards,
Steve Powell
 
S

Steve P

Tad McClellan said:
Why are you specifying to circumvent prototypes when the functions
do not even _have_ prototypes?

I'm a very novice perl scripter and all I know, I am learing from a
very old book (Teach yourself perl in 21 days by Laura Lemay) that
covers up to perl 5.005, so am doing best that I can at the moment. :)
Thanks for the tips Tad.

Regards,
Steve Powell
 
M

Michael Slass

I'm a very novice perl scripter and all I know, I am learing from a
very old book (Teach yourself perl in 21 days by Laura Lemay) that
covers up to perl 5.005, so am doing best that I can at the moment. :)
Thanks for the tips Tad.

Regards,
Steve Powell

The docs that come with perl are pretty good;

perldoc File::Find

will generate the man page for that package.

perldoc perldoc
gives the man page for the doc system itself.
 
T

Tintin

Steve P said:
I'm a very novice perl scripter and all I know, I am learing from a
very old book (Teach yourself perl in 21 days by Laura Lemay) that
covers up to perl 5.005, so am doing best that I can at the moment. :)

In general, be *very* cautious of any book the has a title like "Learn X in
Y days". They are usually a pile of junk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top