|
From: Alien on 7 Apr 2008 03:50 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
From: Gunnar Hjalmarsson on 7 Apr 2008 04:49 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 = \&{$mname.'::foo'}; $subref->($arg); Also, you should read the FAQ entry perldoc -q "variable name" -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
From: Alien on 7 Apr 2008 06:31 > > 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" > > -- > 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!
|
Pages: 1 Prev: funzy.com http://geocities.com/it_super_manager/ Next: somewhat unusual way to define a sub |