2

これがコードであり、機能していません。私がやろうとしているのは、ハッシュのハッシュをサブルーチン別名関数に渡すことですが、奇妙な出力が得られます。

my %file_attachments = (
         'test1.zip'  => { 'price' => '10.00', 'desc' => 'the 1st test'},
         'test2.zip'  => { 'price' => '12.00', 'desc' => 'the 2nd test'},
         'test3.zip'  => { 'price' => '13.00', 'desc' => 'the 3rd test'},
         'test4.zip'  => { 'price' => '14.00', 'desc' => 'the 4th test'}
                   );

                   my $a="test5.zip";
                   my $b="the 5th test";

         $file_attachments{$a}->{'price'} = '18.00';
         $file_attachments{$a}->{'desc'} =$b;


        print(%file_attachments);


sub print{

my %file =@_;

foreach my $line (keys %file) {
        print "$line: \n";
         foreach my $elem (keys %{$file{$line}}) {
          print "  $elem: " . $file{$line}->{$elem} . "\n";
    }
                 }

出力::::

      test2.zipHASH(0x3a9c6c)test5.zipHASH(0x1c8b17c)test3.zipHASH(0x1c8b3dc)test1.zipHASH(0x3a9b1c)test4.zipHASH(0x1c8b5dc)   
4

3 に答える 3

8

perlcriticは、Perl コードのデバッグに便利なツールです。

perlcritic -1 my_code.pl

Subroutine name is a homonym for builtin function at line 24, column 1.  See page 177 of PBP.  (Severity: 4)

これは、他の人が述べたことを発見する自動化された方法です。これはprint組み込み関数です。

于 2011-05-04T17:15:58.667 に答える
4

print組み込み関数です。その名前のサブルーチンを呼び出すには、&print(...)代わりに を使用しprint(...)ます。

于 2011-05-04T15:37:54.170 に答える
3

あなたの問題はprint、サブルーチンを呼び出していることだと思います.printはすでにperlで定義されています。

たとえば、サブルーチンの名前を変更してみてください。これは私にとってはうまくいきます。

my %file_attachments = (
         'test1.zip'  => { 'price' => '10.00', 'desc' => 'the 1st test'},
         'test2.zip'  => { 'price' => '12.00', 'desc' => 'the 2nd test'},
         'test3.zip'  => { 'price' => '13.00', 'desc' => 'the 3rd test'},
         'test4.zip'  => { 'price' => '14.00', 'desc' => 'the 4th test'}
                   );

                   my $a="test5.zip";
                   my $b="the 5th test";

         $file_attachments{$a}->{'price'} = '18.00';
         $file_attachments{$a}->{'desc'} =$b;


        printtest(%file_attachments);


sub printtest{

my %file =@_;

foreach my $line (keys %file) {
        print "$line: \n";
         foreach my $elem (keys %{$file{$line}}) {
          print "  $elem: " . $file{$line}->{$elem} . "\n";
    }
                 }
}
于 2011-05-04T15:41:10.083 に答える