Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

@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

Avatar of Tolgar

ASKER

@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?


https://www.experts-exchange.com/questions/27837106/Hoe-to-create-multidimensional-hash-in-Perl.html


Thanks,
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