pass by reference

pass by reference

am 09.08.2007 19:50:50 von Larry

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?

(I know in this simple example, I could avoid the typeglob assignment
and just use $$arg instead of $arg, but in a more complex usage, that
would bug me... I'd rather just use the more normal-looking $arg
everywhere.)

-------------

sub squareMe (\$) {
my ($argRef) = @_;
local *arg = $argRef;
our $arg;

$arg *= $arg;
}

my $n = 7;
squareMe $n;
print "$n\n"; # prints 49

Re: pass by reference

am 09.08.2007 19:59:16 von Billy Patton

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?
>
> (I know in this simple example, I could avoid the typeglob assignment
> and just use $$arg instead of $arg, but in a more complex usage, that
> would bug me... I'd rather just use the more normal-looking $arg
> everywhere.)
>
> -------------
>
> sub squareMe (\$) {
> my ($argRef) = @_;
> local *arg = $argRef;
> our $arg;
>
> $arg *= $arg;
> }
>
> my $n = 7;
> squareMe $n;
> print "$n\n"; # prints 49
>

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!

Re: pass by reference

am 09.08.2007 20:04:08 von Peter Makholm

Larry writes:

> 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?

Perl already implements a kind of pass by reference in the @_
argument.

#!/usr/bin/perl -l
sub squareMe {
$_[0] *= $_[0];
}

my $n = 7;
squareMe $n;
print $n; # Does indeed print 49

__END__

> (I know in this simple example, I could avoid the typeglob assignment
> and just use $$arg instead of $arg, but in a more complex usage, that
> would bug me... I'd rather just use the more normal-looking $arg
> everywhere.)

No, using pass by reference isn't the normal case, so hiding it will
only make you code more obscure and less maintainable when you forget
whatever samrtness you have put into the code.

Pass by reference can be useful, but keep it well documented and let
it stand out.

//Makholm

Re: pass by reference

am 09.08.2007 20:05:47 von Larry

On Aug 9, 2:04 pm, Peter Makholm wrote:
> Larry writes:
> > 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?
>
> Perl already implements a kind of pass by reference in the @_
> argument.
>
> #!/usr/bin/perl -l
> sub squareMe {
> $_[0] *= $_[0];
>
> }
>
> my $n = 7;
> squareMe $n;
> print $n; # Does indeed print 49
>
> __END__
>
> > (I know in this simple example, I could avoid the typeglob assignment
> > and just use $$arg instead of $arg, but in a more complex usage, that
> > would bug me... I'd rather just use the more normal-looking $arg
> > everywhere.)
>
> No, using pass by reference isn't the normal case, so hiding it will
> only make you code more obscure and less maintainable when you forget
> whatever samrtness you have put into the code.
>
> Pass by reference can be useful, but keep it well documented and let
> it stand out.
>
> //Makholm

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! :)

Re: pass by reference

am 09.08.2007 20:16:14 von Paul Lalli

On Aug 9, 2:05 pm, Larry wrote:

> > Larry writes:
> > > 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?

> 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!

So use your name the entire time you're actually doing something with
the variable, then change it back at the end:

$ perl -le'
my $x = 7;
squareMe($x);
print $x;

sub squareMe {
my ($arg) = @_;
$arg **= 2;
$_[0] = $arg;
}
'
49


Paul Lalli

Re: pass by reference

am 09.08.2007 20:28:07 von Jim Gibson

In article <1186682747.757896.223810@o61g2000hsh.googlegroups.com>,
Larry wrote:

> On Aug 9, 2:04 pm, Peter Makholm wrote:
> > Larry writes:
> > > 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?
> >
> > Perl already implements a kind of pass by reference in the @_
> > argument.
> >
> > #!/usr/bin/perl -l
> > sub squareMe {
> > $_[0] *= $_[0];


>
> 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! :)
>

$_ is a name, just not a pronounceable one!

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: pass by reference

am 09.08.2007 20:59:41 von Uri Guttman

>>>>> "L" == Larry writes:

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

so pass in a reference. the typeglob hack you created is fugly and
wasteful. and don't use prototypes as they are not what you want here. a
plain simple reference works fine.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: pass by reference

am 09.08.2007 21:07:12 von Larry

On Aug 9, 2:59 pm, Uri Guttman wrote:
> >>>>> "L" == Larry writes:
>
> L> I don't like the $_[0] method, because it doesn't give me a chance to
> L> name my variables.... I prefer names, not numbers, thank you! :)
>
> so pass in a reference. the typeglob hack you created is fugly and
> wasteful. and don't use prototypes as they are not what you want here. a
> plain simple reference works fine.
>

I don't want to pass in a reference! You call that "passing by
reference"?! :)

I want to write:

squareMe $foo

not

squareMe \$foo

!!!

Re: pass by reference

am 09.08.2007 21:08:19 von Larry

On Aug 9, 3:07 pm, Larry wrote:
> On Aug 9, 2:59 pm, Uri Guttman wrote:
>
> > >>>>> "L" == Larry writes:
>
> > L> I don't like the $_[0] method, because it doesn't give me a chance to
> > L> name my variables.... I prefer names, not numbers, thank you! :)
>
> > so pass in a reference. the typeglob hack you created is fugly and
> > wasteful. and don't use prototypes as they are not what you want here. a
> > plain simple reference works fine.
>
> I don't want to pass in a reference! You call that "passing by
> reference"?! :)
>
> I want to write:
>
> squareMe $foo
>
> not
>
> squareMe \$foo
>
> !!!

And I *do* need the prototypes... that's the only way to get the
reference without my callers having to use backslashes!

Re: pass by reference

am 09.08.2007 21:10:37 von Larry

On Aug 9, 1:50 pm, 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?
>
> (I know in this simple example, I could avoid the typeglob assignment
> and just use $$arg instead of $arg, but in a more complex usage, that
> would bug me... I'd rather just use the more normal-looking $arg
> everywhere.)
>
> -------------
>
> sub squareMe (\$) {
> my ($argRef) = @_;
> local *arg = $argRef;
> our $arg;
>
> $arg *= $arg;
>
> }
>
> my $n = 7;
> squareMe $n;
> print "$n\n"; # prints 49

I just figured out a somewhat more elegant way:

sub squareMe (\$) {
local (*arg) = @_;
our $arg;

$arg *= $arg;

}

Too bad about the need for having to repeat "arg" in "our" though...

Re: pass by reference

am 09.08.2007 21:17:40 von Larry

On Aug 9, 1:59 pm, Billy Patton 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?
>
> > (I know in this simple example, I could avoid the typeglob assignment
> > and just use $$arg instead of $arg, but in a more complex usage, that
> > would bug me... I'd rather just use the more normal-looking $arg
> > everywhere.)
>
> > -------------
>
> > sub squareMe (\$) {
> > my ($argRef) = @_;
> > local *arg = $argRef;
> > our $arg;
>
> > $arg *= $arg;
> > }
>
> > my $n = 7;
> > squareMe $n;
> > print "$n\n"; # prints 49
>
> 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!

Sometimes, I do want side effects! What if I know that every time I
call "squareMe" I will want to modify the var. being squared?

I'd rather write:

squareMe $n

than

$n = squareMe $n

Re: pass by reference

am 09.08.2007 21:27:08 von Uri Guttman

>>>>> "L" == Larry writes:

L> On Aug 9, 2:59 pm, Uri Guttman wrote:
>> >>>>> "L" == Larry writes:
>>
L> I don't like the $_[0] method, because it doesn't give me a chance to
L> name my variables.... I prefer names, not numbers, thank you! :)
>>
>> so pass in a reference. the typeglob hack you created is fugly and
>> wasteful. and don't use prototypes as they are not what you want here. a
>> plain simple reference works fine.
>>

L> I don't want to pass in a reference! You call that "passing by
L> reference"?! :)

sure is but it is just explicit. i know what pass by ref is.

L> I want to write:

L> squareMe $foo

L> not

L> squareMe \$foo

well, then use $_[0] as others have said. your typeglob answer is
horrible for such a simple issue. in my view you are working too hard
for a tiny syntactical gain. this is from a few weeks of coding
experience that i have :)

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: pass by reference

am 09.08.2007 21:28:06 von Uri Guttman

>>>>> "L" == Larry writes:

L> On Aug 9, 3:07 pm, Larry wrote:
>> On Aug 9, 2:59 pm, Uri Guttman wrote:
>>
>> > >>>>> "L" == Larry writes:
>>
>> > L> I don't like the $_[0] method, because it doesn't give me a chance to
>> > L> name my variables.... I prefer names, not numbers, thank you! :)
>>
>> > so pass in a reference. the typeglob hack you created is fugly and
>> > wasteful. and don't use prototypes as they are not what you want here. a
>> > plain simple reference works fine.
>>
>> I don't want to pass in a reference! You call that "passing by
>> reference"?! :)
>>
>> I want to write:
>>
>> squareMe $foo
>>
>> not
>>
>> squareMe \$foo
>>
>> !!!

L> And I *do* need the prototypes... that's the only way to get the
L> reference without my callers having to use backslashes!

nope again. they can get a ref earlier into another scalar and pass
that. your api design to make their life 'easier' is actually making it
harder. but do what you want.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: pass by reference

am 09.08.2007 21:29:07 von Uri Guttman

>>>>> "L" == Larry writes:

L> On Aug 9, 1:50 pm, 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?
>>
>> (I know in this simple example, I could avoid the typeglob assignment
>> and just use $$arg instead of $arg, but in a more complex usage, that
>> would bug me... I'd rather just use the more normal-looking $arg
>> everywhere.)
>>
>> -------------
>>
>> sub squareMe (\$) {
>> my ($argRef) = @_;
>> local *arg = $argRef;
>> our $arg;
>>
>> $arg *= $arg;
>>
>> }
>>
>> my $n = 7;
>> squareMe $n;
>> print "$n\n"; # prints 49

L> I just figured out a somewhat more elegant way:

L> sub squareMe (\$) {
L> local (*arg) = @_;
L> our $arg;

L> $arg *= $arg;

L> }

ewwwwww!

L> Too bad about the need for having to repeat "arg" in "our" though...

double ewwwwww.

tell them to pass a real ref and be done with it. they need to learn
about refs anyhow and coddling your callers is doing them a big
disservice.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: pass by reference

am 09.08.2007 21:39:36 von Larry

> tell them to pass a real ref and be done with it. they need to learn
> about refs anyhow and coddling your callers is doing them a big
> disservice.
>
> uri

