in reply to When to Use Object Oriented approach in Perl? (RFC)

On a related note, sometimes I find using OO only to have separate name space and not have to import k number of subs. That surely beats having to write the module name every time when calling a function. I do cringe having to provide a constructor (when all I need is a short syntax to call a sub) due to, however minor, work involved.

Is there any other reasonable alternative to the above?

  • Comment on Re: When to Use Object Oriented approach in Perl? (RFC)

Replies are listed 'Best First'.
Re^2: When to Use Object Oriented approach in Perl? (RFC)
by tobyink (Canon) on Aug 01, 2014 at 16:57 UTC

    If these are utilityish methods that don't actually need a real blessed object, then:

    package Foo::Utils { sub frobnicate { shift; # ignore invocant print "frobnicating!\n"; } } ...; # some time later my $utils = "Foo::Utils"; # no need for a constructor ...; # some time later $utils->frobincate; # call as a class method

    That said, writing a constructor isn't hard:

    package Foo::Utils { sub new { bless [], shift } sub frobnicate { shift; # ignore invocant print "frobnicating!\n"; } }

    Or even easier:

    package Foo::Utils { use Moo; sub frobnicate { shift; # ignore invocant print "frobnicating!\n"; } }