Home Ask Login Register

Developers Planet

Your answer is one click away!

chaya February 2016

How to create a dynamic subroutine name in perl

I want to create a dynamic subroutine name in perl, Here is trial code , I am getting error "Bad name after feed_load::"

use strict;
use warnings;

      push @INC, '/freespace/attlas/data/bin/genericLoader /FeedLoaderLib/'

use feed_load;
my type ="L";
my $tempTablefunct  = "Create".$type."Temp_Table";

feed_load::&$tempTablefunct->($tablename); ### pass a dynamic sub name HERE ###


ikegami February 2016

&{ $pkg_name."::".$sub_name }(@args)


( $pkg_name."::".$sub_name )->(@args)

These will fail, however, because you asked Perl to forbid you from doing this by placing use strict; in your program. You can disable use strict; locally

my $ref = do { no strict 'refs'; \&{ $pkg_name."::".$sub_name } };

But it turns out that \&$sub_name is already exempt from strictures.

my $ref = \&{ $pkg_name."::".$sub_name };

If instead of sub call, you needed a method call, you can use

my $ref = $o->can($method_name);

or just


Post Status

Asked in February 2016
Viewed 2,030 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life