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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable references in subroutines, what' s wrong ?

Status
Not open for further replies.

wimvanherp

Programmer
Mar 3, 2001
149
BE
I have a problem with variable references in subroutines:

if you see my simple program below i think it should print : abcdefgh, but it returns : SCALAR(0x15dd8d4)edfg.

what's wrong with my subroutine ?




use strict;

sub sub1
{ my($VAR)=@_;
my $TEMP=$VAR;
my $TEMP=$VAR."efgh";
print $TEMP;
$$VAR=$TEMP;

}


my $T="abcd";
&sub1(\$T);
print $T;

Wim Vanherp
Wim.Vanherp@belgacom.net
 
You concatenated the "efgh" too early. Rather than changin the value, you changed the reference. Then, when you try to derefence it it the last line of the sub, you get the wrong value. Try this instead:

my($VAR)=@_;
# dereference the ref you were passed
my $TEMP=$$VAR;
my $TEMP=$TEMP."efgh";
print $TEMP;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top