Perl inheritance same instance -
I am trying to create a small application with base module 'apps' and see "shared" in all modules "And as" Request "all the data and methods in the" Share "module will be available for all other modules, which are the extension code (example, see, request, app) below the example example code examples.
App.cgi
#! Use / usr / bin / perl mousse; Use the app; My $ app = Apps-> new; message (); # Export, works fine $ app- & gt; msg (); # Works fine as my $ view = $ app- & gt; View; $ View-> msg; # Scene class method, works fine print $ app- & gt; Charset "\ N"; # From Shared.pm, utf8 prints, default $ app- & gt; Charset ("windows-1256"); $ XP in shared PM print in windows-1256 from #Utf8- & gt; Charset "\ N"; #It's okay now, the window prints - 1256 print $ view- & gt; Charset "\ N"; # It's not okay, the same default utf8 exit; Apps.pm
package application; Use mousse; Expansion of QW (shared); Sub init {print "true:". True () "\ n". } 1; see pm
view package; Use mousse; Quad (shared); Sub Message {Print "Hello msg from" "\ n";} 1; Request a PM
Package request; Use mousse; QW Share); Sub Message {Print} Request msg msg <\ n "; } 1; When I run app.cgi, I get this output:
is shared from Hello msg, shared with msg Msg u utf8 windows-1256 utf8 What am I expecting:
is shared by msg msg greetings msg, shared with msg msg utf8 windows-1256 windows-1256 The change in the shared module is not reflected or shared with the view and other modules shared in all extended classes How to create an example of an object.
My goal is to expand all the packages on a shared package and that package should share the same example data between all extensions.
shared should not be a base class, Receive from Shared The class must be completely different from the perspective of heritage. Instead, each class should have a characteristic (it can be a private person, which can be called some ambiguous from the beginning of something undecided), to that point as share When an object creates another object (in your example, $ Aap object creates a $ View object) for a shared object from a class, this is an example of an object for an share class Can specify. Something like this: Usage v5.14; Shared package {use mousse; Is foo = & gt; (Is => 'RW', default => 99); The bar is = & gt; (Is = & gt; 'RW', default => 42); } Package app {Use mood; Is _common = & gt; (Has => & gt; 'ro', default = & gt; sub {share- & gt; new}, handle = & gt; [qw / foo bar /],); Sub view {my $ self = shift; See- & gt; New (@_, _common = & gt; $ self- & gt; normal); }} See package {use mousse; Is _common = & gt; (= Is = & gt; 'ro', required => 1, handles => [qw / foo bar /],); } My $ app = app- & gt; new; My $ view = $ app- & gt; View; '$ App- & gt; foo: ', $ app- & gt; Foo; '$ View- & gt; foo: ', $ view- & gt; Foo; # $ App- & gt; Change the value of foo # it will be shared with $ view $ App- & gt; Foo (66); '$ App- & gt; Foo: ', $ app- & gt; Foo; '$ View- & gt; Foo: ', $ view- & gt; Foo;
Comments
Post a Comment