Home > Net >  Parallel::ForkManager doesn't work with Perl 5.36
Parallel::ForkManager doesn't work with Perl 5.36

Time:09-30

I have a script that runs well with Perl < 5.36:

#!/usr/bin/env perl

use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use Parallel::ForkManager;

sub execute {
    my $command = shift;
    print "Executing Command: $command\n";
    if (system($command) != 0) {
        my $fail_filename = "$0.fail";
        print "$command failed.\n";
        die;
    }
}

sub run_parallel {
    my $cmd = shift;
    my $manager = new Parallel::ForkManager(2);
    foreach my $command (@{ $cmd }) {
        $manager->start and next;
        execute( $command );
        $manager->finish;
    }
    $manager->wait_all_children;#necessary after all lists
}

my @commands = ('echo "a"','echo "b"','echo "c"','which ls','which rm');
run_parallel(\@commands);

but when I make minor changes with the above to 5.36:

#!/usr/bin/env perl

use 5.036;
use warnings FATAL => 'all';
use autodie ':all';
use Parallel::ForkManager;

sub execute {
    my $command = shift;
    print "Executing Command: $command\n";
    if (system($command) != 0) {
        my $fail_filename = "$0.fail";
        print "$command failed.\n";
        die;
    }
}

sub run_parallel {
    my $cmd = shift;
    my $manager = new Parallel::ForkManager(2);
    foreach my $command (@{ $cmd }) {
        $manager->start and next;
        execute( $command );
        $manager->finish;
    }
    $manager->wait_all_children;#necessary after all lists
}

my @commands = ('echo "a"','echo "b"','echo "c"','which ls','which rm');
run_parallel(\@commands);

I get an error:

Bareword found where operator expected at debug.pl line 20, near "new Parallel::ForkManager"

All I switched was use 5.036

Is Parallel::ForkManager incompatible with perl 5.36 or am I doing something wrong?

CodePudding user response:

Perl v5.36 with use v5.36 turns off indirect object notation, where the method comes before the invocant:

my $p = new Some::Module;     # indirect object notation
my $p = Some::Module->new();  # what you should do

If this is inconvenient for you in the short term, you can require the minimum version so you still get the things turned off by use v5.36:

require v5.36;

If you don't actually use v5.36 features, also consider requiring the minimum version that your code actually needs. In your snippet, I don't immediately see any minimum version requirement (other than just Perl 5).

CodePudding user response:

Loading the 5.36 feature bundle (which you do through use 5.036;) disables the indirect method call syntax as if you had done no feature qw( indirect );.

This is a method call using the indirect syntax:

METHODNAME INVOCANT ARGS

Either re-enable the feature or use the "direct" syntax:

INVOCANT->METHODNAME( ARGS )

In your case,

my $manager = Parallel::ForkManager->new( 2 );
  • Related