Thread Variable als subname: Subroutine funzt ned (7 answers)
Opened by dyspro at 2007-04-10 00:26

bloonix
 2007-04-10 01:21
#75773 #75773
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hallo dyspro,

hier das - in ICQ - versprochene Codestück... was du brauchst ist wohl
ein Hash, wenn du einen String über STDIN eingibst und diesen String
als "Variable" ansprechen möchtest.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use strict;
use warnings;

my %bausteine = (
  begruessung => sub { print "Hallo du da\n\n" },
  abschied    => sub { print "Mit freundlichen Gruessen\n" },
  mein_name   => sub { print "dyspro\n" },
);

sub main {
  print STDOUT "Eingabe: ";
  my $eingabe = <STDIN>;
  chomp($eingabe);
  $bausteine{$eingabe}()
     if defined $bausteine{$eingabe};
}

&main;


#> skript.pl
Eingabe: begruessung

Hallo du da



Gruss,
opi

Edit: Codefehler beseitigt\n\n

<!--EDIT|opi|1176154003-->
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread Variable als subname: Subroutine funzt ned