Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're conflating several different ways to handle modules and objects - and ending up with one that doesn't work.</p> <p>Here are four approaches that do work:</p> <p>1/ My::Module is a library. trim is not exported.</p> <pre><code>$ cat My/Module.pm package My::Module; use strict; use warnings; sub trim { my $str = shift; $str =~ s{ \A \s+ }{}xms; # remove space from front of string $str =~ s{ \s+ \z }{}xms; # remove space from end of string return $str; } 1; $ cat test #!/usr/bin/perl use strict; use warnings; use My::Module; # Note: No $ and :: not -&gt; print My::Module::trim( " \t hello world\t \t" ); </code></pre> <p>2/ My::Module is a library. trim is exported.</p> <pre><code>$ cat My/Module.pm package My::Module; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(trim); sub trim { my $str = shift; $str =~ s{ \A \s+ }{}xms; # remove space from front of string $str =~ s{ \s+ \z }{}xms; # remove space from end of string return $str; } 1; $ cat test #!/usr/bin/perl use strict; use warnings; use My::Module; print trim( " \t hello world\t \t" ); </code></pre> <p>3/ MyModule is a class. trim is a class method.</p> <pre><code>$ cat My/Module.pm package My::Module; use strict; use warnings; sub trim { # Note class name passed as first argument my $class = shift; my $str = shift; $str =~ s{ \A \s+ }{}xms; # remove space from front of string $str =~ s{ \s+ \z }{}xms; # remove space from end of string return $str; } 1; $ cat test #!/usr/bin/perl use strict; use warnings; use My::Module; # Note: Not $ and -&gt; not :: print My::Module-&gt;trim( " \t hello world\t \t" ); </code></pre> <p>4/ MyModule is a class, trim is an object method.</p> <pre><code>$ cat My/Module.pm package My::Module; use strict; use warnings; # Need a constructor (but this one does nothing useful) sub new { my $class = shift; return bless {}, $class; } sub trim { # Note: Object method is passed an object (which is ignored here) my $self = shift; my $str = shift; $str =~ s{ \A \s+ }{}xms; # remove space from front of string $str =~ s{ \s+ \z }{}xms; # remove space from end of string return $str; } 1; $ cat test #!/usr/bin/perl use strict; use warnings; use My::Module; my $trimmer = My::Module-&gt;new; print $trimmer-&gt;trim( " \t hello world\t \t" ); </code></pre> <p>I think that you were trying for option 1. In this case, I think I'd recommend option 2.</p> <p>And to answer your final question. You are getting that error because you are trying to call a method on a variable ($My::Module) which is undefined.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload