0

I know this is a duplicate of Can't use string ("1") as a subroutine ref while "strict refs" in use but I can't figure out what my problem is with the call to the dispatch table. The code seems to execute, but the following error appears in the log: Can't use string ("1") as a subroutine ref while "strict refs" in use at C:/filepath/file.pl line 15.

#! C:\strawberry\perl\bin\perl

use strict;
use warnings;
use Custom::MyModule;
use CGI ':standard'; 

my $dispatch_table = {
      getLRFiles => \&Custom::MyModule::getLRFiles,
      imageMod => \&Custom::MyModule::imageMod,
      # More functions
  };

my $perl_function = param("perl_function");
($dispatch_table->{$perl_function}->(\@ARGV) || sub {})->(); # Error occurs on this line

I'm not sure if it has something to do with the fact that I'm using a custom module, and it's probably something stupid, since I'm not extremely familiar with Perl, but any help would be appreciated!

Community
  • 1
  • 1
snyderxc
  • 105
  • 1
  • 11

1 Answers1

5
($dispatch_table->{$perl_function}->(\@ARGV) || sub {})->();

is same thing as

my $x = $dispatch_table->{$perl_function}->(\@ARGV);
($x || sub {})->(); # $x is probably not code ref

Try,

($dispatch_table->{$perl_function} || sub {})->(\@ARGV);

or perhaps

$_ and $_->(\@ARGV) for $dispatch_table->{$perl_function};
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • For your first suggestion to try, I'm now getting `Premature end of script headers.` and `Use of uninitialized value $perl_function in hash element`. That makes a lot of sense though. – snyderxc Dec 31 '13 at 18:22
  • Nevermind, it's working now. I wasn't passing in an argument properly. Thanks! – snyderxc Dec 31 '13 at 18:57