I see your point, but I miss "real" passing by reference from Pascal
and Ada. But C, Perl and Java seem to want to bury it :(

Re: pass by reference

am 09.08.2007 21:40:49 von Larry

On Aug 9, 3:39 pm, Larry wrote:
> > tell them to pass a real ref and be done with it. they need to learn
> > about refs anyhow and coddling your callers is doing them a big
> > disservice.
>
> > uri
>
> I see your point, but I miss "real" passing by reference from Pascal
> and Ada. But C, Perl and Java seem to want to bury it :(

And put C++ in with Pascal and Ada!

Re: pass by reference

am 09.08.2007 22:23:38 von paduille.4061.mumia.w+nospam

On 08/09/2007 01:05 PM, Larry wrote:
>
> 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! :)
>

No problem. Just use typeglob aliases:

use strict;
use warnings;
our $argument;

$argument = 4;
squareMe($argument);
print $argument, "\n";

sub squareMe {
local *argument = \$_[0];
$argument *= $argument;
}

Re: pass by reference

am 09.08.2007 23:00:07 von glex_no-spam

Larry wrote:

> Sometimes, I do want side effects! What if I know that every time I
> call "squareMe" I will want to modify the var. being squared?

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.

As long as you are the only person who will ever use it,
then do whatever you like. Once you start writing code
that other people will use, or apply for a job and submit
code like this, you'll realize that what you 'want' is
totally the opposite of what most people want or expect.

Re: pass by reference

am 10.08.2007 00:39:12 von Uri Guttman

>>>>> "L" == Larry writes:

>> tell them to pass a real ref and be done with it. they need to learn
>> about refs anyhow and coddling your callers is doing them a big
>> disservice.
>>
>> uri

L> I see your point, but I miss "real" passing by reference from Pascal
L> and Ada. But C, Perl and Java seem to want to bury it :(

you still don't get it. perl has real pass by ref in @_. copying @_ to
lexicals makes it pass by value. you have both in perl and you also have
explicit pass by ref when you use refs which is a third option. perl is
more flexible about passing args than most langs. and wait until you see
perl 6! :)

and note that classic pass by ref was nasty too with unintended side
effects being a tough bug to find. perl allows that but the standard
idiom of copying @_ to lexicals eliminates that bug.

again, trust me when i say have the callers pass in a real ref. it is
safer, more explicit (less bug prone) and will teach them better
perl. very very few exported functions on cpan do pass by ref as you
want it. and don't even think about it for object oriented code as that
is even nastier in breaking OO contraints.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Re: pass by reference

am 10.08.2007 02:08:44 von Tad McClellan

Jim Gibson wrote:
> In article <1186682747.757896.223810@o61g2000hsh.googlegroups.com>,
> Larry wrote:
>
>> On Aug 9, 2:04 pm, Peter Makholm wrote:
>> > Larry writes:
>> > > 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?
>> >
>> > Perl already implements a kind of pass by reference in the @_
>> > argument.
>> >
>> > #!/usr/bin/perl -l
>> > sub squareMe {
>> > $_[0] *= $_[0];
>
>
>>
>> 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! :)
>>
>
> $_ is a name, just not a pronounceable one!


I've heard it pronounced "dollar underwear".


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: pass by reference

am 10.08.2007 09:59:31 von rvtol+news

Larry schreef:

> 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! :)

See Data::Alias.

--
Affijn, Ruud

"Gewoon is een tijger."

Re: pass by reference

am 10.08.2007 11:07:32 von Michele Dondi

On Thu, 09 Aug 2007 16:00:07 -0500, "J. Gleixner"
wrote:

>> Sometimes, I do want side effects! What if I know that every time I
>> call "squareMe" I will want to modify the var. being squared?
>
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 10.08.2007 11:08:21 von Michele Dondi

On Thu, 09 Aug 2007 18:05:47 -0000, Larry
wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 10.08.2007 11:09:29 von Michele Dondi

On Thu, 09 Aug 2007 11:28:07 -0700, Jim Gibson
wrote:

>$_ is a name, just not a pronounceable one!

In English, it's "it".


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 10.08.2007 14:03:05 von anno4000

Billy Patton wrote in comp.lang.perl.misc:
> 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

Re: pass by reference

am 10.08.2007 15:04:47 von hymie_

In our last episode, the evil Dr. Lacto had captured our hero,
Tad McClellan , who said:

>I've heard it pronounced "dollar underwear".

Thanks for reminding me. I have to go shopping tonight.

--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
------------------------ Without caffeine for 283 days ------------------------

Re: pass by reference

am 10.08.2007 16:32:47 von Lawrence Statton

Michele Dondi writes:
> On Thu, 09 Aug 2007 11:28:07 -0700, Jim Gibson
> wrote:
>
> >$_ is a name, just not a pronounceable one!
>
> In English, it's "it".

In English.pm it's "$ARG" ... :)

