unable to return multilple values in perl

hello friends,

i have written one perl script.Which opens a file and search for some parameter's value and gets the status of these parameters. but while i am trying to return these value always i am getting false. Can any one please help me..
here is that function:

sub getParameterStatus {
open(FILE,"<$File");
if (grep{/parameter1=false/} <FILE>){
print " parameter1 is false Do you want to make it true \n";
$var1="true";
}
else 
{
print "parameter is already true \n";
}

if (grep{/parameter2=false/} <FILE>){
print " parameter2 is false Do you want to make it true \n";
$var2="true";
}

else 
{
print "parameter is already true \n";
}

if (grep{/parameter3=false/} <FILE>){
print " parameter3 is false Do you want to make it true \n";
$var3="true";
}
else 
{
print "parameter is already true \n";
}
close FILE;

return ($var1,$var2,$var3);

}
sub getParameterStatus {
    open(FILE,"< file.txt");
    for (<FILE>) {
        if (/parameter1=false/) {
            print "param1 is false. setting variable var1 to true\n";
            $var1 = "true";
        }
        if (/parameter2=false/) {
            print "param2 is false. setting variable var2 to true\n";
            $var2 = "true";
        }
        if (/parameter3=false/) {
            print "param3 is false. setting variable var3 to true\n";
            $var3 = "true";
        }
    }
    close FILE;
    return ($var1,$var2,$var3);
}

Why not just something like :

$ cat my_parameters
param1=toto
param2=titu
param3=tita
param4=titb
param5=titc
param6=titd
$ . ./my_parameters
$ echo ${param3} ${param5}
tita titc

?

Thanks a lot balajesuri...it worked. :)..but can you please tell me whats happening in my case...

When you grep on an open filehandle, you read the entire contents at once. After the first if-block, there isn't anything left in the filehandle to be read and grepped. So the 2nd and 3rd if-blocks always go to the else part.

One way is to store the entire contents of the file in an array @array = <FILE> and then grep from the array like this - grep {/parameter1=false} @array . This is a crude and inefficient way. What if your file is large? You wouldn't want to store the entire file in an array and cram the memory, right?

Another way would be to open the file before each if-block and close it before opening it again before 2nd if-block. But that would be an over-kill, wouldn't it?

What I did was to open it once and read it line by line in a for-loop. This way you have better control over the filehandle.

Thanks again to explain in detail.
Yes i tried this also @array = <FILE> and you are absolutely right,mine file is large having so many configurable parameters.
I also tried the second method open the file before each if-block and close it before opening it again before 2nd if-block.it worked but once again you are right :slight_smile: it would be over kill.
anyway mine script is working fine now..Once again thanks a million.. :slight_smile: