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

What the freak is the plus-sign doing in the expression +Mock::Basic in the test suite to DBIx::Skinny?
test 'update mock_basic data' => run { ok +Mock::Basic->update ('mock_basic',{name => 'python'},{id => 1}); my $row = Mock::Basic->single ('mock_basic',{id => 1}); isa_ok $row, 'DBIx::Skinny::Row'; is $row->name, 'python'; };
This code is in a Test::Declare block.

Replies are listed 'Best First'.
Re: freaky deaky plus sign usage
by AnomalousMonk (Archbishop) on Jul 31, 2009 at 15:58 UTC
    This is to get around Perl's 'indirect object syntax'.

    Otherwise
       ok Mock::Basic->update(...);
    looks like
       Mock::Basic->ok->update(...);
    which is probably not what is intended.

    ok(Mock::Basic->update(...)); works as well.

      ok, and how does + "get around" it? What exactly is syntactically happening? Is "ok" being added to "Mock::Basic->update(...)"
        perl -MCGI -MTest::More -e " ok 1; ok +CGI->new, +CGI->new; ok scalar +CGI->new; ok( CGI->new ); ok CGI->new; " ok 1 ok 2 - CGI=HASH(0x1a08a30) ok 3 ok 4 Undefined subroutine CGI::ok at -e line 1 # Tests were run but no plan was declared and done_testing() was not s +een.
Re: freaky deaky plus sign usage
by pemungkah (Priest) on Jul 31, 2009 at 20:39 UTC
    It tells the parser "Hi. Since I have a leading (unary) plus sign, I'm now a scalar expression, so I qualify to match the first item in ok()'s prototype; please do not get confused and interpret me as a class name you should call an ok() method on."

    This was done to avoid putting the parens around ok()'s argument, like this:

    ok( Mock::Basic->update( 'mock_basic', { name => 'python' }, {id => 1} ), 'I should actually put a description on this test' );
    To me this is a lot clearer; it's less mental effort to just add the parens than it is to dork around trying to remember the parsing rules. Once the parens are there, we've unambiguously declared "these are arguments to a subtroutine called ok(). Please to not attempt to interpret it otherwise, thank you." I'll igonore the fact that there's no description on the test - I think that's more objectionable than the syntax trickery, frankly.

    Perhaps you should file a bug on that - feel free to snag this rewrite as a start on a patch.

Re: freaky deaky plus sign usage
by Anonymous Monk on Aug 02, 2009 at 15:43 UTC

    Apparently, there's ambiguity in the parser that does not get resolved as desired.

    a b->c() desired: a( b->c() ) actual: ( b->a() )->c()

    unary-plus is a no-op, but it changes what the compiler sees from a bareword to an operator. That removes the ambiguity.

    Using parens around the parameters (ok( ... )) would also remove the ambiguity.

    - ikegami