develooper Front page | perl.perl5.porters | Postings from October 2011

POSIX::strptime again

Thread Next
From:
Paul LeoNerd Evans
Date:
October 17, 2011 07:01
Subject:
POSIX::strptime again
Message ID:
20111017140112.GA9681@cel.leo
I've had something of an epiphany (actually a suggestion from #perl)

 use POSIX qw( strptime );

 @newtime = strptime $str, $format, @oldtime;   # for list modification

Preserves the useful API of returning a list, to pass through a
function:

 use POSIX qw( mktime strftime strptime ); # because localtime is builtin

 my $format = "%Y-%m-%d %H:%M:%S";

 sub str2epoch { return mktime strptime shift, $format; }
 sub epoch2str { return strftime $format, localtime shift; }

In order to get the "start parsing at pos() and update the pos() marker
to the end" functionallity, how about passing a SCALAR ref?

 @newtime = strptime \$str, $format, @oldtime;

This avoids the surprise you get if you accidentally end up with pos()
set on a string, and find that strptime() pays attention to it. You'd
have to ask explicitly "please use the pos() marker" by passing such a
SCALAR ref. Plus, by passing a SCALAR ref you are declaring to the
reader "be careful here I am intending to modify this in some way".
Useful for m//gc-style incremental parsers.


Any thoughts?

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About