Click here to get back home

Using module whith name stored in variable

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Using module whith name stored in variable Alien 04-07-2008
Posted by Alien on April 7, 2008, 3:50 am
Please log in for more thread options
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

Posted by Gunnar Hjalmarsson on April 7, 2008, 4:49 am
Please log in for more thread options
Alien wrote:
> Can anybody tell how can I use modele which name stored in variable?

<snip>

> # 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 = \&;
$subref->($arg);

Also, you should read the FAQ entry

perldoc -q "variable name"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Alien on April 7, 2008, 6:31 am
Please log in for more thread options
>
> One way is via a reference to the sub:
>
> =A0 =A0 =A0my $subref =3D \&;
> =A0 =A0 =A0$subref->($arg);
>
> Also, you should read the FAQ entry
>
> =A0 =A0 =A0perldoc -q "variable name"
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

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

Similar ThreadsPosted
export a variable from a module March 10, 2006, 6:27 am
Why isn't variable maintained between subroutines in .pm module? February 18, 2007, 2:09 pm
Which module for variable-length binary db file? July 31, 2004, 5:28 pm
use one subroutine's variable value in another subroutine inside a module. April 26, 2007, 9:28 am
Using one methods variable value in another method inside a module April 29, 2007, 9:04 am
Any way to access global variable in Perl script from one module file? May 10, 2007, 4:03 am
DBD::Oracle, tracing, and stored procedures... May 9, 2007, 2:30 pm
Re: How do I follow links stored in an array? April 29, 2008, 2:02 pm
"Variable ... is not imported..." using an imported variable from a module July 1, 2005, 7:04 am
Start processes stored in array and wait in between May 10, 2005, 2:17 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap