Problems with backticks


I have developed a perl script to dynamically build an html page from a supplied text file.

On my RedHat Linux box it works perfectly yet on my ISPs FreeBSD box I have a problem. All I want to do is return the day of the week for a given date (not always today's date)

On RedHat Linux:

$dw = "25/12/1999";
$DofW = `date --date "$dw" +%w`;

... works perfectly and $DofW contains 6 indicating a Saturday. (It took me a while to figure out I needed double quotes around $dw)

On my ISPs box, I find from the man page for date, that the above date command doesn't work the same. I need to call it like this:

$DofW = `date -v25d -v12m -v1999y \"+%u\"`;

The problems occur when I replace the 25, 12 and 1999 with variables.  

I /split/ the required date into @Date then assign......

$Day = $Date[0];
$Month = $Date[1];
$Year = $Date[2];

# I then try the following 4 lines.....

$TD = "date -v".$Day."d -v".$Month."m -v".$Year."y \"+%u\"";
$DofW1 = `$TD`;
$DofW2 = `"$TD"`;
$DofW3 = `date -v25d -v12m -v1999y \"+%u\"`;

# Printing out the 4 values into html I get:

$TD : date -v 25d -v12m -v1999y:
$DofW1 ::
$DofW2 ::
$DofW3 :6 :

(The colons ':' are mine to indicate boundaries)

So why does my manual $DofW3 work (Returning 6 for a Saturday) yet the other two don't?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

UplandAuthor Commented:
Edited text of question.
#it looks like $Day had an extra space
use Time::Local;
$dw = "25/12/1999";
$DofW = (gmtime timegm 0,0,0,$Date[0],$Date[1]-1,$Date[2]-1900)[6];
#should be portable to any operating system

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maybe $Day has a leading space, it has in the output -
  $TD : date -v 25d -v12m -v1999y:
don't know which Unix you're using, my date(1) hasn't got -v option, so I can't test it.
UplandAuthor Commented:

Well it works perfectly - but even after checking the man page for gmtime I cannot say I fully understand it.... such as timegm??

The space in $Day which you picked up on was not relevent - I inserted one in my manual assigment and it still worked.

Many thanks... still curious about the backticks though!

timegm is explained in
perldoc Time::Local

$DofW1 = `$TD`;
should have been the same as
$DofW3 = `date -v25d -v12m -v1999y \"+%u\"`;
although I don't know what happened to the "+%u" in $TD
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.