will_ has asked for the wisdom of the Perl Monks concerning the following question:

I've seen this code in the wild:
ok +Some::Package::Foo->a_method_call($var), 'test succeeded';

I'm told that the plus gives the perl interpreter a hint about the sub called 'ok' that it's not a method in Some::Package::Foo.

Why is that desirable?

Is this syntax documented anywhere?

Replies are listed 'Best First'.
Re: What does a plus mean before a package name?
by ikegami (Patriarch) on May 26, 2011 at 09:32 UTC
    Perl allows one to omit the parens of sub call, but doing so causes problems. In particular, problems surface where barewords are involved because barewords are treated specially by many parsing rules. "+" has no effect at run-time, but it can influence the parser by using it in a spot where a bareword would be otherwise.
Re: What does a plus mean before a package name?
by metaperl (Curate) on May 26, 2011 at 09:11 UTC
Re: What does a plus mean before a package name?
by jethro (Monsignor) on May 26, 2011 at 08:43 UTC

    I would guess that '+' means the return value of a_method_call will be interpreted as a number. Secondary effect should be that it prevents indirect object invocation of ok, but the writer could as well just have used parenthesis to get this effect without confusing everyone else.

    At least thats my take, I might be wrong

      Unary-"+" does not numify (or stringify). It's a no-op.

      >perl -E"$_ = 'abc'; say +$_;" abc >perl -E"$_ = [qw( a b c )]; say @{ +$_ };" abc

      It doesn't even influence context. Not even void!

      >perl -E"sub f { say( ( (wantarray && 'list' ) // 'void' ) || 'scalar' + ); } +f(); $_=+f(); ()=+f();" void scalar list

      What you said is the secondary effect is the only effect.

      I agree. Given that it isn't some weirder operator like the solitary underscore, it had just better be a unary plus operator and yes, I agree also that brackets are a better idea - sometimes I even think the aura of inconsistency (there is of course a technical distinction) of whether brackets are required by a function or command is a bad thing.

      One world, one people

Re: What does a plus mean before a package name?
by Anonymous Monk on May 26, 2011 at 08:35 UTC
    with
    $ perl -MO=Deparse,-p -e " ok +Some::Package::Foo->a_method_call($var +), 'test succeeded'; " (('ok' + 'Some::Package::Foo'->a_method_call($var)), '???'); -e syntax OK $ perl -MO=Deparse,-p -MTest::More -e " ok +Some::Package::Foo->a_met +hod_call($var), 'test succeeded'; " use Test::More; ok('Some::Package::Foo'->a_method_call($var), 'test succeeded'); -e syntax OK
    without
    $ perl -MO=Deparse,-p -e " ok Some::Package::Foo->a_method_call($var) +, 'test succeeded'; " ('Some::Package::Foo'->ok->a_method_call($var), '???'); -e syntax OK $ perl -MO=Deparse,-p -MTest::More -e " ok Some::Package::Foo->a_meth +od_call($var), 'test succeeded'; " use Test::More; ok('Some::Package::Foo'->a_method_call($var), 'test succeeded'); -e syntax OK

      Anonymous brother, can you please point out this difference more clearly?   I am old, and mine eyes are dim.

        Ideas
        Increase font size?
        Increase contrast?
        Disable code wrapping (temporarily)?
        Switch themes (temporarily)?
        Run through perltidy/ppi2html/gvim...pastebin...?
        Try this, with
        # -e " ok +Some::Package::Foo->a_method_call($var), 'test succeeded'; + " (('ok' + 'Some::Package::Foo'->a_method_call($var)), '???'); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ( ( 'ok' + 'Some::Package::Foo' -> a_method_call ( $var ) ) , '???' ) ; ################################################################## # -MTest::More -e " ok +Some::Package::Foo->a_method_call($var), 'test + succeeded'; " use Test::More; ok('Some::Package::Foo'->a_method_call($var), 'test succeeded'); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # use Test::More; ok ( 'Some::Package::Foo' -> a_method_call ( $var ) , 'test succeeded' ) ; ##################################################################
        without
        # -e " ok Some::Package::Foo->a_method_call($var), 'test succeeded'; " ('Some::Package::Foo'->ok->a_method_call($var), '???'); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ( 'Some::Package::Foo' -> ok -> a_method_call ( $var ) , '???' ) ; ################################################################## # -MTest::More -e " ok Some::Package::Foo->a_method_call($var), 'test +succeeded'; " use Test::More; ok('Some::Package::Foo'->a_method_call($var), 'test succeeded'); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # use Test::More; ok ( 'Some::Package::Foo' -> a_method_call ( $var ) , 'test succeeded' ) ;