基于我目前对Perl中哈希的理解,我希望这段代码可以打印出"hello world".它没有打印任何东西.
%a=();
%b=();
$b{str} = "hello";
$a{1}=%b;
$b=();
$b{str} = "world";
$a{2}=%b;
print "$a{1}{str} $a{2}{str}";
Run Code Online (Sandbox Code Playgroud)
我假设散列就像一个数组,为什么我不能让散列包含另一个?
看起来main中的符号'_<-'(没有引号)与其他看起来像是可以处理的东西相同:'_</usr/perl/lib/Carp.pm'例如.
有没有办法使用它?
或者,如果我希望阅读输入源,我是否必须使用源过滤器?
回复mob:我不知道Debug会在哪里打开.转出基表后,%INC的转储显示:
$VAR1 = {
'warnings/register.pm' => 'C:/strawberry/perl/lib/warnings/register.pm',
'XSLoader.pm' => 'C:/strawberry/perl/lib/XSLoader.pm',
'English.pm' => 'C:/strawberry/perl/lib/English.pm',
'Tie/Hash/NamedCapture.pm' => 'C:/strawberry/perl/lib/Tie/Hash/NamedCapture.pm',
'unicore/lib/Perl/_PerlIDS.pl' => 'C:/strawberry/perl/lib/unicore/lib/Perl/_PerlIDS.pl',
'unicore/Heavy.pl' => 'C:/strawberry/perl/lib/unicore/Heavy.pl',
'warnings.pm' => 'C:/strawberry/perl/lib/warnings.pm',
'utf8.pm' => 'C:/strawberry/perl/lib/utf8.pm',
'Config.pm' => 'C:/strawberry/perl/lib/Config.pm',
'overloading.pm' => 'C:/strawberry/perl/lib/overloading.pm',
'Symbol.pm' => 'C:/strawberry/perl/lib/Symbol.pm',
'Carp.pm' => 'C:/strawberry/perl/lib/Carp.pm',
'bytes.pm' => 'C:/strawberry/perl/lib/bytes.pm',
'Exporter/Heavy.pm' => 'C:/strawberry/perl/lib/Exporter/Heavy.pm',
'utf8_heavy.pl' => 'C:/strawberry/perl/lib/utf8_heavy.pl',
'strict.pm' => 'C:/strawberry/perl/lib/strict.pm',
'Exporter.pm' => 'C:/strawberry/perl/lib/Exporter.pm',
'vars.pm' => 'C:/strawberry/perl/lib/vars.pm',
'constant.pm' => 'C:/strawberry/perl/lib/constant.pm',
'Errno.pm' => 'C:/strawberry/perl/lib/Errno.pm',
'overload.pm' => 'C:/strawberry/perl/lib/overload.pm',
'Data/Dumper.pm' => 'C:/strawberry/perl/lib/Data/Dumper.pm'
};
Run Code Online (Sandbox Code Playgroud) 我一直试图通过多种方式做到这一点,但它们似乎都不够优雅.(我也想知道CPAN或Moose是否已经拥有此功能.随着时间的推移,我所做的几十次搜索都没有显示出相同的匹配.)
我想创建一种类
Base->new( %params ),它根据各个子类注册的策略创建类型.UnresolvedPath只知道我们应该首先检查是否存在.显而易见的例子是文件系统目录和文件:
package Path;
use Moose;
...
sub BUILD {
my ( $self, $params ) = @_;
my $path = $params->{path};
my $class_name;
foreach my $test_sub ( @tests ) {
$class_name = $test_sub->( $path );
last if $class_name;
}
croak "No valid class for $path!" unless defined $class_name;
$class_name->BUILD( $self, $params );
}
package Folder;
use Moose;
extends 'Path';
use Path register => selector …Run Code Online (Sandbox Code Playgroud) 我有一个函数将Excel数据提取到一个哈希数组中,如下所示:
sub set_exceldata {
my $excel_file_or = '.\Excel\ORDERS.csv';
if (-e $excel_file_or) {
open (EXCEL_OR, $excel_file_or) || die("\n can't open $excel_file_or: $!\n");
while () {
chomp;
my ( $id, $date, $product, $batchid, $address, $cost ) = split ",";
my %a = ( id => $id
, date => $date
, product => $product
, batchid => $batchid
, address => $address
, cost => $cost
);
push ( @array_data_or, \%a );
}
close EXCEL_OR;
}
}
Run Code Online (Sandbox Code Playgroud)
填充哈希数组是很好的.但是,困难的部分是在数组中搜索特定项(哈希).我似乎无法找到可能具有id或21,或batchid为15或成本> $ 20等的项目.
我将如何实施这样的搜索工具?
谢谢大家,
在调用BUILDARGS时,我很难正确使用MooseX :: Declare.
我正在尝试创建一个对象作为文件的接口.(具体来说,我想要一个二进制文件的接口,让我可以查看文件中的下几个字节,然后将它们关闭以进行进一步处理.)
我希望能够像这样创建其中一个对象
my $f = binary_file_buffer->new( $file_name );
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它
while( my $block_id = $f->peek( $id_offset, $id_length ) ) {
$block_id = unpack_block_id( $block_id );
$munge_block{ $block_id }->(
$f->pop( $block_size[ $block_id ] )
);
}
Run Code Online (Sandbox Code Playgroud)
我的binary_file_buffer类定义/声明看起来像这样
use MooseX::Declare;
class binary_file_buffer {
use FileHandle;
use Carp;
has _file => ( is => 'ro', isa => 'FileHandle' );
has _file_name => ( is => 'ro', isa => 'Str' );
has _buff => ( is => 'rw', isa => 'Str', default …Run Code Online (Sandbox Code Playgroud) 在Perl中,我该如何获得:
$VAR1 = { '999' => { '998' => [ '908', '906', '0', '998', '907' ] } };
$VAR1 = { '999' => { '991' => [ '913', '920', '918', '998', '916', '919', '917', '915', '912', '914' ] } };
$VAR1 = { '999' => { '996' => [] } };
$VAR1 = { '999' => { '995' => [] } };
$VAR1 = { '999' => { '994' => [] } };
$VAR1 = { '999' => { '993' …Run Code Online (Sandbox Code Playgroud) 我现在不太清楚,可能会忽略一些简单的事情.我已经考虑了一段时间并一直在寻找,但不能再想到任何明智的搜索查询会导致我找到我想要的东西.
简而言之,我想知道如何进行模块继承,就像base.pm/parent.pm为面向对象模块做的那样; 仅适用于基于Exporter的模块.
我的意思的一个假设的例子:
这是我们的脚本.它最初加载了Foo.pm并从中调用了baz(),但是baz()有一个可怕的bug(我们很快就会看到),所以我们现在使用Local/Patched/Foo.pm来解决这个问题.我们这样做,因为在这个假设的情况下我们不能改变Foo(它是一个正在积极开发的cpan模块,你看),并且它是巨大的(严重的).
#!/usr/bin/perl
# use Foo qw( baz [... 100 more functions here ...] );
use Local::Patched::Foo qw( baz [... 100 more functions here ...] );
baz();
Run Code Online (Sandbox Code Playgroud)
这是Foo.pm. 正如你所看到的,它导出了调用qux的baz(),它有一个可怕的bug,导致事情崩溃.我们希望保留baz和Foo.pm的其余部分,但没有做大量的复制粘贴,特别是因为它们可能会在以后更改,因为Foo仍在开发中.
package Foo;
use parent 'Exporter';
our @EXPORT = qw( baz [... 100 more functions here ...] );
sub baz { qux(); }
sub qux { print 1/0; } # !!!!!!!!!!!!!
[... 100 more functions here ...]
1;
Run Code Online (Sandbox Code Playgroud)
最后,由于Foo.pm用于很多地方,我们不想使用Sub :: Exporter,因为这意味着将bandaid修复程序复制粘贴到所有这些地方.相反,我们正在尝试创建一个新的模块,其行为和看起来像Foo.pm,并且实际上仍然从Foo.pm加载其99%的功能,并用更好的一个替换丑陋的qux子.
接下来是如果Foo.pm是面向对象的,那么这样的事情会是什么样子:
package Local::Patched::Foo;
use parent 'Foo';
sub qux …Run Code Online (Sandbox Code Playgroud) 在之前的一个问题中,我问过如何使用切片初始化Perl哈希.它是这样完成的:
my %hash = ();
my @fields = ('currency_symbol', 'currency_name');
my @array = ('BRL','Real');
@hash{@fields} = @array;
Run Code Online (Sandbox Code Playgroud)
现在让我们想象一个更复杂的哈希,以下是它的初始化方式:
my %hash = ();
my $iso = 'BR';
$hash->{$iso}->{currency_symbol} = 'BRL';
$hash->{$iso}->{currency_name} = 'Real';
print Dumper($hash);
Run Code Online (Sandbox Code Playgroud)
这导致以下结果:
$VAR1 = {
'BR' => {
'currency_symbol' => 'BRL',
'currency_name' => 'Real'
}
};
Run Code Online (Sandbox Code Playgroud)
现在的问题是:如何使用splice方法初始化这个特定的哈希?
天儿真好,
我正在努力从很多小哈希创造大哈希.假设这些较小的哈希值在每个文件中定义,然后可以包含在较大的哈希值中.
例如,让我们看一些小哈希
档案personcontact.pl:
return {
\'firstname\' => {
\'__type\' => \'String\'
},
\'lastname\' => {
\'__type\' => \'String\'
},
%{include("/tmp/address.pl")}
}
Run Code Online (Sandbox Code Playgroud)
档案address.pl:
return {
\'address\' => {
\'street\' => {
\'__type\' => \'String\'
},
\'unit\' => {
\'__type\' => \'String\',
\'__validation_function\' => {
\'is_a_number\' => \'\'
},
\'__schema_constraints\' => {
\'is_not_null\' => \'\'
}
},
\'suburb\' => {
\'__type\' => \'String\'
},
\'__type\' => \'ARRAY\'
}
}
Run Code Online (Sandbox Code Playgroud)
而且我有相当多的......
我试图重新创建哈希的方式是使用include子程序,如下所示:
sub include {
my …Run Code Online (Sandbox Code Playgroud) 我正在尝试从文件句柄中打印重复的行,而不是删除它们或我在其他问题上看到的任何其他内容.我没有足够的perl经验能够快速做到这一点,所以我在这里问.这样做的方法是什么?