Search code examples
perlstrawberry-perl

Why is the backticks operator in Perl not working without the quotes on Windows?


I'm pretty new to Perl, so forgive me if the question is trivial.

At work I have got an assignment to create a script which would send out e-mails when other developers would miss the due date of their tasks. As whole thing would have to work on Windows, using Strawberry Perl, I have used windows command date /T to perform the date check. I have called external commands quite a lot, using the backticks operator, but in this particular case the backticks would not work:

my $date = `date /T`;

Outputs:

date: invalid date `/T'

Fixed using some additional quotes:

 my $date = `"date /T"`

Outputs:

Mon 07/07/2014

My question is: why is that?

I would get that if the other external calls with backticks would work the same, but that's the only one I have to call that way, to make it work.


Solution

  • You seem to be accidentally using the Cygwin/GnuTools version of date.

    Under a Windows command prompt, date /T gives the current time.
    This is not an executable, this is a command.

    However, running date /T under the Cygwin/GnuTools environment gives date: invalid date '/T'.
    This is because the environment 1) cannot see the Windows date command and 2) finds a date executable in their PATH environment variable and runs it instead.