in reply to Re: perlcritic and MCE::Loop
in thread perlcritic and MCE::Loop

For info doing this

%result = mce_loop sub {

Results in a syntax error

However wrapping the the whole thing in a sub removes the error, will have to see if it breaks the code.ie

%result = mce_loop {sub { my ($mce,$chunk_ref,$chunk_id) = @_; my %ret; for my $item (@{ $chunk_ref }) { my $ret=tags_get($item); $ret{$item->[1]}=$ret->{$item->[1]}; #say $ret{$_->[1]}; } $mce->gather(%ret); } } $fileprobe;

thanks

Replies are listed 'Best First'.
Re^3: perlcritic and MCE::Loop
by LanX (Saint) on Feb 19, 2021 at 21:40 UTC
    did you separate the arguments behind the block with a comma after introducing "sub"?

    DB<25> sub run (&@) { my $code =shift; $code->(@_) } DB<26> *mce_loop = \&run DB<27> mce_loop {print "code @_"} 1,2,3 # no comma needed code 1 2 3 DB<28> mce_loop sub {print "code @_"}, 1,2,3 # comma needed code 1 2 3 DB<29> mce_loop sub {print "code @_"} 1,2,3 # syntax error Number found where operator expected at (eval 38) ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Thanks, I missed that, working now

      My previous "solution" didn't work, code ran but mce_loop did nothing.

      All seems to be working now and perlcritic happy

        > My previous "solution" didn't work, code ran but mce_loop did nothing.

        That's obvious.

        Defining an anonymous sub won't execute it but return the code-ref.

        DB<39> p sub { print "run @_" } # returns ref CODE(0x2e80a90) DB<40> sub{ print "run @_" }->(1..3) # calls ref run 1 2 3 DB<41>

        Though I'm a bit surprised, my muscle memory wanted brackets around the ref for precedence:

        ( sub{ print "run @_" } )->(@_)

        this could still be needed in other contexts. (or - more likely - it's just cargo cult from JS ;)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        > and perlcritic happy

        Still strange, there is a bug somewhere in this policy.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Re^3: perlcritic and MCE::Loop
by LanX (Saint) on Feb 19, 2021 at 19:27 UTC
    > '%result = mce_loop sub {'

    > Results in a syntax error

    this shouldn't be, the problem must be somewhere in your code.

    I looked into the code of MCE::Loop and it works like I guessed.

    That's what's essentially happening in the import

    DB<8> sub run (&@) {} DB<9> *mce_loop = \&run DB<10> mce_loop {print "code"} DB<11> mce_loop sub {print "code"} DB<12> mce_loop sub {print "code"},@_ DB<13> mce_loop 1 Type of arg 1 to main::run must be block or sub {} (not constant item) + at

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery