Passing a reference as a reference

Discussion in 'Perl Misc' started by pamelamishra@gmail.com, Aug 16, 2007.

  1. Guest

    Hi,

    I have this question about Perl references.
    Suppose i have this %hash which has been populated with key -
    values.Subroutine check1 gets the %hash as a refrence from the main
    and this refrence has to be passed to another subroutine check2. How
    can this be done.Any suggestions would be helpful.
    ##############################
    my %hash;
    check1(\%hash);

    sub check1
    {
    my ($hash)=shift;
    foreach my $keys (keys $hash)
    {
    print"$hash{$keys}\n";
    }
    check2 (I want to send the reference of %hash--which is already a
    reference);
    }

    sub check2
    {
    my ($hash)=shift;
    }

    ######################
    Thanks
     
    , Aug 16, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Aug 16, 1:04 pm, wrote:
    > I have this question about Perl references.
    > Suppose i have this %hash which has been populated with key -
    > values.Subroutine check1 gets the %hash as a refrence from the main
    > and this refrence has to be passed to another subroutine check2. How
    > can this be done.Any suggestions would be helpful.
    > ##############################
    > my %hash;
    > check1(\%hash);
    >
    > sub check1
    > {
    > my ($hash)=shift;


    All fine so far. Only minor problem you have is that you've named
    your hash reference $hash. I would call it something like $h_ref or
    $hash_ref or something similar.

    > foreach my $keys (keys $hash)


    Problem here. $hash is not a hash. It's a reference to a hash. If
    you want to get at the hash that it references, you have to
    dereference the reference:

    foreach my $keys (keys %{$hash})

    > {
    > print"$hash{$keys}\n";


    Again. $hash is not a hash. You have to dereference:

    print "$hash->{$keys}\n";

    > }



    > check2 (I want to send the reference of %hash--which is already a
    > reference);


    No. %hash is the hash that you created in the "main" part of the
    script. $hash is a reference to %hash.

    There's nothing magical here. Just pass the reference directly:

    check2($hash);

    >
    > }
    >
    > sub check2
    > {
    > my ($hash)=shift;
    >
    > }


    Again, though, I rather strongly recommend you choose better names for
    your variables. Specifically, name references with some form of
    "_ref" on them, to remind you what they actually are.

    Paul Lalli
     
    Paul Lalli, Aug 16, 2007
    #2
    1. Advertising

  3. Guest

    wrote in message-id: <>

    >
    > Hi,
    >
    > I have this question about Perl references.
    > Suppose i have this %hash which has been populated with key -
    > values.Subroutine check1 gets the %hash as a refrence from the main
    > and this refrence has to be passed to another subroutine check2. How
    > can this be done.Any suggestions would be helpful.
    > ##############################
    > my %hash;
    > check1(\%hash);
    >
    > sub check1
    > {
    > my ($hash)=shift;
    > foreach my $keys (keys $hash)
    > {
    > print"$hash{$keys}\n";
    > }
    > check2 (I want to send the reference of %hash--which is already a
    > reference);
    > }
    >
    > sub check2
    > {
    > my ($hash)=shift;
    > }
    >
    > ######################
    > Thanks


    If im not mistaken check2($hashRef) should work fine, is there a particular
    reason why you want to pass a reference to a reference rather than just pass
    the reference?
     
    , Aug 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    593
    E. Robert Tisdale
    Jul 16, 2003
  2. Victor Bazarov
    Replies:
    25
    Views:
    968
    E. Robert Tisdale
    Mar 23, 2005
  3. pereges
    Replies:
    7
    Views:
    464
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    512
    Pascal J. Bourguignon
    Sep 13, 2009
  5. deadpickle
    Replies:
    1
    Views:
    1,096
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page