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

Hi , I have a DATA directory which contains files like this
total 3 -rwxr-xr-x 1 me unknown 26 Mar 12 09:53 Monday.db -rwxr-xr-x 1 me unknown 27 Mar 12 09:54 Tuesday.db -rwxr-xr-x 1 me unknown 26 Mar 12 09:54 Friday.db -rwxr-xr-x 1 me unknown 26 Mar 12 09:54 Dummy
I am trying to open the directory and read each file and print the output of each file:
my $INFOFILES = "$HOME/DATA"; my @files=glob "$INFOFILES/*\.db"; print " the files are @files\n"; for( my $idx=0; $idx < $#files + 1; $idx ++ ) { $file = $files[$idx]; &process_file($file); } sub process_file { }
for some reason it is not storing any of the files under DATA inside the array @files. Any idea? thanks

Replies are listed 'Best First'.
Re: using glob to get files
by rir (Vicar) on Mar 12, 2004 at 16:32 UTC
    Perhaps you are shell-shocked.

    Do you mean/need:

    my $INFOFILE = "$ENV{HOME}/DATA";
    Be well.
Re: using glob to get files
by pbeckingham (Parson) on Mar 12, 2004 at 16:41 UTC

    The following works:

    #! /usr/bin/perl -w use strict; foreach (glob "$ENV{HOME}/DATA/*.db") { process_file ($_); }

    Please note that calling process_file() with that leading ampersand has side-effects that really surprised me when I first learnt of them.

      If you are depending on the side-effects of not having the leading ampersand, you may be in for some surprises, too. Care to share more about your experiences with the evil ampersand?

        To quote the good book (Camel, 2nd ed., p115) almost verbatim:

        &foo(1,2,3); # pass three arguments. foo(1,2,3); # the same. foo(); # pass a null list. &foo(); # the same. &foo; # foo() gets current args, like foo(@_) !! foo; # like foo() IFF sub foo pre-declared, else bareword "fo +o".

        The ampersand also disables any prototype checking on arguments not provided.

        Which means that under certain circumstances, using the ampersand can lead to unexpected results, so in my case, I tend to avoid it in all sub calls, so I don't have to remember the details.

        But I never called it evil, I just wanted folks to be aware that putting an ampersand in front of a sub call has odd behavior when no explicit arguments are passed. when I first read the Camel book, this section made me cringe.

Re: using glob to get files
by Wonko the sane (Curate) on Mar 12, 2004 at 16:26 UTC
    Hello,

    This works fine for me. Are you sure your path is correct? Or that $HOME
    is populated with what you think it is?

    Wonko

Re: using glob to get files
by borisz (Canon) on Mar 12, 2004 at 16:35 UTC
    try  my @files=<$INFOFILES/*.db>;
    Boris
Re: using glob to get files
by Anonymous Monk on Mar 12, 2004 at 19:48 UTC