Using module whith name stored in variable

A

Alien

Hello all!
Can anybody tell how can I use modele which name stored in variable?
For example, I have this code:

########################################
package MyModule;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT $DEBUG);

@ISA = qw/Exporter/;
@EXPORT = qw(foo);

sub foo($)
{
my $arg = shift;
print "MyModule foo: my arg: '$arg'\n";
return undef;
}

1;
########################################


Here are the code of main programm

########################################
#!/usr/local/bin/perl

use strict;
my $module = 'MyModule.pm';
require $module; # load module

my $arg = 'MyArg';

# I want call procedure by full name. How I can do this?
my $mname = 'MyModule';
$mname::foo($arg); # This doesn't work!!!

exit 0;
########################################

Thanks for help
 
G

Gunnar Hjalmarsson

Alien said:
Can anybody tell how can I use modele which name stored in variable?

# I want call procedure by full name. How I can do this?
my $mname = 'MyModule';
$mname::foo($arg); # This doesn't work!!!

One way is via a reference to the sub:

my $subref = \&{$mname.'::foo'};
$subref->($arg);

Also, you should read the FAQ entry

perldoc -q "variable name"
 
A

Alien

One way is via a reference to the sub:

     my $subref = \&{$mname.'::foo'};
     $subref->($arg);

Also, you should read the FAQ entry

     perldoc -q "variable name"

That's great!
Thank you very mutch! I didn't know where to search the answer. How
easy it was!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top