Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parameters to functions BY REFERENCE

Status
Not open for further replies.

EDHills

Programmer
Jan 24, 2007
35
0
0
US
# First, let me say thank you in advance for your time.
# I've spent over three hours digging, reading,
# searching, experimenting so far and #am frustrated.
# I am still (obviously) in the first few weeks of my
# perl experiance. I have written many programs in C,
# C++ and VB .NET and I KNOW, that there must be a way
# to pass a parameter to a function BY REFERENCE
# (i.e. changing the parameter's value in the function)


# THE CURRENT OUTPUT
myVal_1 = Original Val_1, myVal_2 = Original Val_2, myVal_3 = Original Val_3
myVal_1 = Original Val_1, myVal_2 = Original Val_2, myVal_3 = Original Val_3
myVal_1 = Original Val_1, myVal_2 = Original Val_2, myVal_3 = Original Val_3


# THE DESIRED OUTPUT
myVal_1 = Original Val_1, myVal_2 = Original Val_2, myVal_3 = Original Val_3
myVal_1 = New Value 1, myVal_2 = New Value 2, myVal_3 = New Value 3


# THE PROGRAM AS IT STANDS
Main();

sub Main
{

my $myMainVal_1 = "Original Val_1";
my $myMainVal_2 = "Original Val_2";
my $myMainVal_3 = "Original Val_3";

PrintVals($myMainVal_1,$myMainVal_2,$myMainVal_3);
ChangeVals($myMainVal_1,$myMainVal_2,$myMainVal_3);
PrintVals($myMainVal_1,$myMainVal_2,$myMainVal_3);
ChangeVals(\$myMainVal_1,\$myMainVal_2,\$myMainVal_3); # I thought this was the syntax for
# passing by reference, but same result
PrintVals($myMainVal_1,$myMainVal_2,$myMainVal_3);

print("end, placeholder for breakpooint");
}

sub ChangeVals
{
my $myVal_1 = shift;
my $myVal_2 = shift;
my $myVal_3 = shift;

$myVal_1 = "New Value 1";
$myVal_2 = "New Value 2";
$myVal_3 = "New Value 3";
}

sub PrintVals
{
my $myVal_1 = shift;
my $myVal_2 = shift;
my $myVal_3 = shift;
print ("myVal_1 = $myVal_1, myVal_2 = $myVal_2, myVal_3 = $myVal_3 ");
}
 
ok, so this works, but darn it's ugly.
Do I really have to refer to my parameters like this vs named in order to get pass by ref to work? yuck

sub ChangeVals2
{
$_[0] = "New Value 1";
$_[1] = "New Value 2";
$_[2] = "New Value 3";
}
 
If you're passing in references, then you must dereference them to assign to their scalar values. Otherwise you're just assigning to the variable holding the reference:

Code:
[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]ChangeVals[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [red]([/red][blue]$ref1[/blue], [blue]$ref2[/blue], [blue]$ref3[/blue][red])[/red] = [blue]@_[/blue][red];[/red]

	[blue]$$ref1[/blue] = [red]"[/red][purple]New Value 1[/purple][red]"[/red][red];[/red]
	[blue]$$ref2[/blue] = [red]"[/red][purple]New Value 2[/purple][red]"[/red][red];[/red]
	[blue]$$ref3[/blue] = [red]"[/red][purple]New Value 3[/purple][red]"[/red][red];[/red]
[red]}[/red]

- Miller
 
I saw a very very similar question posted on another forum in recent days, here is the code from that post:

Code:
#!usr/bin/perl
my $a = "one";
my $b = "two";
my $c = "three";
my $d = "four";

print join(" ", $a, $b, $c, $d), "\n";
print sub1($a, $b, $c, $d), "\n";
print sub2($a, $b, $c, $d), "\n";
print sub3($a, $b, $c, $d), "\n";
print join(" ", $a, $b, $c, $d), "\n";

sub sub1 {
  my ($var1, $var2, $var3, $var4) = @_;
  return sub2($var1, $var2, $var3, $var4);
}

sub sub2 {
  my $var1 = shift;
  my $var2 = shift;
  my $var3 = shift;
  my $var4 = shift;
  return  sub3($var2, $var1, $var4, $var3);
}

sub sub3 {
  $_[0] = "i";
  $_[1] = "think";
  $_[2] = "therefore";
  $_[3] = "i am";
 
}

while to code is different it appears to be the same lesson. Is this school/class/course work of some sort?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I saw a very very similar question posted on another forum in recent days, here is the code from that post:

