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

I have this code, and its suppose to lookup the files given a $path. The problem is that it doesn't show hidden files. What should i do?
sub browse_content { my ($widget,$path,$type,@ext) = @_; # $type = 0-Both / 1-File / 2-Folder # $path =~ s/\/$//; $path =~ s/ /\\ /; my @files = (); my $ext_count = @ext; if($ext_count>0){ my $filter = undef; foreach my $ex (@ext){ $filter = $filter . $ex . ','; } $filter =~ s/,$//; @files = <$path/*.{$filter}>; }else{ @files = <$path/*>; } my @return = (); if($type == 0){ return @files; }else{ foreach my $file (@files) { if(-d $file){ if($type == 2){ push @return, $file; } }else{ if($type == 1){ push @return, $file; } } } } return @return; }

Replies are listed 'Best First'.
Re: perl globbing cannot find all files / folder
by ELISHEVA (Prior) on Mar 24, 2009 at 14:23 UTC

    Use File::Find and filter directories and files with a regular expression and/or the various operators defined in -X. This approach will give you much more control over which files are returned. In addition to giving you access to hidden files, you can control how symbolic links and other special files are treated. This is not possible if you use a glob.

    For even more low-level control, you can use opendir, readdir and closedir.

    If you are trying to write portable code, you should try to avoid using globs. Globs aren't portable - their interpretation depends on the underlying operating system and shell - see glob and perlport for more information. Shell globs don't usually include hidden files, hence your inability to list hidden files in Perl.

    Best, beth