lawrence@hummer - /tmp % cat ex-1
#!/usr/bin/perl
use strict;
use warnings;
use English;

sub square_me {
$ARG[0] *= $ARG[0];
}

my $foo = 5;
square_me($foo);
print "$foo\n";

lawrence@hummer - /tmp % perl ex-1
25

--
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.

Re: pass by reference

am 10.08.2007 18:12:04 von glex_no-spam

Lawrence Statton wrote:
> Michele Dondi writes:
>> On Thu, 09 Aug 2007 11:28:07 -0700, Jim Gibson
>> wrote:
>>
>>> $_ is a name, just not a pronounceable one!
>> In English, it's "it".
>
> In English.pm it's "$ARG" ... :)

Shouldn't that be Pirates.pm?

Re: pass by reference

am 10.08.2007 19:05:15 von Lawrence Statton

"J. Gleixner" writes:

> Lawrence Statton wrote:
> > Michele Dondi writes:
> >> On Thu, 09 Aug 2007 11:28:07 -0700, Jim Gibson
> >> wrote:
> >>
> >>> $_ is a name, just not a pronounceable one!
> >> In English, it's "it".
> > In English.pm it's "$ARG" ... :)
>
> Shouldn't that be Pirates.pm?

Ahh, no -- tehre it's $ARRRRRRRRRRRRRRRRRRRRRRRR


--
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.

Re: pass by reference

am 10.08.2007 19:53:27 von Billy Patton

anno4000@radom.zrz.tu-berlin.de wrote:
> Billy Patton wrote in comp.lang.perl.misc:
>> 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" :)

Re: pass by reference

am 11.08.2007 11:15:47 von Michele Dondi

On 10 Aug 2007 09:32:47 -0500, Lawrence Statton
wrote:

>> In English, it's "it".
>
>In English.pm it's "$ARG" ... :)

No, that's "useless quoting of variables"...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 11.08.2007 11:47:29 von Michele Dondi

On Sat, 11 Aug 2007 11:15:47 +0200, Michele Dondi
wrote:

>>In English.pm it's "$ARG" ... :)
>
>No, that's "useless quoting of variables"...

I forgot to add :-p


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 11.08.2007 23:50:14 von savagebeaste

Michele Dondi wrote:
> On Thu, 09 Aug 2007 11:28:07 -0700, Jim Gibson
> wrote:
>
>> $_ is a name, just not a pronounceable one!
>
> 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 systax
that sets the line to $_ each time around.

--
CL

Re: pass by reference

am 12.08.2007 00:08:36 von savagebeaste

anno4000@radom.zrz.tu-berlin.de wrote:
> Billy Patton wrote in comp.lang.perl.misc:
>> 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

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.

:-)

--
CL

Re: pass by reference

am 12.08.2007 00:10:24 von savagebeaste

Billy Patton wrote:
> anno4000@radom.zrz.tu-berlin.de wrote:
>> Billy Patton wrote in comp.lang.perl.misc:
>>> 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)


--
CL

Re: pass by reference

am 12.08.2007 10:57:42 von Michele Dondi

On Sat, 11 Aug 2007 14:50:14 -0700, "Clenna Lumina"
wrote:

>>> $_ is a name, just not a pronounceable one!
>>
>> 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 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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: pass by reference

am 13.08.2007 15:08:09 von Billy Patton

Clenna Lumina wrote:
> Billy Patton wrote:
>> anno4000@radom.zrz.tu-berlin.de wrote:
>>> Billy Patton wrote in comp.lang.perl.misc:
>>>> 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 :)

Re: pass by reference

am 14.08.2007 21:53:45 von savagebeaste

Billy Patton wrote:
> Clenna Lumina wrote:
>> Billy Patton wrote:
>>> anno4000@radom.zrz.tu-berlin.de wrote:
>>>> Billy Patton wrote in comp.lang.perl.misc:
>>>>> 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.

--
CL