2

Here's the code and its not working, What I am trying to do is to pass Hash of Hashes to subroutine aka function, but it gives some odd output.

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";
    }
                 }

OUTPUT::::

      test2.zipHASH(0x3a9c6c)test5.zipHASH(0x1c8b17c)test3.zipHASH(0x1c8b3dc)test1.zipHASH(0x3a9b1c)test4.zipHASH(0x1c8b5dc)   
Axeman
  • 29,660
  • 2
  • 47
  • 102
Xomo
  • 91
  • 1
  • 7

3 Answers3

8

perlcritic can be a handy tool in debugging Perl code:

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)

This is an automated way of discovering what others have stated: that print is a built-in function.

toolic
  • 57,801
  • 17
  • 75
  • 117
4

print is a builtin function; to call a subroutine named that, use &print(...) instead of print(...).

ysth
  • 96,171
  • 6
  • 121
  • 214
  • 1
    Does that work for `&if(...)`? To my horror--*yes it does!* Even with *strict* `sub if { say 'If!'; }` *works!* Perl is the "new" PL/I!! – Axeman May 04 '11 at 16:10
  • 2
    `&if` is just a variable that holds a function type. Are you horrified that you can use `$if`, `@if`, and `%if`? – mob May 04 '11 at 17:22
  • 1
    `*{""} = sub {print "empty string"}; &{""}()` – ysth May 04 '11 at 17:44
  • @mob, not really horrified at `&if`--as I wouldn't be at `$if` or the like--but that I can create it with `sub if {...}`. But as ysth shows `*{''}` can be a sub – Axeman May 04 '11 at 19:57
3

I think your problem is that you are calling print to your subroutine, and print is already defined in perl.

Try changing the name of the subrutine, for instance, this works for me:

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";
    }
                 }
}
pconcepcion
  • 5,591
  • 5
  • 35
  • 53