ggadd has asked for the wisdom of the Perl Monks concerning the following question:

I was trying to extract month and day from localtime() for use in a larger program using if statements. The numerical day worked fine, but the month string seemed to disappear. There's probably a simple explanation my newbie mind isn't grasping. Testing this out below(March 9):

use warnings; $time = scalar localtime(); $time =~ /(\s[A-Z][a-z]+)/; $month = $1; $time =~ /\s(\d+)\s/; $day = $1; print $month," ",$day; print "\n"; if($month eq 'Mar'){print "true\n";} else{print "false\n";} if($day == 9){print"true";} else{print"false";} #prints: Mar 9 false true

The $month value('Mar'} seems to vanish within the if statement and only in the if statement. What's going on here?

Replies are listed 'Best First'.
Re: value disappears in if statement
by Anonymous Monk on Mar 10, 2015 at 03:30 UTC

    You really should be using strict, and using a module like Time::Piece or DateTime to do your date/time handling.

    Anyway, when you print your values, do you see how you've got a space before the month name? That's why the eq isn't matching it. The reason is the \s inside the first capture group; try writing $time =~ /\s([A-Z][a-z]+)/; instead.

    When I print stuff during debugging, I use Data::Dumper, Data::Dump, or at least do something like print "($value)\n"; to catch whitespace issues like these. See also Basic debugging checklist.

      You really should be using strict

      Why?

      I figured it would probably be something simple...thanks a mil. I considered everything except the whitrspace!

Re: value disappears in if statement
by trippledubs (Deacon) on Mar 10, 2015 at 05:05 UTC

      And that would be the most awesome reply eva

Re: value disappears in if statement
by Anonymous Monk on Mar 10, 2015 at 03:36 UTC
Re: value disappears in if statement
by ww (Archbishop) on Mar 10, 2015 at 14:32 UTC

    I hope this is not too late to be useful -- even on topics beyond the scope of the original question.

    #!/usr/bin/perl use 5.018; use strict; # NOT because it makes a difference here, but because +it will # (OFTEN!) point out typos and other mistakes use warnings; # 1119420 (my $time) = scalar localtime(); say "\t DEBUG \$time: |$time|"; # the vbars in the printout will he +lp you spot # mismatches between your expectati +ons & # the actual regex captures $time =~ /\s([A-Z]{3})/i; # you don't want to capture the pre +ceding space; the other my $month = $1; # changes are merely for laziness ( +compactness) and say "\t DEBUG \$month: |$month|"; # rely on the fact that all localti +me() months # are just 3 chars $time =~ /\s(\d+)\s/; my $day = $1; say "\t DEBUG \$day: |$day|"; say "\t ", $month, " " ,$day; # when printing to console, I find +output with # leading tabs makes the output mor +e legible. YMMV. if ( $month eq 'Mar' ){ # reformatting makes (again, YMMV) +the reading easier. print "\t true\n"; } else { print "\t false\n";} if ( $day =~ m{\d+} ) { # regex instead of == to accomodate + days_of_month 10 say "\t true"; # thru 31 but, would, of course ac +cept "47" as a } else { # day_of_month so would be a baaad +idea for some # origins of day_of_month. say "\t false"; }

    Update: fixed typo  s/<c><#!.../<c>#!..../ in markup

Re: value disappears in if statement
by CountZero (Bishop) on Mar 10, 2015 at 20:32 UTC
    localtime can do the parsing for you:
    use Modern::Perl qw /2014/; my ( $day, $mon ) = ( localtime(time) )[ 3, 4 ]; my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; say "$months[$mon] $day";

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics