<?xml version="1.0" encoding="windows-1252"?>
<node id="320383" title="padding/substr quirk" created="2004-01-10 16:30:01" updated="2005-08-15 14:04:48">
<type id="115">
perlquestion</type>
<author id="289824">
RolandGunslinger</author>
<data>
<field name="doctext">
Take a look at this script;

&lt;code&gt;
#!perl
use strict;use warnings;
use Time::localtime;
print "substr month= " . (substr("0" . localtime-&gt;mon() + 1, -2)) . "\n";
print "substr day= " . (substr("0" . localtime-&gt;mday(), -2)) . "\n";
print "sprintf month= " . (sprintf "%02d", localtime-&gt;mon() + 1) . "\n";
print "sprintf day= " . (sprintf "%02d", localtime-&gt;mday()) . "\n";
&lt;/code&gt;

It returns the following;

&lt;code&gt;
&gt;perl pad.pl
substr month= 1
substr day= 09
sprintf month= 01
sprintf day= 09
&lt;/code&gt;

Now, I know the first two lines are not a good way to pad.  I found some nodes on Perlmonks that showed a better way, which is the last two lines.  The problem is I'm perplexed as to the first result.  Why does substr month only return a 1 instead of 01?
&lt;br&gt;
I'm using ActiveState Perl 5.8.0 running under Win32.  Thanks.</field>
</data>
</node>
