SPUG: Pasing hash references?

Adam Monsen adamm at wazamatta.com
Sun Oct 27 15:14:20 CST 2002


On 27-Oct-2002 12:11 -0800, Jay Scherrer wrote:
> Hello,
> I've been trying to pass the contents of a newly created class hash to
> another subroutine. So far I can assign $values to the derived $keys
> individually.  But I'm having trouble sending the whole "key=>value"
> contents of the new hash out to another subroutine for further
> processing. Any suggestions?
> Jay

Hey Jay,

Please provide a short code sample that demonstrates the problem you're
experiencing if you want specific help. That said, here are two ways to
pass a hash into a subroutine.

  #!/usr/bin/perl -w
  use strict;
  use Data::Dumper;
  
  my %hash = ( 'blah' => '178', 'foo' => '296' );
  my_sub(%hash);   # pass hash as a list
  my_sub2(\%hash); # pass hash reference
  
  sub my_sub {
    my %local_hash = @_;
    print "in my_sub()\n";
    print "\%local_hash has: \n", Dumper(\%local_hash);
  }
  
  sub my_sub2 {
    my $local_hashref = $_[0];
    print "in my_sub2()\n";
    print "\$local_hashref has: \n", Dumper($local_hashref);
  }

-- 
Adam Monsen

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list