Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: "importing" methods?

by pwagyi (Monk)
on Apr 28, 2019 at 12:41 UTC ( [id://1233075] : note . print w/replies, xml ) Need Help??


in reply to "importing" methods?

It looks like you've a singleton 'app' and it's been passed around to invoke methods. Normally this sounds like where DI is used? even thought DI would still require you to write $obj->method. Below idea could work. but I'm not really expert in the magical part of Perl. :)

sub _get_instance { ... init singleton if not created yet. return $app; } sub log { # well there should be some easy way to somehow create function that f +orwards to singleton my $app = &_get_instance; $app->log(@_); }

Replies are listed 'Best First'.
Re^2: "importing" methods?
by LanX (Saint) on Apr 28, 2019 at 13:09 UTC
    (I suppose DI means Dependency injection)

    No it's not a singleton, there are (<10%) cases where I need more than one object.

    Otherwise I'd use a normal module.

    That's why I need a mechanism for the remaining 90% to use a normal Class like a singleton module.

    My interpretation:

    From a Java OOP point of view Perl's module/package architecture could be understood as Singleton classes and the import routine as the constructor and package vars as class/instance variables (singleton means class=instance)

    Lexical scoping of a modules effects are crucial to avoid global conflicts.

    These things are hard to discuss because of little incompatibilities between languages.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice