QUOTE(Sun751 @ 19 Jun, 2009 - 11:47 AM)

In above code I am trying to get the value of variable "$var" which is initialize in sub ini, but its not working
could any one tell me how to make it work, problem is i don't want to have global variable and want seperate
subroutine to initialize;
You've almost got it, actually. You just need to pass $var into ini() by reference instead of by value:
CODE
my $var;
ini(\$var);
prn($var);
sub ini
{
my $var1 = shift;
$$var1 = 1; # Note $$ instead of $
}
sub prn
{
my $var2 = shift;
if(defined $var2)
{
print "Variable is defined";
}
}
The reason for the $$ instead of $ on the marked line is because $var1 is now a reference to $var and you want to change the value of the referenced variable ($var), not the value of the reference itself ($var1). If you're familiar with C, you can think of references as being more-or-less like pointers.
Also note that I changed "if($var2)" to "if(defined $var2)" in prn() so that you'll still get the "Variable is defined" output when $var2 is 0 or an empty string (both of which are defined, but false).
A simpler way to do this, if you're not comfortable with references, would be to just return the value from ini and assign it to $var from the calling scope:
CODE
my $var;
$var = ini(); # Need parens because ini() isn't defined yet
prn($var);
sub ini
{
return 1;
}
sub prn
{
my $var2 = shift;
if(defined $var2)
{
print "Variable is defined";
}
}