in reply to Re: strptime("%Y-%m") in perl6
in thread strptime("%Y-%m") in perl6

Let's say I want to check if month is valid. In perl 5 it is so simple:
use Time::Piece; localtime()->strptime(@ARGV[0], "%Y-%m");
I don't care about date parsing, I don't look at string at all -- perl5 does that for me.
bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-01" ok bash$ perl -e 'use Time::Piece; localtime()->strptime(@ARGV[0], "%Y +-%m"); print "ok\n"; ' "2019-77" Error parsing time at /usr/lib/x86_64-linux-gnu/perl/5.26/Time/Piece.p +m line 481.
What is elegant way to do thatn in perl6?

Replies are listed 'Best First'.
Re^3: strptime("%Y-%m") in perl6
by choroba (Cardinal) on Feb 05, 2019 at 14:55 UTC
    But also
    $ perl -MTime::Piece -le 'localtime()->strptime(shift, "%Y-%m-%d"); pr +int "ok"' 2019-02-30 ok
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: strptime("%Y-%m") in perl6
by Laurent_R (Canon) on Feb 05, 2019 at 17:04 UTC
    Nor sure to really understand your requirement, but what about this:
    > for <1 5 3 13> -> $month { say Date.new(2019, $month, 1 )} 2019-01-01 2019-05-01 2019-03-01 Month out of range. Is: 13, should be in 1..12 in block <unit> at <unknown file> line 1
      I need to check command line args. I expected perl6 to be simple as:
      "parse ARGS[0] as %Y-%m od die"
      Just one sentence without additional ARGS[0] manipulation.
        In Perl6, the special MAIN subroutine, if any, receives the arguments passed to the program and can be used to check their type and validity:
        ~ perl6 -e 'sub MAIN (Int $month where 1 <= $month <= 12) {say "Succes +s";}' 4 Success ~ perl6 -e 'sub MAIN (Int $month where 1 <= $month <= 12) {say "Succes +s";}' 14 Usage: -e '...' <month> ~ perl6 -e 'sub MAIN (Int $month where 1 <= $month <= 12) {say "Succes +s";}' 8.5 Usage: -e '...' <month>
        Is this the type of thing you're after?