in reply to Year-month for file names

My code works but I am not sure if I am over doing it or if there would be a better and more straight forward way to accomplish the same thing more efficiently.

Your subroutine takes no parameters, so no variation is possible. In other words, why waste time generating this at runtime, the returned array is constant, so make it so:

use constant DB_DATES => [ qw[ file_name_201212 file_name_201301 file_name_201302 file_name_201303 file_name_201304 file_name_201305 file_name_201306 file_name_201307 file_name_201308 file_name_201309 file_name_201310 file_name_201311 ] ]; print for @{ DB_DATES() };; file_name_201212 file_name_201301 file_name_201302 file_name_201303 file_name_201304 file_name_201305 file_name_201306 file_name_201307 file_name_201308 file_name_201309 file_name_201310 file_name_201311

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Year-month for file names
by daxim (Curate) on Nov 26, 2013 at 15:19 UTC
    Downvote, the sub is not invariant because of the $this_year variable in higher scope.
      the sub is not invariant because of the $this_year variable in higher scope.

      Correct. Now what was my point?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Year-month for file names
by Anonymous Monk on Nov 26, 2013 at 14:53 UTC
    It needs to generate the file names when it runs, and there is no parameters that needs to be passed to the sub, I don't understand your comments.
    "Your subroutine takes no parameters, so no variation is possible. In other words, why waste time generating this at runtime, the returned array is constant...".

      Seems my prompt was too obscure. See if this makes sense:

      use constant DATE_TEMPLATE => "file_name_%4d%02d"; sub buildEm{ my $year = shift; my @dates = sprintf DATE_TEMPLATE", $year-1, 12; push @dates, sprintf DATE_TEMPLATE, $year, $_ for 1 .. 11; return \@dates; };; $dates = buildEm( substr localtime, -4 );; print for @{ $dates };; file_name_201212 file_name_201301 file_name_201302 file_name_201303 file_name_201304 file_name_201305 file_name_201306 file_name_201307 file_name_201308 file_name_201309 file_name_201310 file_name_201311

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.