Ok, in this thread tilly was wondering why I had to pass my args via reference. Here is why:

We had a homegrown DBI here and the boss mandated that we swap it out in place of DBI. However, all existing code had to work without changes. So far, so good. However, we also had to make a feature extension. Before, when calling stored procedures, there was no way to pass in a set of scalars to be modified as OUT parameters. So stored procs could only be called with IN parms.

So, the whole idea was before people would do:

my $retval = $sth->exec_sp($val1, $val2);
And the only way to extend this with functionality for dealing with new OUT parms without breaking any existing code was to "earmark" one of the scalars. And the best way to do that is for that scalar to be an array ref. Thus to make a call to a stored proc and expect OUT parms to be bound is now:
my $retval = $sth->exec_sp($val1, $val2, [ \($outval1, $outval2) ]);
So, then all I do is run through @_ and splice out any scalar which is an array ref and consider those values as the ones to do a DBI bind_param_inout() on.

Ok, so now my earlier unexplained software engineering rationale is now explained.

2001-04-15 Edit by Corion : Moved to Meditations

Replies are listed 'Best First'.
(tye)Re: Non-invasive API extension by use of array refs
by tye (Sage) on Apr 15, 2001 at 10:59 UTC

    I would prefer using:

    my $retval = $sth->exec_sp($val1, $val2, \$outval1, \$outval2 ) ); # which is the same as: my $retval = $sth->exec_sp($val1, $val2, \( $outval1, $outval2 ) );
    That is, tag output parameters by making them simply scalar references instead of references to arrays of references to scalars. Besides making the calling code simpler, it seems to me it would also simplify your called code.

    Passing in a reference to a scalar is particularly nice because it clues the reader into the fact that that particular variable is likely to be modified. This even matches the calling conventions of bind_param() vs. bind_param_inout().

    But then I'm looking at it from farther away than you. Sometimes that is helpful in preventing tunnel vision and sometimes it just means I don't see as well. (: So what advantage do you see to using the extra anonymous array? If you don't mind my asking.

            - tye (but my friends call me "Tye")
Question
by Anonymous Monk on Apr 15, 2001 at 03:30 UTC

    I was just wondering why you chose

    my $retval = $sth->exec_sp($val1, $val2, [ \($outval1, $outval2) ]);

    instead of

    my $retval = $sth->exec_sp($val1, $val2, $outval1, $outval2 );

    with exec_sp modifying the $_[2] and $_[3] values in place.