[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 735
  • Last Modified:

Whu do I get this error in PerL? Can't use string as a HASH ref while "strict refs"

I have a Perl code and it runs some checks on files and generates an html page based on the results.

I wrote the code in a bad way that the html file creation in part of the actual code. Now, I'd like to refactor my code and seperate the html page creation from the actual part where I run the checks.

In order to do that I need to collect all the output that I get from the  actual part (running checks) and then I plan to write a sub function to writes this data to a file (html file).

To do that, I tried to use hashes but I get the following error:

Can't use string ("  WARNING: The file type does no") as a HASH ref while "strict refs" in use

Open in new window


This is part of my code:

my %report = ();

foreach my $fileTobeChecked (@allFilesTobeChecked) {
$fileNo++;
$report{ $fileTobeChecked }{ 'fileNo' } = $fileNo;

SOME CODE IN HERE TO RUN CHECKS

foreach my $check (@Checks) {
$checkno++;
$report{ $fileTobeChecked }{ $checkno } = $check;

SOME CODE IN HERE

if ( $numerrors == 0 ) {
$CheckResult = 'PASSED';
$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;
}
else {
$CheckResult = 'FAILED';	
$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;
}

SOME CODE IN HERE
if{
SOME CODE IN HERE
}
else{
($CheckResult, @arrayofmatches) = eval "$checkFunc('$fileTobeChecked')";
$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;
$report{ $fileTobeChecked }{ $checkno }{ 'arrayofmatches'} = @arrayofmatches;
}

Open in new window



I get the error for line 30 in this code snippet?

Can you please let me know what I am doing wrong?
0
Tolgar
Asked:
Tolgar
  • 2
1 Solution
 
FishMongerCommented:
Start by printing out each of the vars in that block to verify that they hold what you expect.  If one of them doesn't hold what is expected, then work back up the chain of code to find out where the invalid data came from.
0
 
TolgarAuthor Commented:
@Fishmonger: I did what you recommended but I don't see anything wrong.

This the information I got from the debugger:


The failed checks are:
lib::runCheck::runCheck(/some/dir/../lib/runCheck.pm:298):
298:					$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;
  DB<3> x %report
0  '/dir1/dir2/dir3/input.c'
1  HASH(0x202d440)
   1 => '  WARNING: The file type does not exist in the Coding Standards Database'
2  '/dir1/dir2/dir3input.c'
3  HASH(0x2072220)
   'fileNo' => 1
  DB<4> x $CheckResult
0  'n/a'
  DB<5> n
Can't use string ("  WARNING: The file type does no") as a HASH ref while "strict refs" in use

Open in new window

0
 
TolgarAuthor Commented:
@FishMonger: I found the cause of the problem but I cannot think of a way to fix it.

Can you please take a look at the link for the follow up question?


http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_27837106.html


Thanks,
0
 
whosbetterthanmeCommented:
I agree with FishMonger. One of these isn't being assigned a value that you think - somewhere:

$fileTobeChecked
$checkno
$CheckResult

Try printing to see what you get:

else{
($CheckResult, @arrayofmatches) = eval "$checkFunc('$fileTobeChecked')";
print "$fileTobeChecked: $checkno: $CheckResult\n";
#$report{ $fileTobeChecked }{ $checkno }{ 'checkResult'} = $CheckResult;
#$report{ $fileTobeChecked }{ $checkno }{ 'arrayofmatches'} = @arrayofmatches;
}

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now