I see two issues that are central to your question.
First, you apparently want to save precompiled regexes in the variables $white
, $singleyear
, and $nummonth
, but you are not using the right operator for that - you should use qr//
to compile and save the regex. Code like my $white = /^s*$/;
will run the regex against $_
and store the result in $white
.
Second, my $monthnumber = $param =~ /^d{1,2}/;
has two issues: the =~
operator used with m//
in scalar context simply returns a true/false value (that's the 1
you're seeing in the output february 1
), but if you want to get the capture groups from the regex, you need to use it in list context, in this case by saying my ($monthnumber) = ...
(the same issue applies to $yearnumber
). Second, that regex doesn't contain any capture groups!
I don't get exactly the output you claim (although it's close) - please have a look at Minimal, Complete, and Verifiable example, especially since your post initially contained quite a few syntax errors. If I apply the fixes I described above, I get the output
march 1999
which is what I would expect - I hope you can figure out how to fix the off-by-one error.
Update: I should add that you also don't need to try and parse date/times yourself. My favorite module for date/time handling is DateTime
(together with DateTime::Format::Strptime
), but in this case the core Time::Piece
is enough:
use Time::Piece;
my $param = "02 1999";
my $dt = Time::Piece->strptime($param, "%m %Y");
print $dt->fullmonth, " ", $dt->year, "
"; # prints "February 1999"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…