Schrift
[thread]8699[/thread]

Routinen erzeugen (Seite 2)

Leser: 3


<< |< 1 2 >| >> 19 Einträge, 2 Seiten
betterworld
 2007-01-31 18:59
#73851 #73851
User since
2003-08-21
2614 Artikel
ModeratorIn

user image
Wenn man kein "no strict;" mag, kann man es auch so machen:
Code: (dl )
$Package::{$_} = sub {...};


Und ich persoenlich bevorzuge es eigentlich immer, wenn man eine strict-konforme Loesung findet.  Da muss man dann auch keine Kunststuecke machen, um "no strict" fuer einen moeglichst kleinen Teil des Blockes wirken zu lassen.  Beim obigen Beispiel von [s]bloonix[/s] opi hat z. B. der ganze BEGIN-Block kein strict refs.\n\n

<!--EDIT|betterworld|1170336265-->
bloonix
 2007-02-01 10:30
#73852 #73852
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hallo betterworld,

[quote=betterworld,31.01.2007, 17:59]Beim obigen Beispiel von bloonix hat z. B. der ganze BEGIN-Block kein strict refs.[/quote]
Wer ist denn bloonix? :p

Es stimmt aber... ich könnte 'refs' noch ein Stück einschränken... ist auch
schon geschehen. =)

[quote=betterworld,31.01.2007, 17:59]Und ich persoenlich bevorzuge es eigentlich immer, wenn man eine strict-konforme Loesung findet.  Da muss man dann auch keine Kunststuecke machen, um "no strict" fuer einen moeglichst kleinen Teil des Blockes wirken zu lassen.[/quote]
Stimmt. Man sollte immer eine strict-konforme Lösung suchen, aber ich
finde es auch nicht so arg schlimm, wenn man strict mal teilweise aus-
schaltet. Es ist auch nicht unnormal:

