in reply to Call sub in variable module
Also, if each module will always contain only 1 sub, do they need to have a sub at all? If not, how can they be written & called without a sub?
While I see you struck out this question, I can give you an explanation anyway.
That depends on whether you are calling the subroutine more than once.
If you are only calling the subroutine once, then you could skip defining a sub in the file. HOWEVER, any parameters you need to pass to the code in the file would have to be placed in global variables.
Note that repeating a require of a given file will not re-execute the code in it, no matter how many times you require that same file. It only get executed the first time you require a given file. (Though, of course, each different file required will be executed the first time each is required.)
In general, it's better to define subroutines that you call with the needed parameters. Even if the sub is only ever called once. This is cleaner and easier to understand and maintain.
Note: There is a way to "load and execute" the contents of a file any number of times. This can be done with do. HOWEVER, every time you do a file, it will be re-read and re-parsed. Also, unlike require, do only accepts a pathname to the file. Also, like require, any parameters that need to be passed will have to be put in variables.
I strongly suggest you continue to define subroutines that get called instead of trying to "call" a file (or module).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Call sub in variable module
by tel2 (Pilgrim) on Nov 08, 2016 at 01:09 UTC |