MLUG: Re: [MLUG] formatting dates in perl
Re: [MLUG] formatting dates in perl
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Jonathan King wrote:
> 
> On Sun, 1 Apr 2001, Mike Miller wrote:
> 
> > On Sun, 1 Apr 2001, Mikhail Kovalenko wrote:
> >
> > > How about this:
> > >
> > > perl -e
> > > '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
> > > print $mon+1," $mday ",$year+1900,"\n";'
> >
> > Thanks, Mikhail!  Yes, that worked well.  I just don't understand how
> > quoting, commas and brackets work in perl print statements.

It's actually pretty simple until we throw in a couple of levels of
shell quoting as well. In perl, double quotes translate escaped
sequences such as \n and \t, single quotes don't so their contents get
printed literally. One type of quotes doesn't need to be escaped when
quoted by the other type (like " ' "), otherwise it does (like " \" ").
Shells have similar rules, so that's where you have to watch out. Print
will output whatever you give it, separated by commas; it doesn't have
to be a quoted string. There's probably more to it all, so see the
manual. Better yet, examine someone's code, like below :)

> Oh yeah: the most succinct way to print out mm/dd/yy for the current time
> that I know of is:
>    perl -lMPOSIX -e 'print strftime("%D",localtime);'
>    04/01/01
> 
> jking

Now, how could I ever beat that? All I knew is how to get rid of awk...
;)

-- MK

<< Terra es et in terram ibis >>
--
To manage your subscription, go to http://mlug.missouri.edu/members/edit.php

Archives are available at http://mlug.missouri.edu/list-archives/