我正在尝试将以下python程序移植到perl5:
import numpy as np
class Hensuu:
def __init__(self, data):
self.data = data
class Kansuu:
def __call__(self, input):
x = input.data
y = x ** 2
output = Hensuu(y)
return output
x = Hensuu(np.array(9.0))
f = Kansuu()
y = f(x)
print(type(y))
print(y.data)
亨苏下午
package Hensuu;
use strict;
use warnings FATAL => 'all';
our $version = 1.0.0;
sub new {
our ($class,$data) = @_;
my $self = {data=>$data};
bless $self,$class;
return $self;
}
1;
步骤1.pl
#!perl
use strict;
use warnings FATAL => 'all';
use Hensuu;
use PDL;
use PDL::Matrix;
my $datas = mpdl [[1.0,2.0],[3.0,4.0]];
my $x = Hensuu->new($datas);
print($x=>$datas);
坎苏下午
package Kansuu;
#use strict;
use warnings FATAL => 'all';
use Hensuu;
sub call {
my ($self,$input) = @_;
my $x = {$input => $data};
#print($x);
my $y = $x ** 2;
my $output = Hensuu->new($y);
return($output);
}
1;
步骤2.pl
#!perl
#use strict;
use warnings FATAL => 'all';
use PDL;
use PDL::Matrix;
use Hensuu;
use Kansuu;
my $a = mpdl [9.0];
my $x = Hensuu->new($a);
#my $f = Kansuu;
#my $y = $f->call($x);
my $y = Kansuu->call($x);
print($x=>$a);
print(ref($y));
print($y);
发射(步骤2.pl)
Hensuu=HASH(0x1faf80)
[
[9]
]
HensuuHensuu=HASH(0x25b71c0)
Process finished with exit code 0
上述程序(step2.pl), 我想用打印($y)将显示器设置为“81”;,但我不能。 环境是Windows10Pro,草莓perl PDL版(5.32.1.1), IDE是intellij idea社区版perl插件(2020.3)
下午六时
Kansuu.pm:
步骤2.pl:
our
变量用于不需要全局的东西李>$a
用作词法变量,它是在sort中使用的特殊变量李>相关问题 更多 >
编程相关推荐