bayruds has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to pass empty/ undef values to a subroutine the following way:

$ret_val = my_sub($arg1, "", $arg2, "", "", $arg3); And in the subroutine: sub my_sub{ $arg1 = shift; $undef1 = shift; $arg2 = shift; $undef2 = shift; $undef3 = shift; $arg3 = shift; ....... and so on }

The problem is that in the subroutine, undef1 gets arg2 and arg2 gets arg3 since other values are undef. Is there anyway I can pass all the values and get all the values from the subroutine in the order they were defined irrespective of whether the value is "null" or Not.

Thanks

20030622 Edit by Corion: Changed text to paragraphs

Replies are listed 'Best First'.
Re: Passing Empty/ Undefined values to a subroutine
by tilly (Archbishop) on Jun 22, 2003 at 06:10 UTC
    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.

      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...

Re: Passing Empty/ Undefined values to a subroutine
by chromatic (Archbishop) on Jun 22, 2003 at 05:31 UTC

    It works for me. Which version of Perl are you using?

    sub my_sub { my $arg1 = shift; my $arg2 = shift; my $arg3 = shift; my $arg4 = shift; print defined $arg1 ? "$arg1\n" : "undefined\n"; print defined $arg2 ? "$arg2\n" : "undefined\n"; print defined $arg3 ? "$arg3\n" : "undefined\n"; print $arg4 ? "$arg4\n" : "false/null\n"; } my_sub( 'string', undef, 123, '' );
      Hi

      Thanks for replying back. I am using perl5.6.0

      Thanks

      edited: Mon Jun 23 02:40:31 2003 by jeffa - removed (yeah, removed!) code tags, formatting

Re: Passing Empty/ Undefined values to a subroutine
by antirice (Priest) on Jun 22, 2003 at 06:10 UTC

    To see what is actually being passed to my_sub, you may want to try using Data::Dumper to see what @_ actually contains.

    #!/usr/bin/perl -w use Data::Dumper; use strict; my ($arg1,$arg2,$arg3) = qw(arg1 arg2 arg3); my $ret_val = my_sub($arg1, "", $arg2, "", "", $arg3); sub my_sub{ print Dumper(\@_); my $arg1 = shift; my $undef1 = shift; my $arg2 = shift; my $undef2 = shift; my $undef3 = shift; my $arg3 = shift; ....... and so on }

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Update: oops responded to the wrong thing. sorry abvout that.

      Post code that runs and produces the results you see.

      Replacing the non-runnable

      ....... and so on

      with

      print "$arg1,$arg2,$arg3\n";

      produces output of:
      $VAR1 = [ 'arg1', '', 'arg2', '', '', 'arg3' ]; arg1,arg2,arg3


      which is the expected reesult. So what are you actuallydoing which doesn't work as expected?

      --Bob Niederman, http://bob-n.com
Re: Passing Empty/ Undefined values to a subroutine
by Zaxo (Archbishop) on Jun 22, 2003 at 05:51 UTC

    Please show this happening in code we can run. What you report is wrong and nothing would ever work if it were true.

    After Compline,
    Zaxo

Re: (dup; please do not reply) Passing null values to a subroutine
by castaway (Parson) on Jun 22, 2003 at 06:16 UTC
    Your example as given works fine here, maybe you should post the code you're actually using?

    C.

    Test:

    perl -le'$ret_val = my_sub(1, "", 2, "", "", 3); sub my_sub{$arg1 = sh +ift;$undef1 = shift;$arg2 = shift;$undef2 = shift;$undef3 = shift;$ar +g3 = shift; print "Arg1:", $arg1, " Undef1:", $undef1, " Arg2:", $arg +2, " Undef2:", $undef2; }' Prints: Arg1:1 Undef1: Arg2:2 Undef2:
    Oops, browser didnt refresh properly, honest!
Re: Passing Empty/ Undefined values to a subroutine
by bayruds (Acolyte) on Jun 23, 2003 at 03:36 UTC
    I over came the problem by passing a reference to an array. Rather than passing the array itself. It works now!. Thank you all for the help.

    -BaySurfs

    edited: Tue Jul 1 13:50:41 2003 by jeffa - removed code tags