[sf-perl] Embedded Perl in a Korn shell program

Steve Fink sphink at gmail.com
Fri May 11 14:34:26 PDT 2007


On 5/11/07, Loo, Peter # PHX <Peter.Loo at source.wolterskluwer.com> wrote:
>
>
> Hi All,
>
> I am trying to add a day to a date using Perl Time::Local library within a
> Korn shell.  However it is not allowing my Korn shell variable to be used
> within the Perl command.
>
> MM=05
> DD=06
> YYYY=2007
>
> DAY_ADDED=`perl -e 'use Time::Local; ($secs = timelocal(0,0,0,$MM,$DD,$YYYY)
> + 86400); ($year, $mon, $day) = (localtime($secs))[5,4,3]; $year+=1900; $mon
> =~ s/^/0/ if $mon < 10; $day =~ s/^/0/ if $day < 10; print $year . $mon .
> $day;';`
>
> echo $DAY_ADDED
>
> I am getting the following error:
>
>
> Day '' out of range 1..31 at -e line 1
>
> Then I tried the following and got a different error:
>
> PERL_CMD="perl -e 'use Time::Local; (\$secs = timelocal(0,0,0,$MM,$DD,$YYYY)
> + 86400); (\$year, \$mon, \$day) = (localtime(\$secs))[5,4,3]; \$year+=1900;
> \$mon =~ s/^/0/ if \$mon < 10; \$day =~ s/^/0/ if \$day < 10; print \$year .
> \$mon . \$day; ';"
> DAY_ADDED=`${PERL_CMD}`
>
> Can't find string terminator "'" anywhere before EOF at -e line 1.
>
> What am I doing wrong here?

In addition to what Quinn just said, I wonder if you're literally
typing MM=05? If so, you're not exporting those variable settings.

It still isn't pretty (or correct; days aren't always 86400 seconds
long), but this is simpler than what you're doing:

MM=05 DD=06 YYYY=2007 perl -MTime::Local -MPOSIX -le '$secs =
timelocal(0,0,0,$ENV{MM},$ENV{DD},$ENV{YYYY}) + 86400; print
strftime("%Y-%m-%d", localtime($secs))'


More information about the SanFrancisco-pm mailing list