in reply to Trouble usig a subroutine from a custom module

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]

Replies are listed 'Best First'.
Re^2: Trouble usig a subroutine from a custom module
by stevieb (Canon) on Nov 09, 2024 at 08:48 UTC

    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.

Re^2: Trouble usig a subroutine from a custom module
by fritz1968 (Sexton) on Nov 11, 2024 at 16:01 UTC

    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]

        To all. Thanks for your help, but I was able to figure what I was doing wrong.

        It was an ID10t error.

        In addition to the changes that choroba suggested, I realized that I had a typo in my Package.pm file. In the code that I submitted to this question, I changed around some names to mask my company and some directories for the company. In the process, I did not realize that the Package name in the .PL file was named something along the lines of :

        use customPerlMod;

        and although my file name as customPerlMod.pm, in the file itself, it was named:

        package customPerlmod;

        Note that the "m" in the package is lower case while the call to the package in the code has an upper case "M".

        Thanks for all of your help. Apologizes for my error