Trouble passing arguments to subroutine

L

laredotornado

Hi,

I want to create a function that takes a hash (by reference) and a
scalar as arguments. I'm trying ...

sub add_to_block_hash
{
my (%blocks, $curBlock) = @_;
print "curblock: $curBlock\n"; # line 33
....


add_to_block_hash(\%blocks, $curBlock);


but I'm getting the error, "Use of uninitialized value in
concatenation (.) or string at test.pl line 33" in which line 33 is
marked above. How do I correct this? THanks, - Dave
 
J

J. Gleixner

laredotornado said:
Hi,

I want to create a function that takes a hash (by reference) and a
scalar as arguments. I'm trying ...

sub add_to_block_hash
{
my (%blocks, $curBlock) = @_; ^^^^^^^ hash, not a reference
print "curblock: $curBlock\n"; # line 33
...


add_to_block_hash(\%blocks, $curBlock); ^^^^^^^ reference to a hash


but I'm getting the error, "Use of uninitialized value in
concatenation (.) or string at test.pl line 33" in which line 33 is
marked above. How do I correct this? THanks, - Dave

sub add_to_block_hash {
my ($blocks, $curBlock) = @_;

Then, dereference $blocks.

e.g.
for my $k ( keys %$blocks ) { ...}
 
J

Jürgen Exner

laredotornado said:
I want to create a function that takes a hash (by reference) and a
scalar as arguments. I'm trying ...

sub add_to_block_hash
{
my (%blocks, $curBlock) = @_;
^^^^^^^
This hash will suck up all the arguments provided, leaving nothing for
$curBlock. And because you are passing an even number of arguments you
are not even getting a warning that you are trying to create a hash with
an odd number of elements.
If you are passing a hash ref, then you need to treat it as hash ref:

my ($blockRef, $curBlock) = @_;

jue
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top