Nice benefit of "aliased"

Ovid on 2005-10-31T01:53:05

If you haven't checked out my aliased module, you really should. One of the pleasant side effects of this module is how it avoids accidental use of indirect method syntax. I had the following:

use Games::World;
can_ok 'Games::World', 'dump_vocabulary';
is Games::World->dump_vocabulary, $vocabulary,
    '... and it should return the correct vocabulary';

That fails with the following error message:

Can't locate object method "is" via package "Games::World" at ...

That's really annoying. It's particularly annoying because it's trying to use indirect method syntax even though I'm clearly trying to call the dump_vocabulary method. To make that work, I have to use parentheses with is():

use Games::World;
can_ok 'Games::World', 'dump_vocabulary';
is(Games::World->dump_vocabulary, $vocabulary,
    '... and it should return the correct vocabulary');

However, aliased gives me two benefits. The first, of course, is shorter class names. The second is that it does not trigger spurious indirect method invocation (an annoying source of subtle bugs).

use aliased 'Games::World';
can_ok World, 'dump_vocabulary';
is World->dump_vocabulary, $vocabulary,
    '... and it should return the correct vocabulary';


Re:

Aristotle on 2005-10-31T13:10:27

Do note that you can disambiguate that without parens, if you are so inclined.

can_ok 'Games::World', 'dump_vocabulary';
is +Games::World->dump_vocabulary, $vocabulary,
    '... and it should return the correct vocabulary';