> -----Original Message-----
> From: Wright, Thomas [mailto:[EMAIL PROTECTED]]
> Sent: Monday, December 03, 2001 3:15 PM
> To: Beginners (E-mail)
> Subject: system `ls` or File::Find?
>
>
> Hi folks.
>
> I need to select a single file name from a list of filenames
> and assign it
> to a variable.
>
>
> I currently do this in a few shell scripts using: $last_file=`ls -1r
> somedata_file.dat.*.orig | head -1`
>
> The "*" represents a timestamp in the filename. This allows
> me to select the
> most recent file, in this case.
>
>
> as an example, this would return a value such as :
> somedata_file.dat.20011203130101.orig which would be stored
> in $last_file
>
>
>
>
> When I do this in Perl thusly:
>
> $oldfile = `system "ls -1r $oldfile.\*.orig | head -1"`;
You're combining backticks and system(). Just use the backticks:
$oldfile = `ls -lr $oldfile.*.orig | head -1`
> ...
>
> How can I do this?
You can avoid the two external programs and use Perl's globbing
capability. Something like this will work:
$oldfile = (sort {$b cmp $a} <$oldfile.*.orig>)[0]
How this works:
<$oldfile.*.orig> is a glob, returning a list of filenames
sort {$b cmp $a} sorts the list in reverse order
(...)[0] takes the first element of the list (i.e. "last" file)
Beware that since $oldfile is passed in by the user, it should
be checked for naughty characters before passing to ls or glob().
You may want to use -T switch to enable taint checking.
>
> Should I use File::Find? If so, how? I have not been able to
> find how to do
> this with File::Find in any of the O'Reilly books or on the web.
perldoc File::Find has all the poop. Sounds like you really don't
need it here, though.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]