0

I have issues understanding this Perl module, I am very new to Perl, where main logic is within the ExternalLibrary::registerJob and it is executed as listed below, the mentioned ExecutionBlock is a black box for me and all I do know I can list several unitFunc() within tasksequence.

tasksequence=>[
        $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB1'),
        $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB2'),
        $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB3')
      ]

The module part, this is the template pattern we should use:

sub myFunc{
  my %args=@_;
  my $ic = $args{'initialcontext'};
 
  my $basetasksequence = [
    new ExecutionBlock(
      initialcontext=>$ic,
      tasksequence=>[
        $ic->unitFunc('ExternalLibrary::registerJob')
      ]
    )
  ];
 
  my $runtasksequence = [
    new ExecutionBlock(
      initialcontext=>$ic,
      tasksequence=>$basetasksequence
    )
    ->errCallBack($ic->unitFunc('ExternalLibrary::logger', kind=>"J", status=>"F",RunTaskSequence=>$basetasksequence))
  ];
 
  return { tasksequence=>$runtasksequence };
}

I want to modify that to so I can pass multiple jobNames there, the registerJob can accept jobName as parameter, this is a hint for me.

$ic->unitFunc('ExternalLibrary::registerJob', jobName=>$jobName)

I however do not know how to loop this properly or even how to name the problem so I could lookup the problem and read on the topic. Trying to understand the whereabouts are a bit harder to me.

I can get the jobName from a loop similar to this:

my @res= $ic->DBHandler::sql(sql=>$statementQuery);
foreach my $list(@res){
  $jobName = $list->{'JOB_NAME'};
}

But I am not sure how to go about combining this together, whether I can run the loop within the array like this?

sub myFunc{
  my %args=@_;
  my $ic = $args{'initialcontext'};

  my $basetasksequence = [
    new ExecutionBlock(
      initialcontext=>$ic,
      tasksequence=>[
      my @res= $ic->DBHandler::sql(sql=> $statementQuery);
      foreach my $list(@res){
        $jobName = $list->{'JOB_NAME'};
        $ic->unitFunc('ExternalLibrary::registerJob', jobName=>$jobName)
      }
      ]
    )
  ];

  my $runtasksequence = [
    new ExecutionBlock(
      initialcontext=>$ic,
      tasksequence=>$basetasksequence
    )
    ->errCallBack($ic->unitFunc('ExternalLibrary::logger', kind=>"J", status=>"F",RunTaskSequence=>$basetasksequence))
  ];

  return { tasksequence=>$runtasksequence };
}

I think I would need another function to return the whole array and then assign it to tasksequence, but I do not know how to preserve the required form ($ic->unitFunc()) and pass it correctly.

1
  • The name of the language is Perl, not PERL; it's not an acronym. Commented Feb 19, 2021 at 21:57

1 Answer 1

1

If I understand correctly, the question can be summarized as follows:

I have an array of rows @res. Each row is a reference to a hash with a JOB_NAME element.

I want to do the following such that each row results in a task with jobName given by the value of the row's JOB_NAME element:

new ExecutionBlock(
  initialcontext=>$ic,
  tasksequence=>[
    $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB1'),
    $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB2'),
    $ic->unitFunc('ExternalLibrary::registerJob', jobName=>'JOB3')
  ]
)

[] creates an array, so that's what we're going to do.

my @rows = $ic->DBHandler::sql(sql=>$statementQuery);

my @task_sequence;
for my $row (@rows) {
  push @task_sequence,
    $ic->unitFunc('ExternalLibrary::registerJob', jobName=>$row->{JOB_NAME});
}

new ExecutionBlock(
  initialcontext=>$ic,
  tasksequence=>\@task_sequence
)

I would use the following simpler solution:

new ExecutionBlock(
  initialcontext=>$ic,
  tasksequence=>[
    map { $ic->unitFunc('ExternalLibrary::registerJob', jobName=>$_->{JOB_NAME}) }
       $ic->DBHandler::sql(sql=>$statementQuery)
  ]
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this got me on right direction. Having difficulties naming thing properly.

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.