| 1 | [[PageOutline]] |
| 2 | |
| 3 | = Perl = |
| 4 | == Python で言うところの getattr してみる == |
| 5 | ていうかむしろ eval してるだけ. |
| 6 | eval じゃなくてホントに getattr したいんだけど,どうやってやるんだろう? |
| 7 | |
| 8 | * {{{hoge.pm}}} |
| 9 | {{{ |
| 10 | #!perl |
| 11 | use strict; |
| 12 | use warnings; |
| 13 | |
| 14 | package hoge; |
| 15 | |
| 16 | our $foo = "hogeraccho"; |
| 17 | |
| 18 | 1; |
| 19 | |
| 20 | __END__ |
| 21 | }}} |
| 22 | * {{{unko.pm}}} |
| 23 | {{{ |
| 24 | #!perl |
| 25 | use strict; |
| 26 | use warnings; |
| 27 | |
| 28 | package unko; |
| 29 | |
| 30 | our $foo = "unkokko"; |
| 31 | |
| 32 | 1; |
| 33 | |
| 34 | __END__ |
| 35 | }}} |
| 36 | * {{{modules}}} ファイル |
| 37 | {{{ |
| 38 | unko |
| 39 | hoge |
| 40 | foo |
| 41 | }}} |
| 42 | * {{{main.pl}}} |
| 43 | {{{ |
| 44 | #!perl |
| 45 | #!/usr/bin/env perl |
| 46 | |
| 47 | use strict; |
| 48 | use warnings; |
| 49 | use Symbol; |
| 50 | |
| 51 | my $sym = gensym; |
| 52 | open($sym, "modules"); |
| 53 | while (my $module = <$sym>) { |
| 54 | chomp $module; |
| 55 | next unless $module; |
| 56 | |
| 57 | eval { |
| 58 | require $module . '.pm'; |
| 59 | my $foo = eval("\$${module}::foo"); |
| 60 | print "\$${module}::foo = $foo\n"; |
| 61 | }; |
| 62 | warn "WARNING: $@" if $@; |
| 63 | } |
| 64 | close($sym); |
| 65 | }}} |
| 66 | * 実行結果 |
| 67 | {{{ |
| 68 | #!sh |
| 69 | bash$ perl main.pl |
| 70 | $unko::foo = unkokko |
| 71 | $hoge::foo = hogeraccho |
| 72 | WARNING: Can't locate foo.pm in @INC ... |
| 73 | }}} |