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::"

#!/usr/bin/perl
use strict;
use warnings;

BEGIN {
      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 ###

Answers


ikegami February 2016

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

or

( $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 } };
$ref->(@args)

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

my $ref = \&{ $pkg_name."::".$sub_name };
$ref->(@args)

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

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

or just

$o->$method_name(@args)

Post Status

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

Search




Leave an answer