zsdc wrote:

> Mallik wrote:
>
> > Dear Friends,
> >
> > I need to pass 3 parameters to a subroutine, in which the
> > first parameter is an array and the last two parameters
> > are strings.
> >
> > @abc = qw(1 2 3);
> > $x = 4;
> > $y = 5;
> >
> > testsub(@abc, $x, $y);
>
> Hello Mallik,
>
> I've just read this thread and I'd like to add one more way to solve
> your problem in addition to solutions already posted by Joseph, William,
> Jim and David.
>
> This is quite a common and simple problem for which reading the whole
> perldoc perlsub might be somewhat too confusing, but asking about it on
> mailing lists usually starts discussions which often become arguments on
> whose favorite style is better, so I'll do my best to describe every
> possible way as objectively as I can, and then I'll say which style I
> would personally use in which circumstances.
>
> In general, you can pass an array to a subroutine in two ways: as a
> flattened list of scalars (its elements), or by reference. (Every
> subroutine call in Perl passes the arguments as a single flat list of
> scalars, but one of those scalars might just happen to be a reference to
> your array.)

Thans for an excellent summary.  I must admit I have been a bit strident in
advocating a "pass the references as explicit scalars" approach.  I really do
think that the use of references is a concept that beginners should be
encouraged to wrap their heads around from the start.  It really is a very
simple concept, complicated largely by a mystique of difficulty that has somehow
accrued around it.

I usually operate on the assumption that arrays are, or could grow to be, of
production scale.  With large scale, even in a by-value context, references
offer a great efficiency.  They also have implicit dangers.  The writer of any
function should be very careful to indicate whether the function operates on the
referents of the arguments.  The user of any function should be aware of whether
it operates on, or simply uses the values from, the original.  This sort of
attention is necessary in any case, though.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to