-1

I have a pm file that contains several subroutines. Following is the example of the script "myscript.pm":

sub a();
sub b();
sub c();

a();      #this can not be deleted in my situation in this pm file. 

sub a() {
 print 'a';
}

sub b() {
 print 'b';
}

sub c() {
 print 'c';
}

sub d() {
 print 'd';
}

In the other script "running.pl", I would like to invoke c subroutine from myscript.pm. Following is the script:

use myscript qw(b);
b();

The result I get will be ab . However, this is not my intention. I was expecting b in the result. I wonder how I can invoke b subroutine from myscript.pm without running a(); ?

Cecil
  • 1

2 Answers2

1

Loading a module is simply executing it.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

a() will always run on loading the module because you are calling it explicitly in the module.

Why are you running a() in that way? You shouldn't use Myscript as both a script and a library.

Rather, move the code calling a() into a separate file and run that instead of Myscript. Rename it to MyLibrary, and call the new file Myscript. Now in the file calling b(), import Mylibrary instead of Myscript.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Tim De Lange
  • 739
  • 3
  • 6