CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
function returning incorrect values

 



jcpratap
New User

Aug 29, 2016, 11:52 PM

Post #1 of 2 (1831 views)
function returning incorrect values Can't Post

HI ,
I am running a program on hpux in perl.
I am encountering a strange issue where when i print a variable in the sub which is returning it , it prints a different value but when i call it and store value in a variable it gives a different o/p.

the sub is

Code
sub CheckConfigFilePattern 
{
local($FILE) = @_;
$intLineCount = 1;
$intSendOpcmon = 0;
$intCorrectFormat = 0;

open(CONFFILEHDR, "< $FILE") or die "$!";
foreach $strLine (<CONFFILEHDR>)
{
$strLine = trim($strLine);
if( length($strLine) != 0)
{
print "##############################\n";
print "$strLine \n";
print "step 0 \n";
if( $strLine !~ m/(^#)|(^\s*(\/|\d+)\s+(\/|\d+)\s+(\/|\d+)\s+(\/|\d+)$)/ )
{
OpcMsg("OS","minor","ConfigFileError","Error in Config File $FILE : Line No. $intLineCount, Text - $strLine","OpC");
$intSendOpcmon = 1;
print "step 1 \n";
}
if ( $strLine =~ m/(^\s*(\/|\d+)\s+(\/|\d+)\s+(\/|\d+)\s+(\/|\d+)$)/ )
{
$intCorrectFormat = $intCorrectFormat + 1;
print "step 2 \n";
}
}
$intLineCount = $intLineCount + 1;
}
close CONFFILEHDR;
if ( $intCorrectFormat != 1 )
{
OpcMsg("OS","minor","ConfigFileError","Error in Config File $FILE","OpC");
$intSendOpcmon = 1;
print "step 3 \n";
}
print "1st intSendOpcmon: $intSendOpcmon \n";

return $intSendOpcmon;
}

I am printing intSendOpcmon before retruning it inside sub.

Now I am calling it ,

$intSendOpcmon = CheckConfigFilePattern($CONFFILE);
if ( $intSendOpcmon = 1)
{
print "2nd intSendOpcmon:$intSendOpcmon \n";
exit 0;
}


But I am getting different values of retruned variable

#"./cHmem_util.pl" 1
1st intSendOpcmon: 0
2nd intSendOpcmon:1

Please check why function is returning different values inside and outside


(This post was edited by FishMonger on Aug 30, 2016, 6:23 AM)


Laurent_R
Veteran / Moderator

Aug 30, 2016, 3:52 AM

Post #2 of 2 (1825 views)
Re: [jcpratap] function returning incorrect values [In reply to] Can't Post

Hi,

there are several problems in your code, but my best guess is that your trouble comes from this line:


Code
if ( $intSendOpcmon = 1)


which doesn't check if $intSendOpcmon is equal to 1, as you probably believe, but makes an assignment, i.e. sets $intSendOpcmon to 1.

You should probably have:


Code
if ( $intSendOpcmon == 1)


You should really have the following pragmas:


Code
use strict; 
use warnings;


at the top of your script, and correct any errors or warnings it will diagnoses. This would have detected the above coding error (among others).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives