www.farid-hajji.net banner

Farid Hajji

Perl: Einführung, Anwendungen, Referenz (2/e) [Support-Site]

Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2

Beispielprogramm

autoload2.pl
#!/usr/local/bin/perl -w
# autoload2.pl -- Selektive Simulation von Funktionen mit AUTOLOAD()

%ADISP = ( hello => sub { "hello world! (@_)\n" },
       bye   => sub { "that's all, folks! (@_)\n" },
       what  => sub { "what's that? (@_)\n" } );

sub AUTOLOAD {
    my (@theargs) = @_;         # Zu uebergebende Argumente
    $AUTOLOAD =~ s/.*:://;      # Entferne den Packagenamen

    if (exists $ADISP{$AUTOLOAD}) {
        # Bekannte Funktion indirekt aufrufen
    return &{$ADISP{$AUTOLOAD}}(@theargs);
    } elsif ($AUTOLOAD eq 'magic') {
        # Noch eine spezielle Behandlung
    return "This is magic! (@_)\n";
    } else {
        # Nicht behandelte Faelle als Exceptions weiterreichen
    die "Undefined subroutine &$AUTOLOAD(@_) called!\n";
    }
}

# Versuchen wir es:
print hello("wow!", "that's", "cool!");   # Indirekter Aufruf
print what("wizard", "of", "oz");         # Indirekter Aufruf
print magic("xyzzy");                     # Direkt von AUTOLOAD()
print bye("used time", times());          # Indirekter Aufruf
print nondef("never", "seen");            # Erzeugt Ausnahme
print "Still living!\n";                  # Wird nie erreicht!
   

[Prev] [Up] [Relevant Chapter] [Next]

[Alte Quelle]


Last modified: $Date: 2006/05/18 12:55:51 $
FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy
FreeBSD Logo