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

POSIX::strptime again

Thread Next
Paul LeoNerd Evans
October 17, 2011 07:01
POSIX::strptime again
Message ID:
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

 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
ICQ# 4135350       |  Registered Linux# 179460

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About