0

I want to create a module in Perl. The below code is not working properly. I want to create a word count module and I want to reuse it further. Can anyone help me out to create this module? This is my first attempt to create a module so kindly help me out.

package My::count

use Exporter qw(import);
our @Export_ok = qw(line_count);

sub line_count {
  my $line = @_;
  return $line;
}

I saved the above code in count.pm

use My::count qw(line_count);

open INPUT, "<filename.txt";
$line++;
print line count is $line \n";

I saved the above script in .pi extension.

This code is showing error when I run it on an Ubuntu platform. Kindly help me to fix this errors.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 2
    What's the error the code is showing? – dudeman Mar 09 '15 at 03:59
  • Can't locate My/count.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at g.pl line 1. BEGIN failed--compilation aborted at g.pl line 1. – Aditya Acharya Mar 09 '15 at 04:05
  • 1
    [This](http://perlmaven.com/how-to-create-a-perl-module-for-code-reuse) is a pretty good article that will help you. – squiguy Mar 09 '15 at 04:27

3 Answers3

0

Perl scripts are stored with .pl extension. As you say use My::count qw(line_count); Perl tries to search the modules from the directories stored in @INC variable. You can run it with the -I flag to specify the directory to search the custom packages. Refer to this question for more info.

Community
  • 1
  • 1
xtreak
  • 1,376
  • 18
  • 42
0

By convention Perl packages usually have a capitalized first letter, so My::count is more in keeping with convention if you call it package My::Count;. Typically lower-cased module names are reserved for pragmas such as 'strict' and 'warnings'. So go ahead and change the name to My::Count.

Next, save the module in a path such as lib/My/Count.pm. lib is by convention as well.

Then you have to tell your script where to find package My::Count.

Let's assume you're storing your module and your executable like this:

~/project/lib/My/Count.pm
~/project/bin/count.pl

Notice I also used a .pl extension for the executable. This is another convention. Often on Unix-like systems people omit the .pl extension altogether.

Finally, in your count.pl file you need to tell perl where to find the library. Often that is done like this:

#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";

use My::Count 'line_count';

# The rest goes here...

As you can see, we're using FindBin to locate where the executable is stored, and then telling perl that it should look (among other places) in the lib folder stored in a relative location to the executable.

Naturally, as this is Perl, this is not the only way to do it. But it's one common idiom.

DavidO
  • 13,812
  • 3
  • 38
  • 66
  • Can't locate My/Count.pm in @INC (you may need to install the My::Count module) (@INC contains: /home/ngs6/adi2/../lib /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at count.pl line 6. BEGIN failed--compilation aborted at count.pl line 6. how to fix this problem sir, kindly help me to fix this problem !!!! – Aditya Acharya Mar 10 '15 at 10:14
0

You need to move your count.pm file into a directory called My. So you have the following.

./count.pl
./My/count.pm
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • if i run 2nd command" bash: ./My/count.pm: permission denied" like this out put is coming , kindly fix this plz – Aditya Acharya Mar 10 '15 at 10:07
  • I wasn't telling you to run those as commands. I was showing you where to put the files. Look, why don't you just pay a programmer to write this for you? – Dave Cross Mar 10 '15 at 12:19