5.8/B/Bytecode.pm:          no strict 'refs';
5.8/B/C.pm:    no strict 'refs';
5.8/B/C.pm:      no strict 'refs';
5.8/B/C.pm:                no strict 'refs';
5.8/B/C.pm:   no strict 'refs';
5.8/B/C.pm: no strict qw(vars refs);
5.8/B/C.pm: no strict 'vars';
5.8/B/C.pm: no strict qw(refs);
5.8/B/CC.pm:    no strict 'refs';
5.8/B/Concise.pm:                   no strict 'refs';
5.8/B/Deparse.pm:       no strict 'refs';
5.8/B/Deparse.pm:       no strict 'refs';
5.8/B/Deparse.pm:       no strict 'refs';
5.8/B/Lint.pm:      no strict 'refs';
5.8/B/Lint.pm:    no strict qw(vars refs);
5.8/B/Showlex.pm:               no strict 'refs';
5.8/B/Xref.pm:      no strict 'refs';
5.8/B/Xref.pm:    no strict qw(vars refs);
5.8/Encode/Guess.pm:    no strict 'refs';
5.8/I18N/Langinfo.pm:   no strict 'refs';
5.8/IO/Handle.pm:    no strict 'refs';
5.8/Sys/Syslog.pm:    no strict 'refs';
5.8/Sys/Syslog.pm:    my $value = eval { no strict 'refs'; &$name };
5.8/Sys/Syslog.pm:      no strict 'refs';
5.8/Time/HiRes.pm:      no strict 'refs';
5.8/B.pm:    no strict 'refs';
5.8/Config.pm:    no strict 'refs';
5.8/Cwd.pm:    no strict 'refs';
5.8/DB_File.pm:    no strict 'refs';
5.8/Errno.pm:   no strict 'refs';
5.8/GDBM_File.pm:    no strict 'refs';
5.8/Safe.pm:    no strict 'refs';
5.8/Safe.pm:    no strict 'refs';
5.8/Safe.pm:    no strict 'refs';
5.8/encoding.pm:            no strict 'refs';
5.8.7/B/Bytecode.pm:        no strict 'refs';
5.8.7/B/C.pm:    no strict 'refs';
5.8.7/B/C.pm:    no strict 'refs';
5.8.7/B/C.pm:                no strict 'refs';
5.8.7/B/C.pm:   no strict 'refs';
5.8.7/B/C.pm: no strict qw(vars refs);
5.8.7/B/C.pm: no strict 'vars';
5.8.7/B/C.pm: no strict qw(refs);
5.8.7/B/CC.pm:    no strict 'refs';
5.8.7/B/Concise.pm:                 no strict 'refs';
5.8.7/B/Deparse.pm:     no strict 'refs';
5.8.7/B/Deparse.pm:     no strict 'refs';
5.8.7/B/Deparse.pm:     no strict 'refs';
5.8.7/B/Lint.pm:            no strict 'refs';
5.8.7/B/Lint.pm:    no strict qw(vars refs);
5.8.7/B/Showlex.pm:             no strict 'refs';
5.8.7/B/Xref.pm:            no strict 'refs';
5.8.7/B/Xref.pm:    no strict qw(vars refs);
5.8.7/Encode/Guess.pm:  no strict 'refs';
5.8.7/I18N/Langinfo.pm: no strict 'refs';
5.8.7/IO/Handle.pm:    no strict 'refs';
5.8.7/Sys/Syslog.pm:    no strict 'refs';
5.8.7/Sys/Syslog.pm:    my $value = eval { no strict 'refs'; &$name };
5.8.7/Sys/Syslog.pm:    no strict 'refs';
5.8.7/Time/HiRes.pm:    no strict 'refs';
5.8.7/B.pm:    no strict 'refs';
5.8.7/Config.pm:    no strict 'refs';
5.8.7/Cwd.pm:    no strict 'refs';
5.8.7/DB_File.pm:    no strict 'refs';
5.8.7/Errno.pm: no strict 'refs';
5.8.7/GDBM_File.pm:    no strict 'refs';
5.8.7/Safe.pm:    no strict 'refs';
5.8.7/Safe.pm:    no strict 'refs';
5.8.7/Safe.pm:    no strict 'refs';
5.8.7/encoding.pm:          no strict 'refs';
Compress/Zlib.pm:    no strict 'refs';
Crypt/SSLeay/MainContext.pm:        no strict 'refs';
DBD/File.pm:    no strict qw(refs);
DBD/ExampleP.pm:            no strict 'refs';
DBD/ExampleP.pm:    use strict; no strict 'refs'; # cause problems with filehandles
DBI/PurePerl.pm:    no strict qw(refs);
DBI/DBD/Metadata.pm:        no strict 'refs';
DBI.pm:  no strict;
DBI.pm: no strict 'refs';
DBI.pm:     no strict 'refs';
DBI.pm: no strict 'refs';
DBI.pm: no strict 'refs';
DBI.pm:     no strict 'refs';
DBI.pm:    no strict 'refs';
DBI.pm: no strict 'refs';
Glib.pm:no strict qw(refs);
Irssi.pm:  no strict 'refs';
Irssi.pm:  no strict 'refs';
Irssi.pm:  no strict 'refs';
Mysql/Statement.pm:    no strict;
Mysql.pm:           no strict 'refs';
Net/SSLeay/Handle.pm:    no strict 'refs';
silc-client/Irssi.pm:  no strict 'refs';
silc-client/Irssi.pm:  no strict 'refs';
silc-client/Irssi.pm:  no strict 'refs';
\n\n

<!--EDIT|opi|1170318812-->
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.
betterworld
 2007-02-01 15:25
#73853 #73853
User since
2003-08-21
2614 Artikel
ModeratorIn

user image
[quote=opi,01.02.2007, 09:30]Wer ist denn bloonix? :p[/quote]
Entschuldigung, ich habe mich irgendwie vertippt. Ist gefixt.
bloonix
 2007-02-01 15:52
#73854 #73854
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=betterworld,01.02.2007, 14:25]Entschuldigung, ich habe mich irgendwie vertippt.  Ist gefixt.[/quote]
Kein Problem. Die Macht der Gewohnheit. =)
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.
Strat
 2007-02-02 12:23
#73855 #73855
User since
2003-08-04
5246 Artikel
ModeratorIn
[Homepage] [default_avatar]
To strict or not to strict: wenn man gut begruenden kann, wieso man strict fuer eine Anweisung deaktiviert (und das dann auch dokumentiert oder kommentiert), sehe ich da kein Problem.

Ich wuerde sowas auch on-the-fly machen, weil ich es fuer unnuetz halte, da x Mal dieselbe Routine mit minimalen Veraenderungen zu schreiben, und es gut kommentieren. So braucht der arme Kerl, der das mal erbt, nur ein paar Kommentare lesen, als sich durch eventuell eine Menge Code zu quaelen, bevor er zu dem Schluss kommt, dass der eh alles das gleiche macht.
Die Liste der Subroutinen wuerde ich allerdings in ein Array (oder sogar in eine Config) auslagern, weil es dann bei Erweiterungen einfacher handelbar wird.


Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
# config
our @AutoGeneratedSubroutineNames = qw( foo bar );

