Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Use of special character "*" to retrieve the contents

by thanos1983 (Vicar)
on Jun 07, 2017 at 13:30 UTC ( #1192272=note: print w/replies, xml ) Need Help??

in reply to Use of special character "*" to retrieve the contents

Hello dk27,

What about some alternatives? Such as File::Find

#!/usr/bin/env perl use warnings; use strict; use File::Find; my $location="$var1/$var2/$var3/"; sub find_ksh { my $F = $File::Find::name; if ($F =~ /ksh$/ ) { print "$F\n"; } } find({ wanted => \&find_ksh, no_chdir=>1}, $location);

Or readdir:

opendir(my $dh, $some_dir) || die "Can't opendir $some_dir: $!"; my @files= map{s/\.[^.]+$//;$_}grep {/\.ksh$/} readdir DIR; closedir $dh;

Both solutions are untested but they should work straight out of the box.

Update: Also if you are running linuxOS or Cygwin terminal:

Update3: as monk haukex pointed out using back ticks is not a good option, read the link on his response bellow why.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $path = shift || '.'; my @files = `find $path -name '*.ksh' -o -name '*.txt'`; chomp @files; print Dumper \@files;

WindowsOS with Cygwin:

my @files = `find $path \( -name '*.ksh' -o -name '*.txt' \)`;

Update2: Similar question with some maybe useful answers combining regex on search see (Re: File::Find find several strings in one directory).

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: Use of special character "*" to retrieve the contents
by haukex (Abbot) on Jun 07, 2017 at 13:58 UTC

    It's important to note there is a difference between readdir/glob and File::Find/find: the latter recurses into subdirectories as well. As for running external commands, I wrote about that, including the potential security risks, at length here. At the very least I would recommend using something like IPC::System::Simple's capturex, e.g. my @files = capturex("find",$path,qw/ ( -name *.ksh -o -name *.txt ) /);

      "... the latter recurses..."

      But fortunately there is -maxdepth 0. From find:

      -maxdepth n Always true; descend at most n directory levels below the + command line arguments. If any -maxdepth primary is specified, it applies to the entire +expression even if it would not normally be evaluated. ``-maxdepth 0'' limits the whole +search to the command line argu- ments.

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      Furthermore I consider that Donald Trump must be impeached as soon as possible

Re^2: Use of special character "*" to retrieve the contents
by marinersk (Priest) on Jun 07, 2017 at 14:09 UTC

    Of course, this will fall apart a bit when fed "$var7/$var8/test*.dat" .

    Imagine their surprise, getting back a list of *.ksh files from that input. :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1192272]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-07-21 08:05 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (445 votes). Check out past polls.