Code:
#!usr/bin/perl
my $a = "one";
my $b = "two";
my $c = "three";
my $d = "four";

print join(" ", $a, $b, $c, $d), "\n";
print sub1($a, $b, $c, $d), "\n";
print sub2($a, $b, $c, $d), "\n";
print sub3($a, $b, $c, $d), "\n";
print join(" ", $a, $b, $c, $d), "\n";

sub sub1 {
  my ($var1, $var2, $var3, $var4) = @_;
  return sub2($var1, $var2, $var3, $var4);
}

sub sub2 {
  my $var1 = shift;
  my $var2 = shift;
  my $var3 = shift;
  my $var4 = shift;
  return  sub3($var2, $var1, $var4, $var3);
}

sub sub3 {
  $_[0] = "i";
  $_[1] = "think";
  $_[2] = "therefore";
  $_[3] = "i am";
 
}

while the code is different it appears to be the same lesson. Is this school/class/course work of some sort?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
freaking TT website has been acting strange lately, sorry for the double-post.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sweet MillerH, that looks like what I want, the method for dereferencing! I'll try that. Thanks KevinADC also, but that method was the method that I was trying to avoid. I like meaningful variable names rather than obscure syntax like $_[0]
 
Is this school/course/class work?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
not school, maintaining / enhancing some code that's being used in our business.

I am just a little anal and like my code to be a certain way. It helps me wrap my head around things to not use obscure syntax. I've been writing code for the masses for a long time, and often ran into people writing code that I'd have to stare at to decipher, I prefer quick and easy to read code, faster maintanance and less questions should I pass it along to the next guy one day

 
OK, I just thought it looked very similar to the code/question posted on the other forum which made me think it might be school work. With perl you have to be careful using the term reference, which is a special way to treat data, which is not the same as passing paramaters to and from functions. Here is an example of passing data to a function using a reference to the data:

Code:
@array = qw(foo bar);
show_me(\@array);
sub show_me {
   my $array = shift;
   foreach my $var ( @{$array_ref} ) {
      print "$var\n";
   }
}

References are very useful, if you continue to learn perl you will definetly want to learn and use them.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It seems like I saw a good example of this. It was something along the lines of "overloaded functions"

Basically, you use parenthesis next to the sub's name for the variables it receives, i.e.

Code:
my $pet = 'dog';
my $color = 'black';
my @needs = ('food','water');

&test ($pet,$color,@needs);

sub test ($\$\@) {
   my $pet2 = shift;
   my $color2 = shift;
   my @needs2 = @_;

   $pet2 = 'cat'; # does not affect $pet: not a reference
   $color2 = 'white'; # $color becomes 'white'
   push (@needs2,'attention'); # @needs adds attention
}

Something like that. Googling overloaded functions would turn up something, and iirc you don't have to actually dereference the variables yourself.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
This is what I was referring to.

I think it's a good topic because I found it hard to find good documentation on the matter and this thread my help someone in the future too!

------
thanks for all of your inputs! I plan on working with Perl (among other langs) for the duration. I've always wanted a reason to learn it and now I am involved in a business that uses it all of the time. I look forward to knowing it well enough to answer newbies questions myself.
 
Thats a prototype. Seems like a terrible way to overload variables. Could get real confusing.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
hey Kirsle, fyi, just stepped through your pet color needs code, it doesn't have the result you'd hoped. Overloaded functions are functions that have the same name but accept different parameters. Not trying to argue, I appreciate efforts to communicate and share knowledge. I'd love to be proven wrong and understand why.


Fyi MillerH's solution ChangeVals, nailed the concept of passing parameters into a function and using named variables, make changes to those variables in the calling scope. It and works exactly like I had hoped.


Called like this:
ChangeVals(\$myMainVal_1,\$myMainVal_2,\$myMainVal_3);

sub ChangeVals {
my ($ref1, $ref2, $ref3) = @_;

$$ref1 = "New Value 1";
$$ref2 = "New Value 2";
$$ref3 = "New Value 3";
}

-------------------
KevinADC "Thats a prototype."

I don't know what you're trying to tell me, but again, I appreciate your efforts and your warning about possibly writing confusing code, I'll use comments to make it clear :)

 
EDHills,

We posted at the same time, I was commenting to Kirsle that what he posted is a prototype, a function with parenthesis after the function name. I'm not sure what usefull purpose they serve, if any.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I see my example of using a reference above has an error in it, this line:

Code:
   my $array = shift;

should have been:

Code:
   my $array_ref = shift;




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top