Closures II

 [Vorheriges Kapitel]  [Vorherige Seite]  [Inhaltsverzeichnis]  [Nächste Seite]  [Nächstes Kapitel]

counters2.pl
#!/usr/local/bin/perl

use strict;
use warnings;

my %counter = ();
while (<>) {
   chomp;
   next unless my ($sign, $name) = /^([+-])(\S+)$/;
   $counter{$name} = new_counter()
      unless defined $counter{$name};
   print &{$counter{$name}{$sign}}(), "\n";
}

sub new_counter {
   my $counter = 0;
   return {
      '+' => sub { return ++ $counter },
      '-' => sub { return -- $counter },
   };
}

*Mehrere Inkarnationen anonymer Prozeduren können sich eine Umgebung teilen, wenn sie alle der gleichen Umgebung entstammen.
 

 [Vorheriges Kapitel]  [Vorherige Seite]  [Inhaltsverzeichnis]  [Nächste Seite]  [Nächstes Kapitel]
Copyright © 1996 - 2003 Andreas Borchert, in HTML konvertiert am 01.10.2003