1

I have written a Perl subroutine that is supposed to take the name of a month and return a number if it matches, otherwise returning the original argument if not.

sub convert_month_to_number {
 my ($input) = @_;
 my @month_names = qw/January February March April May June July August September October November December/;
 my @month_names_short = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
 for (my $i = 0; $i < scalar(@month_names); $i = $i + 1){
    print "$month_names[$i]\n";
    if ($input == $month_names[$i]{
        return $i;

    }   
 }

return $input;  
}

}

However, when compiling this in Padre I receive the error:

syntax error at additionalscripts.pl line 16, near "}"
Global symbol "$input" requires explicit package name at additionalscripts.pl li
ne 19.
syntax error at additionalscripts.pl line 20, near "}"
Execution of additionalscripts.pl aborted due to compilation errors.

I'm not exactly sure why this is occurring, considering how I've already declared $input as a global variable up top. Any help would be greatly appreciated. I'm currently using Perl 5.010 on Windows.

1 Answers1

0

You had a few errors in your code, which I've cleaned up.

my @test = qw(Foo January Bar);
say convert_month_to_number($_) foreach @test;

sub convert_month_to_number {
    my ($input) = shift;
    my @month_names = qw/January February March April May June July August September October November December/;
    my @month_names_short = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

    for (my $i = 0; $i < @month_names; $i++){
        if ($month_names[$i] eq $input){
            return $i;
        }

    }
    return $input; 

}

fugu
  • 6,417
  • 5
  • 40
  • 75