ktom wrote:
> I have to subs in which i am attempting to pass multiple args to and i
> have managed to get it to work, after spending all afternoon trying to
> get my brain around it. I still don't have my brain around and the
> solution seems absurd!!
>
> below are snippets...
>
> 1st call:
>
> &printVec( \@spline, \@indx, \@port1 );
----------^
Any more (that is, with Perl's less than many years old), you shouldn't
use the & in your call unless you actually want the specific
functionality it provides. See:
perldoc perlsub
for details.
> }
>
> 1st working sub:
>
> sub printVec {
> #our @spline;
> # my (@spline,@indx,@port) = @_ ;
> # print "pre @$spl, @$indx, @$port\n";
> # print "spline @spline\n";
> my ($spl,$indx,$port) = @_ ;
> my @prt = @$port ;
> my @ndx = @$indx ;
> my @spline = @$spl ;
These latter three statements are OK if you actually want to make a copy
of the three arrays in your sub. It is not necessary, and, if the
purpose of your sub is to modify values in the arrays, won't work unless
you copy the modified values back to the arrays before you return. You
can instead access elements of the arrays as, for example:
$$port[3]='something or other';
@$indx[3..6]=(4..7);
$val=$$spl[23];
or you can use notation like:
$port->[3]='something or other';
etc if you prefer a "cleaner" implementation (at least according to some
definition of "clean").
>
>
> 2nd call:
>
> &printTset( \@indx, $vect, $cmmnt ) ;
> }
>
> 2nd working sub:
>
> sub printTset {
> my ( $indx, $vct, $cmt) = @_ ;
> my @ndx = @$indx;
> my $vect = $vct;
> my $cmmnt = $cmt;
Again, you don't have to make local copies of the data unless that is
really what you want to do. In this case, passing the scalars directly
is fine, since they will always have just a single value in @_.
>
> if i don't use the \ on the passed parameters, it seems to merge them
> all into one big array.. blahh.
Yep. That's the way Perl's argument calling semantics work. It's a
feature, not a bug or problem

.
>
> this seems like a lot of code, seemingly redundant to my wee perl mind,
> to pass values to a subroutine.
Well, it isn't that much overhead when writing the code -- one extra $
or @ for each time you want to reference an item. Plus don't forget you
can also pass hashes, globs, and references (and maybe some other stuff
I'm forgetting or don't know about) this way too. And of course, hashes
of arrays of hashes, etc etc.
>
> what magic am i missing or is this just the way it is???
Nope, that's the way it is. Except you shouldn't make local copies
unless there is a good reason why.
....
--
Bob Walton
--
Bob Walton