简体   繁体   中英

PERL Can't locate object method “echo” via package “IO::File” at ./echo.pl

I'm learning Perl by following the book "Learning Perl the Hard Way" by Allen B. Downey. It instructs me to execute the following code "echo.pl":

#! /usr/bin/perl

use strict;

sub echo {
    my @params = @_;
    print "@params\n";
}

echo ARGV

Testing the code returns:

$./echo.pl TEST NOW
Can't locate object method "echo" via package "IO::File" at ./echo.pl
line 10.

It should return:

TEST NOW

What is wrong?

echo ARGV

is interpreted by Perl as "indirect object notation", ie the same as

ARGV->echo

ARGV is a special file handle, its methods come form IO::File - but there's no echo method there.

what you are looking for should be this.

#!/usr/bin/perl
use warnings;
use strict;


sub echo {
     my @params = @_;
     print "@params\n";
}

echo(@ARGV);

The book is online here . This program is on page 4 (going by the page numbers). It is:

sub echo {
    print "@_\n";
}
echo @ARGV

So this is just a typo. You have ARGV where you wanted @ARGV .

That book is from 2003. A lot has changed in Perl since then. You might well be better off looking at Modern Perl instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM