I\'m new in perl and I\'m trying to do this exercise but it doesn\'t work.
This is my module that I created.
#!/usr/bin/perl
use warnings;
use stric
When it comes to package naming, three things need to agree:
location and name of the package file
name in the package statement in the package file (the namespace)
the use statement for the package in the code that uses it
They need to "agree" as follows.
If the package declaration in its file is package My::Package; then the package need be used as use My::Package, and its file is Package.pm in the directory My.
This directory My itself need be in a location that the interpreter will search, or we need to inform it where to look. Custom packages are usually not in directories that are searched by default, which is what lib pragma is for: With your
use lib '/home/foobar/code';
I'd expect My directory, with Package.pm in it, to be in /home/foobar/code directory.
Then here is your example, with fixed names and with a few more adjustments.
File /home/foobar/code/My/Prepare.pm :
package My::Prepare;
use warnings;
use strict;
use Exporter qw(import);
our @EXPORT_OK = qw( clean my_print );
sub clean { return chomp $_[0] }
sub my_print { print "The Results: $_[0]\n" }
1;
And the script that uses this module
#!/usr/bin/perl
use warnings;
use strict;
use lib '/home/foobar/code';
use My::Prepare qw(clean my_print);
print "Enter a word: ";
my $input = <STDIN>;
print "You entered: $input";
my $cleaned_input = clean($input);
my_print($cleaned_input);
Please adjust paths above to your actual directory structure, by adding or removing path components as suitable. That My:: is sticking out in particular.
A few notes.
there is no need for the "shebang" line (#!/usr/bin/perl) in a module
use of Exporter above is a bit more modern
I strongly recommend using @EXPORT_OK (instead of @EXPORT), so that all listed symbols must be specifically imported by the user of the module. That is just better for everybody