Perl inside shell

I am trying to find out the difference between two dates, for which I am using perl inside SHELL. Below are my codes.

perl -MDate -e 'Date::Calc qw(Delta_DHMS);'

perl -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'

But I am getting the error:

[edwpapp9a:u210502] /home/u210502 $ ksh perltest
Can't locate Date.pm in @INC (@INC contains: /u001/edwcommon/ul/perl5/lib/5.8.0/aix-64all /u001/edwcommon/ul/perl5/lib/5.8.0 /u001/edwcommon/ul/perl5/lib/site_perl/5.8.0/aix-64all /u001/edwcommon/ul/perl5/lib/site_perl/5.8.0 /u001/edwcommon/ul/perl5/lib/site_perl .).
BEGIN failed--compilation aborted.
Undefined subroutine &main::Delta_DHMS called at -e line 1.

Can Someone help me.

Thanks In advance.

Make sure you have installed the Date::Calc Module

perl -MDate::Calc -e 'use Date::Calc qw(Delta_DHMS); ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)';
 
1 Like

little simpler to avoid use statement

perl -MDate::Calc=Delta_DHMS -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'
1 Like

My program is below

year1=2011
month1=01
day1=07
hour1=06
min1=05
sec1=16
year2=2013
month2=12
day2=12
hour2=09
min2=18
sec2=23

perl -MDate::Calc=Delta_DHMS -e '($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2)'
perl -e '$elapsed = "$Dh:$Dm:$Ds"'
echo $elapsed

I am getting the error below.

Date::Calc::Delta_DHMS(): not a valid date at -e line 1.

This is because the perl one liner doesn't expand the shell variables $year1,$month1....$sec2 within the single quotes and consider them as undefined and therefore invalid date.
Also the second perl statement will not work since it will not be executed on the same context as the previous one and $Dh, $Dm will be undefined on this line.

You should be writing a perl script than shell script for this.

So Can I interchange pass values between a perl program and shell program?
if yes how ?

Thanks
Saurav

Double quotes works in some cases, but not all. You can use shell env variables in perl using -MEnv option

$ export year1=2011
$ perl -MEnv -e 'print $year1'
2011
1 Like