4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
imkort
  • 81
  • 1
  • 8
  • 1
    It works for me without the quotes. – ooga Jul 07 '14 at 21:17
  • What happens when you exclude the quotes (aside from "not working")? – AKHolland Jul 07 '14 at 21:21
  • I'm running this as a script. Also sorry for missing the output of "not working". I somehow figured it's obvious. Noob mistake. – imkort Jul 07 '14 at 21:40
  • 3
    Are you using Cygwin? Under the command prompt, `date /T` works. Under Cygwin, `date /T` gives `date: invalid date \`/T'`. – Mr. Llama Jul 07 '14 at 21:42
  • I actually am using Cmder with Cygwin and some of the GnuTools. When I'm entering the `date /T` in command prompt, I'm getting proper date. On script execution, I'm getting the `date: invalid date '/T'`. – imkort Jul 07 '14 at 21:50
  • 1
    It sounds like when you call it without quotes, it is pointed to cygwin's `date` executable based on cygwin's path. When you call it with quotes, it is using Window's date executable, based on your Windows path. Go into Cygwin and type `which date` or `type date` and you'll see that you are not using the Windows date. – hmatt1 Jul 07 '14 at 22:10
  • 5
    Why don't you use a pure-Perl method of getting the date? e.g. `use Time::Piece; my $t = localtime; print $t->strftime('%a %d/%m/%Y');` This saves you from having to 1) check for errors in your external command, 2) worry about differences between external commands on different platforms, and 3) worry about annoying quoting issues. – ThisSuitIsBlackNot Jul 07 '14 at 22:17
  • I would use `Time::Piece` gladly, yet due to long story, and idiotic time format in our database, and system constraints it's not an option for me. – imkort Jul 07 '14 at 22:39
  • Can you be more specific about those constraints? I'm just curious why a core module like `Time::Piece` wouldn't work, since it can produce the exact same output as the `date` command, without the issues I mentioned in my previous comment. – ThisSuitIsBlackNot Jul 08 '14 at 04:56
  • Biggest "system" constraint is my manager, who actually want to get a grip on the code of the tool he is using. – imkort Jul 08 '14 at 07:17

1 Answers1

5

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.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115