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

I was hoping that someone can help me with this issue. I was able to create a use custom Perl modules from my previous employment, but am having an issue at this new job. Unlike the old job, I am not using the server version of perl, but instead, installed a local version to my home directory.

In this directory (/home/myhomedrive/script/prog1) I have this program called dupCheck.pl:

#!/home/myhomedrive/opt/perl/bin/perl #use strict; use warnings; use lib '/home/myhomedrive/scripts/lib'; use customPerlMod; my $string = " ll "; $string = trim ( $string );

As you can see from the code above, I have the custom module here: /home/myhomedrive/scripts/lib

Here is a snippet of my custom module (I have several different subroutines in it, but for the purposes of this question, I am showing only a few pieces of code):


#!/home/myhomedrive/opt/perl/bin/perl package customPerlMod; sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } # Left trim function to remove leading whitespace sub ltrim($) { my $string = shift; $string =~ s/^\s+//; return $string; } # Right trim function to remove trailing whitespace sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; } 1;

when I run the program, I get this at the command prompt:

./dupCheck.pl Undefined subroutine &main::trim called at ./dupCheck.pl line 20, <DAT +A> line 960.
any idea what I am doing wrong?

Replies are listed 'Best First'.
Re: Trouble usig a subroutine from a custom module
by choroba (Cardinal) on Nov 08, 2024 at 18:40 UTC
    You aren't exporting the subroutines.

    Without exporting them, you have to call them fully qualified:

    $string = customPerlMod::trim($string);

    Or, export them:

    package customPerlMod; use Exporter 'import'; our @EXPORT_OK = qw( trim ltrim rtrim );
    and then in the using code:
    use customPerlMod qw{ trim }; $string = trim($string);

    Other problems I noticed:

    1. You're using prototypes. Are you sure you know what they do?
    2. You commented out use strict;. And the module doesn't seem to use it at all. That's gonna hurt you.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Everything that choroba has said, plus permission from your employer to use custom code outside of whatever build stack your company may have.

      I'm all for using custom code to make things better, but if this isn't your gig, you should cover your legal bases.

      I love bending and manipulating rules, but be sure your ass isn't on the line if technical boundaries don't prevent you from breaching policy or legal ones.

      Choroba, thanks for your reply. I implemented your suggestions and will add the code here for trouble shooting purposes, but I am still getting this error:
      Undefined subroutine &VSCOperl::trim called at ./dupCheck.pl line 19, <DATA> line 960.

      Here is the new code to my program:

      #!/home/myhomedrive/opt/perl/bin/perl use strict; use warnings; use Data::Dumper; use Net::LDAP; use lib '/home/myhomedrive/scripts/lib'; use customPerlMod qw{ trim ltrim rtrim }; my $string = " ll "; $string = customPerlMod::trim ( $string );

      and here is the Module:

      #!/home/myhomedrive/opt/perl/bin/perl package customPerlMod; use strict; use warnings; use Exporter 'import'; our @EXPORT_OK = qw(return_month return_day trim ltrim rtrim ); # Perl trim function to remove whitespace from the start and end of th +e string sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; } # Left trim function to remove leading whitespace sub ltrim($) { my $string = shift; $string =~ s/^\s+//; return $string; } # Right trim function to remove trailing whitespace sub rtrim($) { my $string = shift; $string =~ s/\s+$//; return $string; } 1;

      what do you mean by prototypes?

      Thanks in advance for your help! Frank

        Something is off here.

        I copied the program to a file ~/_/0/1.pl and the module to ~/_/0/customPerlMod.pm. I changed the path to lib to actually point to the directory:

        use lib $ENV{HOME} . '/_/0';
        and added the following line:
        print "<$string>\n";
        When I run the script, I get back the following line:
        <ll>

        I get the same output when I drop the fully qualified name, as it's not needed with imported subroutines:

        $string = trim ($string);

        Prototypes are the ($) after subroutine names in their declarations. They tell Perl parser how to parse the arguments, they are not argument lists. If you don't know what they are and what they do, you should drop them.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
A reply falls below the community's threshold of quality. You may see it by logging in.