in reply to Re: Is there a better way.
in thread Is there a better way.

Instead of running an external program to get a list of file names just use Perl's built-in glob function:

my @files = glob "$dir/*$caseNumber*";

Replies are listed 'Best First'.
Re^3: Is there a better way.
by Lawliet (Curate) on Feb 24, 2009 at 01:47 UTC

    Without digressing too much, why is it better to use Perl's built-in functions?

    And you didn't even know bears could type.

      Because someone might decide to copy the script to a machine where there is no command called "ls" (or where such a command is not in the user's PATH). Also, it might seem unlikely in this case, but one person's "ls" command might produce different output (or perform some different function) than another person's "ls" command.

      A perl-internal method like a glob or opendir()/readdir() can be counted on to produce consistent results wherever perl is installed.

      The qx operator forks off a separate process and then runs a shell (usually /bin/sh) to execute the `ls` command.   Perl's glob executes the glob(3) library function directly.

      If ls encounters a directory name matching *$caseNumber* then it will descend into that directory and list all the files there.

      If any of the file names contains a newline then qx will split it on the newline into separate file names.

      In general built-in functions are less platform dependent than invoking a command line program which may behave differently on various flavors of UNIX. In this case, 'ls' probably will work the same everywhere, but the general rule holds...