Jemand zu Hause?Leser: 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
my $prog=Progamm->new(); $prog->runn(); package Programm; sub new { my $class=shift; #... bless(my $self, $class); #... $self->{dienst}=Dienst->new(); #... return $self; } sub mach_was { my $self=shift; my $dienst=$self->{dienst}; #... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
package Dienst; my $dienst; sub new { #... } sub get_Dienst { $dienst=Dienst->new(@_) unless($dienst); return $dienst; } package main; #... sub mach_was { my $dienst=Dienst::get_Dienst(); #... }
IOC oder
Peco::Container. Dabei handelt es sich um eine Bibliothek, die das Zusammenbauen eines komplexen Objektgraphen mit wechselseitigen Abhängigkeiten erleichtern soll und eine Art Zentraldepot für Serviceobjekte bereitstellt.
1 2 3 4 5 6 7 8 9
package Dienst; my $singleton; sub new { # ... setup } sub singleton { $singleton ||= shift->new(@_) }
my $dienst = Dienst->singleton(...)
sub singleton { $singleton ||= shift->new(@_) }
1 2 3 4 5
sub singleton { $singleton = shift->new(@_) unless($singleton); return $singleton; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package Sitescriptor::Core::Request::Httpheader; my $me; sub new { my $pkg = shift; my $Self = {}; bless( $Self, (ref $pkg || $pkg) ); $me = $Self; return $Self; } sub get_obj { $me = Sitescriptor::Core::Request::Httpheader->new(@_) unless($me); return $me; }
2010-08-17T23:46:18 sitescriptorNoch eine Bemerkung zu Euren Beispielen: in sub new... müsste auch einmal das Objekt der Packetvariablen zugewiesen werden.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package Sitescriptor::Core::Request::Httpheader; my $me; sub new { return $me if($me); my $pkg = shift; my $Self = {}; bless( $Self, (ref $pkg || $pkg) ); $me = $Self; return $Self; } sub get_obj { return __PACKAGE__->new(@_) }
1 2
#create global object sub BEGIN { $Konstrukt::Cache = __PACKAGE__->new() unless defined $Konstrukt::Cache; }
$Konstrukt::Cache->funktion()
1 2 3 4
my $Konstrukt::Cache; sub BEGIN { $Konstrukt::Cache = __PACKAGE__->new() unless defined $Konstrukt::Cache; } sub Konstrukt::Cache{ return $Konstrukt::Cache; }
Konstrukt::Cache()->funktion();
2010-08-18T22:15:36 topeg...Es könnte zum Problem werden alle Objekte schon zur Startzeit des Scriptes zu erzeugen,...
$Konstrukt::Cache
1 2 3 4 5 6 7 8
#!/usr/bin/perl use strict; use warnings; use MyTest; print $MyTest::Test;
Guest werDanke, da habe ich wieder etwas gelernt!Man kann Variablen und Funktionen von "außen" in jeden beliebigen Namensraum packen, auch wenn man niemals ein package erzeugt hat.
Wie installiert man ein Modul?