# dann irgendwo weiter unten...
for my $subName ( @AutoGeneratedSubroutineNames ) {

# verankere vorgenerierte Subroutinen ueber symbolische Referenz
# in der Symboltabelle (Sub::Install ist leider kein Standardmodul):
no strict 'refs';
*$subName = sub { my $self = shift; $self->foobar($subName, @_); };

} # for @AutoGeneratedSubroutineNames

Aber die Schreibweise $Package::{$_} = sub {...}; finde ich trotzdem schoener...\n\n

<!--EDIT|Strat|1170411843-->
perl -le "s::*erlco'unaty.'.dk':e,y;*kn:ai;penmic;;print"
http://www.fabiani.net/
bloonix
 2007-02-02 15:51
#73856 #73856
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
[quote=Strat,02.02.2007, 11:23]Die Liste der Subroutinen wuerde ich allerdings in ein Array (oder sogar in eine Config) auslagern, weil es dann bei Erweiterungen einfacher handelbar wird.[/quote]
Das ist eine gute Idee und ich habe es auch mal probiert. Zunächst aller-
dings mit :

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package BeginTest;

use strict;
use warnings;

my @ROUTINES = qw(echo);

BEGIN {
  for my $sub (@ROUTINES) {
     no strict 'refs';
     *{"$sub"} = sub { shift if $_[0] eq __PACKAGE__; print $_[0]; };
  }
}

1;


Das hat natürlich nicht geklappt, da der BEGIN-Block vorher verarbeitet
wird. Was mich nur stutzig macht ist, dass ich keine Fehlermeldung
erhalten habe, dass @ROUTINES nicht existiert.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package BeginTest;

use strict;
use warnings;

use constant ROUTINES => qw(echo);

BEGIN {
   for my $sub (ROUTINES) {
     no strict 'refs';
     *{"$sub"} = sub { shift if $_[0] eq __PACKAGE__; print $_[0]; };
  }
}

1;


Das hat geklappt. Heißt das, dass use-Anweisungen vor BEGIN verarbeitet
werden?

Das Skript...
Code: (dl )
1
2
3
4
5
6
use strict;
use warnings;
use lib '.';
use BeginTest;

BeginTest->echo("Hello World!\n");
\n\n

<!--EDIT|opi|1170424846-->
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.
bloonix
 2007-02-02 16:05
#73857 #73857
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Da fällt mir gerade ein, dass ich die Syslog-Level sowieso als Konstanten
definiert habe:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
use constant DEBUG     =>  0;
use constant INFO => 1;
use constant NOTICE => 2;
use constant NOTE => 2;
use constant WARNING => 3;
use constant ERROR => 4;
use constant ERR => 4;
use constant CRITICAL => 5;
use constant CRIT => 5;
use constant ALERT => 6;
use constant EMERGENCY => 7;
use constant EMERG => 7;


Ich könnte also rechte einfach Keys erzeugen.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use constant LEVELS => {
DEBUG => 0,
INFO => 1,
NOTICE => 2,
NOTE => 2,
WARNING => 3,
ERROR => 4,
ERR => 4,
CRITICAL => 5,
CRIT => 5,
ALERT => 6,
EMERGENCY => 7,
EMERG => 7,
}

BEGIN {
for my $level (keys LEVELS) {
no strict 'refs';
my $sub = lc($level);
*{"$sub"} = sub { my $self = shift; $self->_print($level, @_); };
}
}


Aber dann wären die Werte leider nicht mehr konstant. =(
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.
pq
 2007-02-02 16:36
#73858 #73858
User since
2003-08-04
12209 Artikel
Admin1
[Homepage]
user image
Code: (dl )
1
2
3
4
5
my @ROUTINES;
BEGIN {
 @ROUTINES = qw(echo);
 ...
}

der BEGIN-block wird bei deiner version vor der zuweisung ausgeführt, aber die
deklaration passiert vor dem BEGIN.\n\n

<!--EDIT|pq|1170427024-->
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
betterworld
 2007-02-02 17:11
#73859 #73859
User since
2003-08-21
2614 Artikel
ModeratorIn

user image
[quote=Strat,02.02.2007, 11:23]To strict or not to strict: wenn man gut begruenden kann, wieso man strict fuer eine Anweisung deaktiviert (und das dann auch dokumentiert oder kommentiert), sehe ich da kein Problem.[/quote]
Du meinst also, es ist eine gute Begründung, wenn man keine Lust hat, $Package::{$name} zu schreiben? ;)
<< |< 1 2 >| >> 19 Einträge, 2 Seiten



View all threads created 2007-01-30 16:53.