-4

I have following code in Perl where a hash reference is passed from main function to func1 and then to func2. In func2 hash is updated. I want to access the updated hash in main function. Also there is a while loop in main function and i am expecting the hash should be updated during every iteration. The code might not look logical, but i have just written the skeleton of the code. I always get empty hash when i try to print the hash in main function

sub main {
      my %hash1;
      while (some condition)
      {
        my $i=0;
        if($i==0)
        { 
          func1($i,\%hash1);                      
          $i=1;
        }
        else
        {  
          func1($i,\%hash1);                      
          $i=0;
        }
      }
      foreach my $a (keys %hash1)
      {
        print "$hash1{$a}";
      }       
}
sub func1
{    
my ($i,$hash1)=@_;
----
if($i==0)
{ 
   func2($hash1);       
}

}

sub func2
{
my ($hash2)=@_;
$hash2->{key1}=1;
$hash2->{key2}=2;
}
Sam
  • 243
  • 1
  • 9
  • 22
  • Please check your code, since there is one closing } too much and it is unclear where your while-loop starts and where it ends. – Erik Jun 01 '11 at 14:22
  • Thanks Erik...I made the change – Sam Jun 01 '11 at 14:27
  • 2
    Correcting for the missing } and supplying a condition for the code you gave results in working code. The hash values are set. Please share the actual code with which you are having a problem. – Adam Bellaire Jun 01 '11 at 14:27
  • BTW IMHO a sub is called with a leading & in Perl - so the code should be &func1(), ... But Adam is right. Your abstraction of your problem is not usefull to dertect your problem and help you. – Erik Jun 01 '11 at 14:40
  • I have updated the code...So you can see the func2 is called only when i=0. If during the last iteration i=1, the hash is empty – Sam Jun 01 '11 at 14:43
  • I mean: naked i is not variable $i at all. – Dallaylaen Jun 01 '11 at 14:47
  • what happens in `func2` if you copy dereference your hash like this: `%hash3 = %$hash2` and then use `%hash3`? – Nathan Fellman Jun 01 '11 at 14:47
  • 2
    The code does not look like valid perl to me, and does not show the problem. Provide a working code we can copy and debug so that we can work on your problem by running the code on our machines – Erik Jun 01 '11 at 14:51
  • @Sam `int i` and `sub main` is not perl (well, you can `sub` whatever you want, but you do not need a main in a perl program). You should learn the basic syntax before learning about passing hash references. – TLP Jun 01 '11 at 15:07
  • @Dallaylaen `Can't locate warnigns.pm in @INC` ;) – TLP Jun 01 '11 at 15:10
  • @Sam: Drop the else in your while - it never can be reached, since in the beginning of your while-loop you always declare $i=0 - so everytime the first if-statement will be true. It's pretty annoying to get a randomly changing, invalid and not able to run code of you here! – Erik Jun 01 '11 at 15:14
  • Oh... `use warnings;`, sorry for the typo. – Dallaylaen Jun 01 '11 at 15:26

1 Answers1

1

Ah, I see. You think it's not printing anything as a result of the hash's being empty. But it's really not printing anything because you're not giving it anything to run. You're giving it stuff to compile, but nothing to run.

sub main means nothing in Perl. In order to get that to run, you have to put somewhere on your mainline main(); Then provided that you comment out the --- on line 25, you'll get the output you expect.

This is why printing stuff out yourself is either 1) a bit more typing, or 2) not reliable. You saw nothing and thought our hash was empty. When really, the code never even got to the declaration. So, here's a hint, on the command line:

 cpan Smart::Comments

And then, in your code:

use Smart::Comments;
...

### %hash1

That way an empty hash looks like this:

### %hash1: {}

And the one that you expect, looks like this:

### %hash1: {
###           key1 => 1,
###           key2 => 2
###         }

Without calling the main sub, your output looks like this:

(yes, it is blank)

Otherwise, there is nothing wrong with your passing the hash.

Axeman
  • 29,660
  • 2
  • 47
  • 102