On Fri, 2007-04-27 at 12:03 -0400, Chas Owens wrote:
> On 4/27/07, Pierre Mariani <[EMAIL PROTECTED]> wrote:
> snip
> > > - modify_variable() doesn't appear to modify anything, otherwise why
> > > are you assigning its return value to the scalar passed as a parameter?
> > > It seems to be just a function.
> >
> > Modify_variable modifies its input variable.
>
> I think the issue is what you mean by "modifies". In order to say
> that the modify_variable function modifies its input then something
> like the following must be true:
>
> my $foo = 5
> modify_variable($foo)
> print "$foo\n"; #prints "6\n"
>
Yeah, that's exactly it.
>
> >
> > > - Why are you using references? Are you sure you need to?
> > >
> > Please correct me if I am wrong.
> > My understanding is that:
> > 1) if I do:
> > my @array = ($a, $b, $c);
> > for (@array) { $_ = modify_variable($_)}
> > I am going to modify $array[0], $array[1] and $array[2], and NOT $a, $b,
> > $c.
>
> Yes, that is correct, but this is most likely an invalid use of an
> array. Either you should be using a array from the beginning, or you
> should be using a list. Assigning a bunch of scalar variables to an
> array just to group them is useless.
Agree on that, but given other details that I didn't communicate, it is
not quite possible (this is actually a pl/perlu function in postgresql,
the variables I am working on are passed to the function, and their type
is limited by the function definition, so unless I create a composite
type I can't pass them as an array directly).
> $_ = modify_variable($_) for $a, $b, $c;
>
> is a better construct. However, it would be even better to make
> modify_variable take multiple arguments:
>
> ($a, $b, $c) = modify_variables($a, $b, $c);
I agree on that (even better would be to make it work on references),
but unfortunately I can't modify it.
> > 2) if I do:
> > for ($a, $b, $c) {$_ = modify_variable($_)}
> > I am going to modify $a, $b, $c, which is good, but if $a, $b, $c are
> > big I am going to be passing around lots of data.
>
> Ah, here is the crux of the problem. You thing you are passing
> copies. Perl aliases its arguments to functions. In C++ terms you
> are passing by reference (if I got the terminology right). Passing
> "this is a really long string" and "a" both take up the same amount of
> time and memory. Only passing around arrays and hashes take up O(n)
> space and time. That is why it is good form to pass them as
> references.
>
> snip
> > > $_ = function($_) foreach ($var1, $var2, $var3);
> > >
> > > Will this do? Or is there more to the problem than you've explained?
> snip
>
> $_ = function($_) foreach ($var1, $var2, $var3);
>
> is the same as
>
> foreach ($var1, $var2, $var3) {
> $_ = function($_)
> }
>
> If you only have one statement* in a loop for can write it in the
> former method. It is considered by many to be clearer.
>
>
> * well, you can chain statements using , but that is poor form outside
> of Perl Golf, Obfuscation, and one-liners.
Thank you for the information. I was familiar with this kind of syntax
but never used it.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/