in reply to Passing Empty/ Undefined values to a subroutine

Are you sure that you have the quotes in the empty entries?

When I was a beginner I saw something that I thought was exactly like the behaviour you describe, in a situation where the call looked like, my_sub($arg1,,$arg2,,,$arg3) - obviously I was confused and what I thought from experience with another language would be an empty argument (the space between the commas) was actually a list of length 0, and therefore did not get passed.

The solution was to actually include an explicit value there. So I would recommend that you go back to your code and see whether you might just have a pair of commas there, and see if that is the issue.

If it is not, then please produce the smallest self-contained example that you can which runs and clearly demonstrates the problem.

Replies are listed 'Best First'.
Re: Re: Passing Empty/ Undefined values to a subroutine
by chanio (Priest) on Jun 22, 2003 at 16:50 UTC
    I would pass a reference to a hash array.
      I prefer giving answers which I believe the asker will understand.

      While there is something to be said for passing an anonymous hash if you have a lot of arguments, understanding that requires references, which someone who is still figuring out the calling semantics probably isn't ready to deal with. Also I hate leaving someone confused in a position where they feel, "I don't know why that didn't work, I don't know why this does, it is all magic to me." That leads to fear and cargo-cult programming.

      However since you apparently do understand that answer, I'll give you another one. Read Pass by reference vs globals to see some of the issues that using references to hash arrays for lots of parameters can allow to grow...

        Hi, I have attached the code and output below.. Thanks #! /usr/bin/perl print "Testing Subroutine parameter passing\n"; my $var1 = "var1"; my $var2 = "var2"; my_sub1($var1, $var2); print "End Test\n"; sub my_sub1 { my $var1 = shift; my $var2 = shift; my ($var3, $var4); my @undef1 = (); if ($var1 eq "var2") { $var3 = $var1; } if ($var2 eq "var2") { $var4 = $var2; } print_vars($var1, $var3, @undef1, $var4); } sub print_vars{ my $var1 = shift; my $var3 = shift; my @undef1 = shift; my $var4 = shift; print "Var1: $var1\n"; print "var3: $var3\n"; print "undef1: @undef1\n"; print "var4: $var4\n"; } OUTPUT: Testing Subroutine parameter passing Var1: var1 Use of uninitialized value in concatenation (.) at ./passtest.pl line +32. var3: undef1: var2 Use of uninitialized value in concatenation (.) at ./passtest.pl line +34. var4: End Test