gpt4 book ai didi

缺少 Perl 参数

转载 作者:行者123 更新时间:2023-12-01 05:12:41 27 4
gpt4 key购买 nike

我有一个名为 new 的方法。调用 new 时,我传递了一个参数,但是当我运行应用程序时,出现没有参数或参数为空的错误。

StepReader.pm

package StepReader;

use strict;
use warnings;

sub new {
# Variable for file path
my $class = shift;
my ($filename) = @_;
if (!defined($filename) or length($filename) == 0) { die 'file path argument is required!'} # check if argument is empty or not!
# Open my file
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";

# Print the file!
while (my $row = <$fh>) {
chomp $row;
print "$row\n";
}

return bless {}, $class;
}

1;

example.pm

use strict;
use warnings;

use lib 'src/';
use StepReader;

StepReader::new('assets/glasses.STEP');

输出

file path argument is required! at src/StepReader.pm line 10.

Use of uninitialized value $filename in print at src/StepReader.pm line 10.

我希望有人能帮助我。提前致谢!

最佳答案

在调用方法调用时使用:: 不会将类作为参数传入。本质上,new() 中的第一个参数是文件名,它被放入 $class 变量中。

改变:

StepReader::new('assets/glasses.STEP');

...到:

StepReader->new('assets/glasses.STEP');
# ^^

关于缺少 Perl 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57772671/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com