Click to See Complete Forum and Search --> : perl: foo(/@array) vs. foo(@array)
YaRness
01-24-2001, 11:38 AM
most of my subs where i'm modifiying parameters like that are called with foo(/@array). but perl has functions like "push" and "chomp" etc, that don't need a reference passed, but they still modify the parameter given to it. howdya do that?
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-24-2001, 02:51 PM
YaR:
After doing some looking in 'Programming Perl 3rd edition' I came across a section (on page 220) that details this. The trick with adjusting the values of passed parameters without having to pass a reference is how you handle the passed parameters inside the sub. When you assign the @_ to variables, as:
sub changeToUppercase
{
my(@wordList)=@_;
for(@wordList) {
tr/a-z/A-Z/;
print;
}
}
it's considered passed by value. When you deal with the @_ directly, as:
sub changeToUppercase
{
for(@_) {
tr/a-z/A-Z/;
print;
}
}
it's considered passed by reference. And, changing the data of a reference alters the data permanently. http://www.linuxnewbie.org/ubb/smile.gif
So, that's how you adjust the values of passed parameters without using references. Of course, if you push items onto @_ inside the sub, the pushed items are not reflected outside the sub.
But, pretty qool.. http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-24-2001, 03:18 PM
i shoulda thought to just try that, but i'm doing 30 things at once here. thanks duck.
looks like with multiple parameters, that could be some pretty ugly stuff, but that's sometimes not as ugly as making the calls hafta be references.
been doing some OO stuff today, about to update my Yar package i've been playing/learning with in another thread.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
YaRness
01-26-2001, 03:22 PM
GROWL.
i can't make it do what i want. damn i should look at the code for "shift". i want to be able to do something like
thingie(@list);
and remove or add stuff to @list inside thingie. if shift can do it, why can't i. GRRR.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
YaRness
01-26-2001, 03:30 PM
AHA!
#!/usr/local/bin/perl -w
use strict;
use warnings;
#
#magic is in the "\@" here...
sub thingie(\@);
#
my @list = (1,2,3,4,5);
#
thingie(@list);
print "after shift_until: @list\n";
#
#...and more importantly here
sub thingie(\@)
{
shift @{$_[0]};
}
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
YaRness
01-26-2001, 03:35 PM
and so i have a new "shift_until" function, which i've prolly mentioned before.
demonstrated here (shifts off stuff in @list into limbo until it finds pattern. returns true when found, false if it hits EOF):
#!/usr/local/bin/perl -w
use strict;
use warnings;
sub shift_until($\@);
my @list = (1,2,3,4,5);
shift_until("3", @list);
print "after shift_until: @list\n";
sub shift_until($\@)
{
my $pattern = shift;
my $array_ref = \@{$_[0]};
while (@$array_ref and not $$array_ref[0] =~ /$pattern/)
{
shift @$array_ref;
}
(@$array_ref) ? return 1 : return 0;
}
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-26-2001, 05:39 PM
So, when you prototype a sub with a \@, in will convert the array into a reference for you.. qool.. I tried using that before and kept trying to pass it references, and it didn't like the reference.
So, now I know how that works. http://www.linuxnewbie.org/ubb/smile.gif
I do have a question for you, though.
my $array_ref = \@{$_[0]};
Is there a reason for dereferencing and then rereferencing the reference? In my tests of this, $_[0] held a reference, as defined by the prototype. Then, @{} it turns it into an actual array, then \ turns it back into a reference.. when I printed them out, it printed the exact same memory location.
Does it catch an error, or am I missing the point? Just curious... http://www.linuxnewbie.org/ubb/smile.gif
I'm also partial to using
my($pattern,$arrayref)=@_;
Instead of shifting the array. I dunno if it makes a difference or not. http://www.linuxnewbie.org/ubb/smile.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-26-2001, 10:43 PM
actually i HAD to reference that with the @{} i think to get it to work properly (i couldn't just do @$_ or something, i forget). i just referenced it because it was easier to look at a variable than a bunch of @{@_}{}_@@}{_@@{} looking things.
now that i look at it though, maybe it could just be $ref = $_[0] or whatever. i dunno. didn't try it.
i did the $pattern = shift just to be perl-ish http://www.linuxnewbie.org/ubb/biggrin.gif
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
TheLinuxDuck
01-27-2001, 12:03 AM
Originally posted by YaRness:
i did the $pattern = shift just to be perl-ish http://www.linuxnewbie.org/ubb/biggrin.gif
::grin:: I noticed that you use it quite a bit. I just hadn't ever used it much.
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-27-2001, 09:57 AM
well i've been using shift a lot because of the stuff i've been working on. but the examples of modules i looked at, a lot of the subroutines pulled off parameters like that, instead of the (@list) = @_ kinda thing we've been doing.
just trying out different stuff.
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
YaRness
01-29-2001, 10:12 AM
Originally posted by TheLinuxDuck:
Is there a reason for dereferencing and then rereferencing the reference?
you were right, it just needs to be
my $array_ref = $_[0];
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
[This message has been edited by YaRness (edited 29 January 2001).]
TheLinuxDuck
01-29-2001, 11:32 AM
What is this in reference to?
::giggle:: Get it!? Reference? Reference!
Ok...nevermind.. http://www.linuxnewbie.org/ubb/wink.gif
------------------
TheLinuxDuck
I have a belly button.
:wq
YaRness
01-29-2001, 12:23 PM
Originally posted by TheLinuxDuck:
What is this in reference to?
::giggle:: Get it!? Reference? Reference!
Ok...nevermind.. http://www.linuxnewbie.org/ubb/wink.gif
HAR HAR HAR http://www.linuxnewbie.org/ubb/tongue.gif
you bring new meaning to JAPH:
Just Another Punny Helper
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/