pass by reference

M

Michele Dondi

Fine. Everyone is telling you that it's not a good practice
and you're not going to convince anyone that it is a good practice.

Oh, C'mon! While I NEVER do that myself (guess I have a penchant for
fp - while never having dove into it) Perl is already so
side-effectful that it shouldn't matter, if one really knows what
she's doing.


Michele
 
M

Michele Dondi

I don't like the $_[0] method, because it doesn't give me a chance to
name my variables.... I prefer names, not numbers, thank you! :)

Wait for Perl 6. Or play with pugs already! ;-)


Michele
 
A

anno4000

Billy Patton said:
Larry said:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less verbose way
to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have to
track it down for debugging!

That raises the question why you are using the mutator "*=" at all. It
doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno
 
B

Billy Patton

Billy Patton said:
Larry said:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less verbose way
to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have to
track it down for debugging!

That raises the question why you are using the mutator "*=" at all. It
doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno

The beauty of perl "Always more than one way to skin a cat" :)
 
C

Clenna Lumina

Michele said:
In English, it's "it".

Yes, in the context of for loops*, map, and such, from what I understand
that's exactly what "it" is :)

* Though it doesn't seem to be the came of a while loop onless you're
reading a handle (file, pipe, socket, etc) using the <HANDLE> systax
that sets the line to $_ each time around.
 
C

Clenna Lumina

Billy Patton said:
Larry said:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less verbose
way to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have to
track it down for debugging!

That raises the question why you are using the mutator "*=" at all. It
doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno

Or do ya one better for a variable amount of arguments and
simultaneously increasing it's usefulness:

sub square_me { wantarray ? map { $_*$_ } @_ : $_[0]*$_[0]; }

my $sq = square_me 5;
print $sq, "\n";

my @squares = square_me 3, 7, 12;
print join(', ', @squares);

Output:

25
9, 49, 144

This allows it to be used in both scalar and list context, and of course
can be expanded for more useful algorithms of course.

:)
 
C

Clenna Lumina

Billy said:
Billy Patton said:
Larry wrote:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less verbose
way to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have to
track it down for debugging!

That raises the question why you are using the mutator "*=" at all.
It doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno

The beauty of perl "Always more than one way to skin a cat" :)

Just how many ways do you need to skin a cat :p yeeesh

(Seriously though, it's one the things I really love about Perl)
 
M

Michele Dondi

Yes, in the context of for loops*, map, and such, from what I understand
that's exactly what "it" is :)

* Though it doesn't seem to be the came of a while loop onless you're
reading a handle (file, pipe, socket, etc) using the <HANDLE> systax
that sets the line to $_ each time around.

It's more or less still "it", the rationale being that a natural
language concept like a pronoun can fit nicely in the model of a
programming one, but it won't map just *exactly*: Perl *resembles*
English in some points, but obviously it is *not* English:

: If I convert a program into English, I don't get Shakespeare, but
: Cobol. A subset of English fit for morons and guaranteed not to make
: use of its expressive powers.
: - David Kastrup in comp.text.tex, "Re: pdflatex or dvipdf ?"


Michele
 
B

Billy Patton

Clenna said:
Billy said:
Larry wrote:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less verbose
way to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have to
track it down for debugging!
That raises the question why you are using the mutator "*=" at all.
It doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno
The beauty of perl "Always more than one way to skin a cat" :)

Just how many ways do you need to skin a cat :p yeeesh

(Seriously though, it's one the things I really love about Perl)
Depends on the cat and your skinning tool :)
 
C

Clenna Lumina

Billy said:
Clenna said:
Billy said:
(e-mail address removed)-berlin.de wrote:
Larry wrote:
The following code snippet shows how I do "pass by reference" in
Perl. It works fine, but I'm wondering... is there a less
verbose way to do this?
[...]

Do you really want side effects ?
Passing by reference does this.
Guess this is too simple to make that decision.

sub squareMe($) {
my ($arg) = @_;
$arg *= $arg;
}

my $n=7;
$n = squareMe $n; # no side effects
I hate it when my variables get changed somewhere else and I have
to track it down for debugging!
That raises the question why you are using the mutator "*=" at all.
It doesn't do anything useful.

sub square_me {
my $arg = shift;
$arg*$arg;
}

would be exactly equivalent, as would be

sub square_me { $_[0]*$_[0] }

Anno
The beauty of perl "Always more than one way to skin a cat" :)

Just how many ways do you need to skin a cat :p yeeesh

(Seriously though, it's one the things I really love about Perl)
Depends on the cat and your skinning tool :)

Very true.

P.S. Please stay away from my cat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top