If a subroutine is in the same directory as my main function, how do I call it?

John

Alright I'm rewriting this to elaborate more. I am fairly new to Perl & programming so please bear with me. Essentially, I want to know how run one or more files (I am guessing modules?) inside of the main .pl file when they come from within the same directory. My code works fine with them all plugged in at the bottom of my file as a subroutine but not if I pull the subroutines out and turn them into modules.

dwarring

Here's a couple of steps to hopefully get you started. This exports some subs to package MyModule.

Firstly Create MyModule.pm in the same directory as your main program. For example:

package MyModule;
use warnings; use strict;

sub foo {
    my $p = shift;
    print "foo called with param: $p\n";
    bar();
}

sub bar {
    print "bar called\n";
}

1; # don't delete this line

Then to load and call subs from this package from your main program:

#/usr/bin/perl                                                                                                                                            
use warnings; use strict;

use File::Basename qw(dirname);
use lib dirname(__FILE__); # prepend source directory to the include path                                                                                 

use MyModule;

MyModule::foo(42);

The statement use lib dirname(__FILE__) is prepending the source directory of the main program to the module include path.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I call the `function` function?

From Dev

How do I call a function in bash if there is an alias by the same name?

From Dev

How do I get the alias of a function call?

From Dev

how do I call an inline friend function with the same name as a member function?

From Dev

do I have to have main function in my python code?

From Dev

How do I call this function correctly?

From Dev

How do I call a function on all hyperlinks on my page?

From Dev

How do I call a function inside a function

From Dev

How do I call all objects from the same constructor function at once?

From Dev

How do I structure my PyQt GUI right and call a function while passing the ui?

From Dev

How do I encrypt my /tmp directory?

From Dev

How do I call my viewmodel function from its view - child of viewmodel (knockout.js)

From Dev

How do I call this attribute to my if statement?

From Dev

How do I encrypt my /tmp directory?

From Dev

How do I call a different subroutine using parameters passed into the current one (for use in recursion)?

From Dev

How do I call a Javascript function from my Android activity?

From Dev

How do I make my JFrame class my main class?

From Dev

How do I call this function correctly?

From Dev

How do I call a method inside another class from my main?

From Dev

How do I call a function on all hyperlinks on my page?

From Dev

How do i call the case function

From Dev

How do I call function edit.draw from Leaflet in my button?

From Dev

How can I make my main if, elseIf and else to do the same thing as my main if does without getting "duplicate" errors?

From Dev

How do I use the `Function Call Operator` to load `rvalue` types to my object?

From Dev

How do I pass my struct values into my print function and then call it in main? - C Language

From Dev

How do I call some part of code again in same function?

From Dev

VBA EXCEL: How to call a subroutine in another subroutine?

From Dev

How do I call a function with a struct in C?

From Dev

How do I run functions inside my main function in python?

Related Related

  1. 1

    How do I call the `function` function?

  2. 2

    How do I call a function in bash if there is an alias by the same name?

  3. 3

    How do I get the alias of a function call?

  4. 4

    how do I call an inline friend function with the same name as a member function?

  5. 5

    do I have to have main function in my python code?

  6. 6

    How do I call this function correctly?

  7. 7

    How do I call a function on all hyperlinks on my page?

  8. 8

    How do I call a function inside a function

  9. 9

    How do I call all objects from the same constructor function at once?

  10. 10

    How do I structure my PyQt GUI right and call a function while passing the ui?

  11. 11

    How do I encrypt my /tmp directory?

  12. 12

    How do I call my viewmodel function from its view - child of viewmodel (knockout.js)

  13. 13

    How do I call this attribute to my if statement?

  14. 14

    How do I encrypt my /tmp directory?

  15. 15

    How do I call a different subroutine using parameters passed into the current one (for use in recursion)?

  16. 16

    How do I call a Javascript function from my Android activity?

  17. 17

    How do I make my JFrame class my main class?

  18. 18

    How do I call this function correctly?

  19. 19

    How do I call a method inside another class from my main?

  20. 20

    How do I call a function on all hyperlinks on my page?

  21. 21

    How do i call the case function

  22. 22

    How do I call function edit.draw from Leaflet in my button?

  23. 23

    How can I make my main if, elseIf and else to do the same thing as my main if does without getting "duplicate" errors?

  24. 24

    How do I use the `Function Call Operator` to load `rvalue` types to my object?

  25. 25

    How do I pass my struct values into my print function and then call it in main? - C Language

  26. 26

    How do I call some part of code again in same function?

  27. 27

    VBA EXCEL: How to call a subroutine in another subroutine?

  28. 28

    How do I call a function with a struct in C?

  29. 29

    How do I run functions inside my main function in python?

HotTag

Archive