Because you use single quotes instead of double...
Perl doesn't interpolate variables enclosed in single quotes, so what you are doing is sending the string '$srceDir' to the shell which will normally be unset (blank) unless you have it set in your environment somewhere.
Try this:
my $find_cmd = "find $srceDir -type f -newermt 2013-02-14 ! -newermt 2013-02-15";
or better this:
my $find_cmd = sprintf
'find "%s" -type f -newermt 2013-02-14 ! -newermt 2013-02-15',
$srceDir;
... care about spaces while *find_cmd* would be executed under forked sh.
* Important remark *
As @vonbrand rightly comment: perl do offer a lot of libraries for ensuring communication between your programm and approx everything.
For file system operation find perl use the File library module File::Find, for wich a little utility exist: find2perl who will translate your commande line in a little perl script:
$ find2perl -type f -mtime -3 ! -mtime -2;
#! /usr/bin/perl -w
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if 0; #$running_under_some_shell
use strict;
use File::Find ();
# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.
# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '.');
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
-f _ &&
(int(-M _) < 3) &&
! (int(-M _) < 2)
&& print("$name\n");
}
So your need could become something like this:
#! /usr/bin/perl -w
my $srceDir = "/mnt/SDrive/SV/Capture Data/";
my $startDate = "2013-02-14";
my $endDate = "2013-02-15";
use strict;
use File::Find ();
use POSIX qw|mktime|;
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
my ($sDay,$eDay)=map {
my ($year,$month,$day)=split("-",$_);
(time()-mktime(0,0,0,$day,$month-1,$year-1900))/86400
} ($startDate,$endDate);
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
-f _ &&
(-M _ < $sDay) &&
! (-M _ < $eDay)
&& print("$name\n");
}
File::Find::find({wanted => \&wanted}, $srceDir );
The most advantage of doing this, instead of open $fh,"find ...|" is that this is very robust, you don't have to care about of characters present in filenames (like spaces, quotes, ampersand...).