WearyTraveler has asked for the wisdom of the Perl Monks concerning the following question:

I loaded strawberry perl on my Win 7 machine. Things are going pretty good, but I would like to create a subs.pm file that can store all the little subroutines that I am writing / using.

They work fine when inside the program, but when I try to call the subs.pm, perl can’t find them.

Directory structure: C:\strawberry\perl\bin\test.pl C:\strawberry\perl\subs.pm

Subs.pm snippet:

… sub remove_spaces { my ($input, $junk) = @_ ; $input =~ s/\s+/ /g ; $input =~ s/^ //g ; $input =~ s/ $//g ; chomp($input) ; return $input ; } …

Script snippet:

#!C:\strawberry\perl\bin\perl.exe #use lib 'C:\strawberry\perl'; #use subs ; … $line = remove_spaces($line); …

Results in:

C:\strawberry\perl>test.pl Undefined subroutine &main::remove_spaces called at C:\strawberry\perl +\test.pl line 18, <INF> line 1.

How do I refer to the subroutine in the pm file?

Thank you in advance!

Replies are listed 'Best First'.
Re: strawberry perl and subroutines - subs.pm
by Corion (Patriarch) on Dec 09, 2010 at 23:16 UTC

    Your subs.pm is named somewhat unfortunate, as it clashes with subs. If you name your module something else (like, maybe my_subs.pm) and then use it, Perl will likely find it

Re: strawberry perl and subroutines - subs.pm
by jethro (Monsignor) on Dec 10, 2010 at 00:59 UTC
    Additionally it would be advisable to remove the '#' in front of your use statements.
Re: strawberry perl and subroutines - subs.pm
by 7stud (Deacon) on Dec 10, 2010 at 01:41 UTC

    Just so you know, perl checks the directories contained in a perl global variable called @INC for any files you include in your program. perl searches each directory listed in @INC for any files you try to use.

    By default, @INC contains the current directory ".", as well as certain directories that were created when you installed perl. If you want to see what directories are in @INC, you can print them out:

    for my $dir (@INC){ print "$dir\n"; }

    You can always add directories to @INC yourself--if perl is unable to find your files. For instance,

    unshift @INC, "C:\strawberry\perl";

    That will add your directory to the front of the @INC array. That may also take care of your name conflict with perl's subs.pm module: perl checks the directories in the order found in @INC, so perl will look in the directory you shoved onto the front of @INC before it checks any other directories, so if one of the other directories contains perl's subs.pm, your version will be found first.

    However, it's always best when you are new to a language to preceed variable names, class names, or module names with some unique identifier, e.g. "MY", so that you don't run into conflicts with built in names. Conflicts between user defined names and built in names can be hard to track down in any language.

      unshift @INC, "C:\strawberry\perl";

      Unless it happens at compile time, that won't do him any good. Besides, he is already using lib like a smart fellow.

Re: strawberry perl and subroutines - subs.pm
by WearyTraveler (Novice) on Dec 20, 2010 at 19:43 UTC
    Thank you for all your help!