-3

The below code is showing errors kindly help me to solve the errors in the code

package My::count
use Exporter qw(import);
our @Export_ok=qw(line_count);

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

I saved above code in count.pm

use My::count qw(line_count);
open INPUT,"<filename.txt";
$line++;
print line count is $line \n";

I saved this file in a.pl

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • 1
    Hover your mouse over the `api` tag you used on this question. See how it says "DO NOT USE THIS TAG!!"? That's good advice. I can think of at least two completely different things you might be asking about so you really should clarify your question. Preferably with an example. – Quentin Mar 04 '15 at 10:03
  • tq sir, for example if i want to count the number of lines in the file , and i want to create one API for that in perl, so that i can use that API for more than one time in any platform.,,, now did you get my problem?? – Aditya Acharya Mar 04 '15 at 10:06
  • 1
    Those are called modules. See [the manual](http://perldoc.perl.org/perlmod.html#Perl-Modules) or [this duplicate](http://stackoverflow.com/questions/1731089/how-do-i-get-started-writing-a-module-for-cpan) or [this other duplicate](http://stackoverflow.com/questions/550831/how-do-you-create-a-perl-module). – Quentin Mar 04 '15 at 10:09
  • 1
    It looks like you want to create a Perl module. You could start here for an introduction: http://en.wikipedia.org/wiki/Perl_module , then study any "learn to program in Perl" book, online tutorial etc. It is not really a question for Stack Overflow, but please do ask again if you have some specific code you are working on, and are stuck – Neil Slater Mar 04 '15 at 10:10
  • is it possible to create?? if yes than kindly help me out !!!! – Aditya Acharya Mar 04 '15 at 10:10
  • #!usr/bin/perl #use strict; #use warning; open IP,") { $line_count++; my @words_on_this_line= split(" ",$line); $word_count+= scalar(@words_on_this_line); } print"This file contains $line_count lines\n"; print"this file contains $word_count words\n"; foreach $line(@IP) { if($line =~ /^>/) { print $line; } } close IP; in this script i am getting partial output, after foreach statement its not showing any kind of output. kindly help me to fix this problem? – Aditya Acharya Mar 04 '15 at 10:12
  • Probably you should close and reopen the file after the count, I don't think `@IP` does what you expect. If that doesn't help, I suggest you make your code a new question, it is not possible to answer in comments, and is completely different to your first question. Please take a little time to format it correctly and explain more about what is wrong. – Neil Slater Mar 04 '15 at 10:19
  • tq Mr.Neil,sure i will take some time n prepare nice question – Aditya Acharya Mar 04 '15 at 10:20
  • Please don't dump code in a comment like that. If you want to share code with us, then edit your question and add the code there (properly formatted). – Dave Cross Mar 04 '15 at 11:19

1 Answers1

1

Let's look at this code in some detail.

# There's a typo on the line below. It should be /usr/bin/perl
#!usr/bin/perl

# Never comment out these lines.
# Instead, take the time to fix the problems.
# Oh, and it's "warnings", not "warning".
#use strict;
#use warning;

# Always check the return value from open()
# Please use lexical filehandles.
# Please use three-arguments to open().
# open my $ip_fh, '<', 'test1.txt' or die $!;
open IP,"<test1.txt";
my ($line_count,$word_count)=(0,0);

# You're rather fighting against Perl here.
# If you do things in a Perlish manner then it all becomes easier
while(my $line=<IP>) {
  $line_count++;
  my @words_on_this_line= split(" ",$line);
  $word_count+= scalar(@words_on_this_line);
}

print"This file contains $line_count lines\n";
print"this file contains $word_count words\n";

# It all goes a bit wrong here. You don't have an array called @IP.
# I think you wanted to iterate across the file again.
# Either use seek() to return to the start of the file, or store
# the lines in @IP as you're reading them.
# Also, you need to declare $line.
foreach $line(@IP) {
  if ($line =~ /^>/) {
    print $line;
  }
}

close IP;

I would do something like this.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010; # for say()

my $filename = shift || die "Must give file name\n";

open my $fh, '<', $filename or die "Can't open $filename: $!\n";

my ($word_count, @matches);

while (<$fh>) {
  # By default, split() splits $_ on whitespace
  my @words_on_line = split;
  $word_count += @words_on_line;
  push @matches, $_ if /^>/;
}

# No need to count lines, Perl does that for us (in $.)
say "This file contains $. lines";
say "This file contains $word_count words";
print @matches;
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thank you Mr.DAVE, its working nice !!!!!!and one more thing Mr.Dave if i want to count the character in file, than where i have to add the script ? kindly suggest me it will be much more helpful for me . once again i would like to thank you for your previous script !!!!! – Aditya Acharya Mar 04 '15 at 12:48
  • Sir the above code , how i have to give input file from external disk? – Aditya Acharya Mar 04 '15 at 13:00
  • Well, I'd rather that you were grateful for my explanations than my code. It's always a worry when I answer questions with working code that someone will just cut and paste the code without understanding what's going on. So I'm not going to try and decipher what your subsequent questions mean. I'll just post a link to the [Learn Perl](http://learn.perl.org/) web site and suggest that you spend a few days working through one of their [recommended books](http://learn.perl.org/books/). – Dave Cross Mar 04 '15 at 13:15
  • package My::count use Exporter qw(import); our @Export_ok=qw(line_count); sub line_count {my $line=@_; return $line; } I saved above code in count.pm use My::count qw(line_count); open INPUT," – Aditya Acharya Mar 06 '15 at 06:10
  • As I said, in a comment on your original question, putting code in a Stack Overflow comment is a *terrible* idea. No-one is going to take the time to decipher it. – Dave Cross Mar 06 '15 at 13:39
  • i am not getting how to post the code sir !!! – Aditya Acharya Mar 07 '15 at 05:31
  • hello Mr.DAVE, $. expression is not working for other script, what i can do further? kindly help me out – Aditya Acharya Mar 07 '15 at 06:51
  • If you have a new question, then post it as a new question (including the code). – Dave Cross Mar 07 '15 at 14:12
  • $. this operator is not working properly, is there any alternative way to count the lines in the file??? – Aditya Acharya Mar 12 '15 at 04:25
  • "is not working properly" is a terrible way to describe a problem. If you have a new question, then please post it as a new question along with a small self-contained example which demonstrates the problem. – Dave Cross Mar 12 '15 at 07:29
  • i wish to create my own module and i want to use that module name for further use, the main concept is, create a module which should contains subroutines for line count, word-count and character count, and in main program i should use that module and i should read the file and the output should show me total number of lines , total number of words and total number of characters in that file. i think this much information is enough to create, if u need any further information than let me know !!!! kindly help me to create complete this task. – Aditya Acharya Mar 12 '15 at 08:00
  • That comment doesn't explain the way in which `$.` is not working properly for you. – Dave Cross Mar 12 '15 at 09:44
  • Look, this question has been closed as unclear. So forget about it and start again. Post a completely new question explaining exactly how far you have got and what problems you are currently having. – Dave Cross Mar 12 '15 at 09:49