1

I’m a beginner in Perl but have to develop some script to parse TCP-dump. Here it is (I truncated it and leave the relevant code only):

I get the following error: Thread 1 terminated abnormally: Invalid value for shared scalar at TestThread1.pl line 16, This is because of this string: %input = (%input, $fix_ClOrdID => [$FrameTime_epoch,$fix_MsgType]);

From many similar topics I’ve read I understand that I cannot share a nested references using threads::shared. And I also found some workaround how to do it. But due to lack of experience I can’t apply examples for my case.

Could you please advise how to fix my script to make it work!

use threads;
use threads::shared;
my %input:shared = ();
threads->new(\&FixParser)->detach();

sub FixParser{  
 open(InFile, 'myFix.dmp') || die;
 while(InFile>){
  ($FrameTime_epoch,$fix_MsgType,$fix_ClOrdID) = split(';',);
  if(exists($input{$fix_ClOrdID})){
   $ExecDelay=$FrameTime_epoch-$input{$fix_ClOrdID}[0];
   print "MsgType: $input{$fix_ClOrdID}[1] ExecDelay: $ExecDelay us\n";
   delete($input{$fix_ClOrdID});
  }
  else{
   %input = (%input, $fix_ClOrdID => [$FrameTime_epoch,$fix_MsgType]);
  }
 }
} 
1
  • Did you copy/paste the actual code from your application? I can't help but notice you didn't use strict; use warnings;, and the code as listed wouldn't compile. For example, I'm just eyeballing it but this line while(InFile>) should say while(<InFile>). Try adding use strict; use warnings; and fix those errors. If there's still a problem you should ask again. Commented Jan 27, 2014 at 8:46

1 Answer 1

1

Lock your shared variable and clone structure before adding to hash,

sub FixParser {  
  open(my $InFile, "<", 'myFix.dmp') || die $!;
  while(<$InFile>) {

    ADVISORY_LOCK: {
      lock(%input);
      # ..
      if(exists($input{$fix_ClOrdID})) {
        # ..
      }
      else{
       # %input = (%input, $fix_ClOrdID => [$FrameTime_epoch,$fix_MsgType]);
       $input{ $fix_ClOrdID } = threads::shared::shared_clone( [$FrameTime_epoch,$fix_MsgType] );
      }
    }

  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!! It seems working! But this $input{ $fix_ClOrdID } = threads::shared::shared_clone( [$FrameTime_epoch,$fix_MsgType] ); just replaces the last entry of hash but I need to add entries into %input..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.