【问题标题】:can any one help me to fix the error from this script? [closed]任何人都可以帮我解决这个脚本的错误吗? [关闭]
【发布时间】:2015-05-05 06:21:19
【问题描述】:

以下代码显示错误,请帮助我解决代码中的错误

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

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

我把上面的代码保存在count.pm中

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

我将此文件保存在 a.pl 中

【问题讨论】:

  • 将鼠标悬停在您在此问题上使用的api 标签上。看看它怎么说“不要使用这个标签!!”?这是个好建议。我可以想到至少两个完全不同的事情你可能会问所以你真的应该澄清你的问题。最好举个例子。
  • tq 先生,例如,如果我想计算文件中的行数,并且我想在 perl 中为此创建一个 API,以便我可以多次使用该 API在任何平台上。,,,现在你得到我的问题了吗??
  • 那些被称为模块。请参阅the manualthis duplicatethis other duplicate
  • 看起来你想创建一个 Perl 模块。您可以从这里开始介绍:en.wikipedia.org/wiki/Perl_module,然后学习任何“学习 Perl 编程”书籍、在线教程等。这不是 Stack Overflow 的问题,但如果您有一些特定的代码,请再次询问正在处理,但被卡住了
  • 可以创建吗??如果是,请帮帮我!!!!

标签: perl


【解决方案1】:

让我们详细看看这段代码。

# 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;

我会做这样的事情。

#!/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;

【讨论】:

  • 谢谢 Mr.DAVE,它工作得很好!!!!!!如果我想计算文件中的字符,Dave 先生还有一件事,而不是我必须在哪里添加脚本?请建议我,这对我会更有帮助。再次感谢你之前的剧本!!!!!!
  • 先生上面的代码,我如何从外部磁盘提供输入文件?
  • 好吧,比起我的代码,我宁愿你感谢我的解释。当我用工作代码回答问题时,总是担心有人会在不了解发生了什么的情况下剪切和粘贴代码。因此,我不会尝试破译您随后提出的问题的含义。我将发布一个指向Learn Perl 网站的链接,并建议您花几天时间通过他们的recommended books 之一工作。
  • package My::count use Exporter qw(import);我们的@Export_ok=qw(line_count); sub line_count {我的 $line=@_;返回$行;我把上面的代码保存在 count.pm use My::count qw(line_count);打开输入,“
  • 正如我所说,在对您的原始问题的评论中,将代码放在 Stack Overflow 评论中是一个可怕的想法。没有人会花时间去破译它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2014-10-17
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
相关资源
最近更新 更多