Leser: 3
|< 1 2 >| | 19 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
sub foo {
my $self = shift;
return $self->foobar('foo', @_);
}
sub bar {
my $self = shift;
return $self->foobar('bar', @_);
}
sub foobar {
return 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/perl
package Foo;
BEGIN{
for('foo','bar'){
*$_ = sub{foobar(@_)};
}
}
sub foobar{
print $_,"\n" for @_;
}
package main;
Foo::foo('hallo');
Foo::bar('Opi');
1
2
3
4
5
6
7
8
9
10
11
12
my %hash =
(
"foo" => \&foobar,
"bar" => \&foobar,
);
sub foobar{
print $_,"\n" for @_;
}
$hash{"foo"}->("hallo");
$hash{"bar"}->("opi");
1
2
3
4
for my $sub (qw(foo bar)) {
no strict 'refs';
*$sub = sub { my $self = shift; $self->foobar($sub, @_); };
}
1
2
3
4
$foo->foobar('FOO', $message);
$foo->foobar('BAR', $message);
$foo->foobar('OOF', $message);
$foo->foobar('RAB', $message);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sub foo {
my $self = shift;
return $self->foobar('FOO', @_);
}
sub bar {
my $self = shift;
return $self->foobar('BAR', @_);
}
sub oof {
my $self = shift;
return $self->foobar('OOF', @_);
}
sub rab {
my $self = shift;
return $self->foobar('RAB', @_);
}
1
2
3
4
5
6
7
BEGIN {
no strict 'refs';
for my $prefix (qw(FOO BAR OOF RAB)) {
my $sub = lc($prefix);
*{$sub} = sub { my $self = shift; return $self->foobar($prefix, @_) }
}
}
QuoteStatt dessen möchte ich gerne die Prefixe als Methoden anbieten:
1
2
3
4
5
6
7
BEGIN {
no strict 'refs';
for my $sub (qw(debug info notice warning error critical alert emergency)) {
my $level = uc($sub);
*{"$sub"} = sub { my $self = shift; return $self->_print($level, @_); };
}
}
|< 1 2 >| | 19 Einträge, 2 Seiten |