O'Reilly logo

Programmieren mit Perl by Jon Orwant, Tom Christiansen, Larry Wall

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

use fields

Im Pet-Modul:

package Pet;
use strict;
use fields qw(name weight _Pet_pid);
my $PID = 0;
sub new {
    my Pet $self = shift;
    unless (ref $self) {
        $self = fields::new($self);
        $self->{_Pet_pid} = "this is Pet's secret ID";
    }
    $self->{name} = "Hey,you!";
    $self->{weight} = 20;
    return $self;
}
1;

Im separaten Programm demopet:

use Pet;
my Pet $rock = new Pet;            # lex. Geltungsbereich

$rock->{nanie}    = "quartz";
$rock->{weight}   = "2kg";
$rock->{_Pet_pid} = 1233;       # privates Attribut

$rock->{color}   = "blue";      # erzeugt Fehler während der Kompilierung

Im Dog-Modul:

package Dog; use strict; use base 'Pet'; # Felder und Methoden von Pet erben use fields qw(name pedigree); # Pet-Namensattribut überschreiben # neues pedigree-Attribut aufnehmen use fields qw(wag ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required