Question

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!

Was it helpful?

Solution

($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};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top