summaryrefslogtreecommitdiffstats
path: root/Bachelor/Systemprogrammierung in Perl/examples/example11.pl
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Systemprogrammierung in Perl/examples/example11.pl
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Systemprogrammierung in Perl/examples/example11.pl')
-rw-r--r--Bachelor/Systemprogrammierung in Perl/examples/example11.pl62
1 files changed, 62 insertions, 0 deletions
diff --git a/Bachelor/Systemprogrammierung in Perl/examples/example11.pl b/Bachelor/Systemprogrammierung in Perl/examples/example11.pl
new file mode 100644
index 0000000..47e984b
--- /dev/null
+++ b/Bachelor/Systemprogrammierung in Perl/examples/example11.pl
@@ -0,0 +1,62 @@
+#!/Perl/bin/perl
+use strict;
+use warnings;
+
+# Nun erfolgt die Initialisierung aus einem File in einem Unterprogramm
+
+
+my ( %words );
+my ( $name, $guess );
+
+&init_words;
+
+print "Wie heissen Sie? ";
+$name = <STDIN>;
+chomp $name;
+
+if ( $name =~ /^gerhard\b/i ) {
+ print "Hallo Gerhard, wie nett, dass du da bist!\n";
+}
+else {
+ print "Hallo $name!\n"; # Standard Gruß
+ print "Wie lautet das Geheimwort? ";
+ $guess = <STDIN>;
+ chomp ( $guess );
+
+ while ( ! &good_word($name, $guess) ) {
+ print "Falsch geraten, nochmal. Wie lautet das Geheimwort? ";
+ $guess = <STDIN>;
+ chomp $guess;
+ }
+}
+exit;
+
+sub init_words {
+ my ($word, $myname);
+ open(WORDSLIST, "wordlist.txt");
+ while( $myname = <WORDSLIST> ) {
+ chomp( $myname );
+ $word = <WORDSLIST>;
+ chomp( $word );
+ $words{ $myname } = $word;
+ }
+ close(WORDSLIST);
+}
+
+sub good_word {
+ my ( $somename, $someguess ) = @_; #Parameter bennenen
+
+ $somename =~ s/\W.*//; # alles hinter dem ersten Wort vergessen
+ $somename =~ tr/A-Z/a-z/; # alles in Kleinbuschstaben
+
+ if( $somename eq "gerhard" ) {
+ return 1; #Rückgabewert ist wahr
+ }
+ elsif ( ($words{$somename} || "groucho") eq $someguess ) {
+ return 1; #Rückgabewert ist wahr
+ }
+ else {
+ return 0; #Rückgabewert ist falsch
+ }
+}
+