Module

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

Passwd.pm
package Passwd;

use strict;
use warnings;
use IO::File;

my $passwd_file = "/etc/passwd";
my $passwd = new IO::File $passwd_file
   or die "Unable to read from $passwd_file: $!\n";
my %passwd_by_uid; my %passwd_by_login;
while(<$passwd>) {
   chomp;
   my ($login, $passwd, $uid, $gid,
      $name, $home, $shell) = split /:/;
   $passwd_by_uid{$uid} =
   $passwd_by_login{$login} = {
      login => $login, passwd => $passwd,
      uid => $uid, gid => $gid,
      name => $name, home => $home, shell => $shell
   };
}
$passwd->close;

sub getpwent_by_uid { return $passwd_by_uid{$_[0]}; }
sub getpwent_by_login { return $passwd_by_login{$_[0]}; }
1; # module was loaded successfully

*Jedes Modul hat seinen eigenen Namensraum und entsprechend gibt es keine globale Variablen -- abgesehen von verschiedenen Standard-Variablen von Perl ($_ zum Beispiel).
 
*Die Dateiendung für Perl-Module ist zwingend .pm.
 

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