perltie >
5.10.0
との差分
perltie 5.10.0 と 5.30.0 の差分
| 1 | 1 | |
| 2 | 2 | =encoding euc-jp |
| 3 | 3 | |
| 4 | 4 | =head1 NAME |
| 5 | 5 | X<tie> |
| 6 | 6 | |
| 7 | 7 | =begin original |
| 8 | 8 | |
| 9 | 9 | perltie - how to hide an object class in a simple variable |
| 10 | 10 | |
| 11 | 11 | =end original |
| 12 | 12 | |
| 13 | 13 | perltie - オブジェクトクラスを単純な変数に隠す方法 |
| 14 | 14 | |
| 15 | 15 | =head1 SYNOPSIS |
| 16 | 16 | |
| 17 | 17 | tie VARIABLE, CLASSNAME, LIST |
| 18 | 18 | |
| 19 | 19 | $object = tied VARIABLE |
| 20 | 20 | |
| 21 | 21 | untie VARIABLE |
| 22 | 22 | |
| 23 | 23 | =head1 DESCRIPTION |
| 24 | 24 | |
| 25 | 25 | =begin original |
| 26 | 26 | |
| 27 | 27 | Prior to release 5.0 of Perl, a programmer could use dbmopen() |
| 28 | 28 | to connect an on-disk database in the standard Unix dbm(3x) |
| 29 | 29 | format magically to a %HASH in their program. However, their Perl was either |
| 30 | 30 | built with one particular dbm library or another, but not both, and |
| 31 | 31 | you couldn't extend this mechanism to other packages or types of variables. |
| 32 | 32 | |
| 33 | 33 | =end original |
| 34 | 34 | |
| 35 | 35 | 5.0 より前の Perl では、プログラマは dbmopen() を使ってディスクにある |
| 36 | 36 | 標準 UNIX dbm(3x) フォーマットのデータベースをプログラム中の %HASH と |
| 37 | 37 | 結び付けることができました。 |
| 38 | 38 | しかしながら、Perl は特定の dbm ライブラリか別のものを使って |
| 39 | ビルドすることができたものの、両方一度にはできませんでした | |
| 39 | ビルドすることができたものの、両方一度にはできませんでした; そして、この | |
| 40 | ||
| 40 | 仕組みを他のパッケージや変数の型に拡張することはできなかったのです。 | |
| 41 | できなかったのです。 | |
| 42 | 41 | |
| 43 | 42 | =begin original |
| 44 | 43 | |
| 45 | 44 | Now you can. |
| 46 | 45 | |
| 47 | 46 | =end original |
| 48 | 47 | |
| 49 | 48 | 今はできます。 |
| 50 | 49 | |
| 51 | 50 | =begin original |
| 52 | 51 | |
| 53 | 52 | The tie() function binds a variable to a class (package) that will provide |
| 54 | 53 | the implementation for access methods for that variable. Once this magic |
| 55 | 54 | has been performed, accessing a tied variable automatically triggers |
| 56 | 55 | method calls in the proper class. The complexity of the class is |
| 57 | 56 | hidden behind magic methods calls. The method names are in ALL CAPS, |
| 58 | 57 | which is a convention that Perl uses to indicate that they're called |
| 59 | 58 | implicitly rather than explicitly--just like the BEGIN() and END() |
| 60 | 59 | functions. |
| 61 | 60 | |
| 62 | 61 | =end original |
| 63 | 62 | |
| 64 | 63 | tie() 関数は変数と、その変数に対するアクセスメソッドの実装を提供する |
| 65 | 64 | クラス(パッケージ)とを結び付けます。 |
| 66 | 65 | この魔法が一度働けば、tie された変数は自動的に適切なクラスにある |
| 67 | 66 | メソッド呼び出しを実行します。 |
| 68 | 67 | クラスのすべての複雑性はメソッド呼び出しに隠されます。 |
| 69 | 68 | それらのメソッドの名前は、BEGIN() や END() と同様に(そのメソッドを) Perl が |
| 70 | 69 | こっそりと呼び出すことを示すための規約に従って全て大文字です。 |
| 71 | 70 | |
| 72 | 71 | =begin original |
| 73 | 72 | |
| 74 | 73 | In the tie() call, C<VARIABLE> is the name of the variable to be |
| 75 | 74 | enchanted. C<CLASSNAME> is the name of a class implementing objects of |
| 76 | 75 | the correct type. Any additional arguments in the C<LIST> are passed to |
| 77 | 76 | the appropriate constructor method for that class--meaning TIESCALAR(), |
| 78 | 77 | TIEARRAY(), TIEHASH(), or TIEHANDLE(). (Typically these are arguments |
| 79 | 78 | such as might be passed to the dbminit() function of C.) The object |
| 80 | 79 | returned by the "new" method is also returned by the tie() function, |
| 81 | 80 | which would be useful if you wanted to access other methods in |
| 82 | 81 | C<CLASSNAME>. (You don't actually have to return a reference to a right |
| 83 | 82 | "type" (e.g., HASH or C<CLASSNAME>) so long as it's a properly blessed |
| 84 | 83 | object.) You can also retrieve a reference to the underlying object |
| 85 | 84 | using the tied() function. |
| 86 | 85 | |
| 87 | 86 | =end original |
| 88 | 87 | |
| 89 | 88 | tie() コールの中で、C<VARIABLE> は魔法を掛けられる変数の名前です。 |
| 90 | 89 | C<CLASSNAME> は正しい型のオブジェクトを実装するクラスの名前です。 |
| 91 | 90 | C<LIST> にあるその他の引数はクラスの適切なコンストラクタメソッド |
| 92 | 91 | TIESCALAR()、TIEARRAY()、TIEHASH()、TIEHANDLE() のいずれかに |
| 93 | 渡されます | |
| 92 | 渡されます。 | |
| 94 | 同じものです | |
| 93 | (典型的にはこれらの引数は C の dbminit() 関数に渡すのと同じものです。) | |
| 95 | 94 | "new" メソッドから返されたオブジェクトは同様に関数 tie() からも |
| 96 | 返されます | |
| 95 | 返されます; これはあなたが C<CLASSNAME> の中の別のメソッドで | |
| 97 | ||
| 96 | アクセスしたいというときに便利でしょう。 | |
| 98 | ||
| 97 | (あなたは実際には正しい「型」(HASH か C<CLASSNAME>) の参照を、それが適切な | |
| 99 | ||
| 98 | bless されたオブジェクトであるということから返す必要はありません。) | |
| 100 | 返す必要はありません)。 | |
| 101 | 99 | また、関数 tied() を使って、基礎となるオブジェクトへのリファレンスを |
| 102 | 100 | 取得することができます。 |
| 103 | 101 | |
| 104 | 102 | =begin original |
| 105 | 103 | |
| 106 | 104 | Unlike dbmopen(), the tie() function will not C<use> or C<require> a module |
| 107 | 105 | for you--you need to do that explicitly yourself. |
| 108 | 106 | |
| 109 | 107 | =end original |
| 110 | 108 | |
| 111 | 109 | dbmopen() とは異なり、tie() はモジュールを C<use> したり C<require> したり |
| 112 | することはありません | |
| 110 | することはありません--あなたが、自分自身でそれを明示的に | |
| 113 | ||
| 111 | 行わなければなりません。 | |
| 114 | 112 | |
| 115 | 113 | =head2 Tying Scalars |
| 116 | 114 | X<scalar, tying> |
| 117 | 115 | |
| 118 | 116 | (スカラを tie する) |
| 119 | 117 | |
| 120 | 118 | =begin original |
| 121 | 119 | |
| 122 | 120 | A class implementing a tied scalar should define the following methods: |
| 123 | 121 | TIESCALAR, FETCH, STORE, and possibly UNTIE and/or DESTROY. |
| 124 | 122 | |
| 125 | 123 | =end original |
| 126 | 124 | |
| 127 | 125 | tie されたスカラを実装するクラスは、TIESCALAR, FETCH, STORE, |
| 128 | 126 | そして可能であれば UNTIE や DESTROY といったメソッドを定義しておくべきです。 |
| 129 | 127 | |
| 130 | 128 | =begin original |
| 131 | 129 | |
| 132 | 130 | Let's look at each in turn, using as an example a tie class for |
| 133 | 131 | scalars that allows the user to do something like: |
| 134 | 132 | |
| 135 | 133 | =end original |
| 136 | 134 | |
| 137 | 135 | 以下のような操作を、ユーザーに許しているスカラに対してクラスを |
| 138 | 136 | tie する例を使って順に見て行きましょう。 |
| 139 | 137 | |
| 140 | 138 | tie $his_speed, 'Nice', getppid(); |
| 141 | 139 | tie $my_speed, 'Nice', $$; |
| 142 | 140 | |
| 143 | 141 | =begin original |
| 144 | 142 | |
| 145 | 143 | And now whenever either of those variables is accessed, its current |
| 146 | 144 | system priority is retrieved and returned. If those variables are set, |
| 147 | 145 | then the process's priority is changed! |
| 148 | 146 | |
| 149 | 147 | =end original |
| 150 | 148 | |
| 151 | 149 | こうした後ではこれらの変数のいずれかがアクセスされたときには、カレントの |
| 152 | 150 | システム優先順位が取得されたり返されたりします。 |
| 153 | 151 | もし変数に代入が行われれば、プロセスの優先順位は変更されます! |
| 154 | 152 | |
| 155 | 153 | =begin original |
| 156 | 154 | |
| 157 | 155 | We'll use Jarkko Hietaniemi <F<jhi@iki.fi>>'s BSD::Resource class (not |
| 158 | 156 | included) to access the PRIO_PROCESS, PRIO_MIN, and PRIO_MAX constants |
| 159 | 157 | from your system, as well as the getpriority() and setpriority() system |
| 160 | 158 | calls. Here's the preamble of the class. |
| 161 | 159 | |
| 162 | 160 | =end original |
| 163 | 161 | |
| 164 | 162 | システムの PRIO_PROCESS, PRIO_MIN, PRIO_MAX といった定数に |
| 165 | 163 | アクセスするために Jarkko Hietaniemi <F<jhi@iki.fi>> の |
| 166 | 164 | BSD::Resource クラスを使います。 |
| 167 | 165 | 以下はこのクラスの前置きです。 |
| 168 | 166 | |
| 169 | 167 | package Nice; |
| 170 | 168 | use Carp; |
| 171 | 169 | use BSD::Resource; |
| 172 | 170 | use strict; |
| 173 | 171 | $Nice::DEBUG = 0 unless defined $Nice::DEBUG; |
| 174 | 172 | |
| 175 | 173 | =over 4 |
| 176 | 174 | |
| 177 | 175 | =item TIESCALAR classname, LIST |
| 178 | 176 | X<TIESCALAR> |
| 179 | 177 | |
| 180 | 178 | =begin original |
| 181 | 179 | |
| 182 | 180 | This is the constructor for the class. That means it is |
| 183 | 181 | expected to return a blessed reference to a new scalar |
| 184 | 182 | (probably anonymous) that it's creating. For example: |
| 185 | 183 | |
| 186 | 184 | =end original |
| 187 | 185 | |
| 188 | これはクラス | |
| 186 | これはこのクラスのコンストラクタです。 | |
| 189 | 187 | その役割は作成された新たな(おそらくは無名の)スカラへの bless された |
| 190 | 188 | 参照を返すことです。 |
| 191 | ||
| 189 | 例えば: | |
| 192 | 190 | |
| 193 | | |
| 191 | sub TIESCALAR { | |
| 194 | | |
| 192 | my $class = shift; | |
| 195 | | |
| 193 | my $pid = shift || $$; # 0 means me | |
| 196 | 194 | |
| 197 | | |
| 195 | if ($pid !~ /^\d+$/) { | |
| 198 | | |
| 196 | carp "Nice::Tie::Scalar got non-numeric pid $pid" if $^W; | |
| 199 | | |
| 197 | return undef; | |
| 200 | | |
| 198 | } | |
| 201 | 199 | |
| 202 | | |
| 200 | unless (kill 0, $pid) { # EPERM or ERSCH, no doubt | |
| 203 | | |
| 201 | carp "Nice::Tie::Scalar got bad pid $pid: $!" if $^W; | |
| 204 | | |
| 202 | return undef; | |
| 205 | | |
| 203 | } | |
| 206 | 204 | |
| 207 | | |
| 205 | return bless \$pid, $class; | |
| 208 | | |
| 206 | } | |
| 209 | 207 | |
| 210 | 208 | =begin original |
| 211 | 209 | |
| 212 | 210 | This tie class has chosen to return an error rather than raising an |
| 213 | 211 | exception if its constructor should fail. While this is how dbmopen() works, |
| 214 | 212 | other classes may well not wish to be so forgiving. It checks the global |
| 215 | 213 | variable C<$^W> to see whether to emit a bit of noise anyway. |
| 216 | 214 | |
| 217 | 215 | =end original |
| 218 | 216 | |
| 219 | 217 | このtie クラスでは、コンストラクタが失敗したときに例外を起こすのではなく |
| 220 | 218 | エラーを返すことを選択しました。 |
| 221 | 219 | dbmopen() が動作している間に、他のクラスは例外が起きることを |
| 222 | 220 | 好まないかもしれないからです。 |
| 223 | 221 | グローバル変数 C<$^W> でエラーメッセージを出すかどうかを検査しています。 |
| 224 | 222 | |
| 225 | 223 | =item FETCH this |
| 226 | 224 | X<FETCH> |
| 227 | 225 | |
| 228 | 226 | =begin original |
| 229 | 227 | |
| 230 | 228 | This method will be triggered every time the tied variable is accessed |
| 231 | 229 | (read). It takes no arguments beyond its self reference, which is the |
| 232 | 230 | object representing the scalar we're dealing with. Because in this case |
| 233 | 231 | we're using just a SCALAR ref for the tied scalar object, a simple $$self |
| 234 | 232 | allows the method to get at the real value stored there. In our example |
| 235 | 233 | below, that real value is the process ID to which we've tied our variable. |
| 236 | 234 | |
| 237 | 235 | =end original |
| 238 | 236 | |
| 239 | 237 | このメソッドは tie された変数がアクセス(読み出し)される度に起動されます。 |
| 240 | 238 | これは自分のリファレンス、つまり私たちが扱おうとしている |
| 241 | 239 | スカラを表現するオブジェクトの他に引数は取りません。 |
| 242 | 240 | この場合、単に SCALAR の参照をtieされたスカラオブジェクトとして |
| 243 | 241 | 使うので、単純な $$self がそこに格納されている実際の値を取得する |
| 244 | 242 | メソッドとなります。 |
| 245 | 243 | 以下に示した例では、実際の値は変数に tie されたプロセス ID です。 |
| 246 | 244 | |
| 247 | 245 | sub FETCH { |
| 248 | 246 | my $self = shift; |
| 249 | 247 | confess "wrong type" unless ref $self; |
| 250 | 248 | croak "usage error" if @_; |
| 251 | 249 | my $nicety; |
| 252 | 250 | local($!) = 0; |
| 253 | 251 | $nicety = getpriority(PRIO_PROCESS, $$self); |
| 254 | 252 | if ($!) { croak "getpriority failed: $!" } |
| 255 | 253 | return $nicety; |
| 256 | 254 | } |
| 257 | 255 | |
| 258 | 256 | =begin original |
| 259 | 257 | |
| 260 | 258 | This time we've decided to blow up (raise an exception) if the renice |
| 261 | 259 | fails--there's no place for us to return an error otherwise, and it's |
| 262 | 260 | probably the right thing to do. |
| 263 | 261 | |
| 264 | 262 | =end original |
| 265 | 263 | |
| 266 | ここでは、renice に失敗した場合には例外を引き起こすようにしました | |
| 264 | ここでは、renice に失敗した場合には例外を引き起こすようにしました-- | |
| 267 | 265 | エラーを返すための場所がなく、例外を引き起こすことがおそらく妥当です。 |
| 268 | 266 | |
| 269 | 267 | =item STORE this, value |
| 270 | 268 | X<STORE> |
| 271 | 269 | |
| 272 | 270 | =begin original |
| 273 | 271 | |
| 274 | 272 | This method will be triggered every time the tied variable is set |
| 275 | 273 | (assigned). Beyond its self reference, it also expects one (and only one) |
| 276 | argument | |
| 274 | argument: the new value the user is trying to assign. Don't worry about | |
| 277 | returning a value from STORE | |
| 275 | returning a value from STORE; the semantic of assignment returning the | |
| 278 | 276 | assigned value is implemented with FETCH. |
| 279 | 277 | |
| 280 | 278 | =end original |
| 281 | 279 | |
| 282 | 280 | このメソッドは tie された変数に代入される度毎に起動されます。 |
| 283 | 281 | 自分の参照のほか、ただ一つの引数としてユーザーが代入しようとする |
| 284 | 282 | 新しい値を取ります。 |
| 285 | STORE から返される値は気にしないで下さい | |
| 283 | STORE から返される値は気にしないで下さい; | |
| 286 | 284 | 代入された値を返す代入の動作は FETCH で実装されています。 |
| 287 | 285 | |
| 288 | | |
| 286 | sub STORE { | |
| 289 | | |
| 287 | my $self = shift; | |
| 290 | | |
| 288 | confess "wrong type" unless ref $self; | |
| 291 | | |
| 289 | my $new_nicety = shift; | |
| 292 | | |
| 290 | croak "usage error" if @_; | |
| 293 | 291 | |
| 294 | | |
| 292 | if ($new_nicety < PRIO_MIN) { | |
| 295 | | |
| 293 | carp sprintf | |
| 296 | | |
| 294 | "WARNING: priority %d less than minimum system priority %d", | |
| 297 | | |
| 295 | $new_nicety, PRIO_MIN if $^W; | |
| 298 | | |
| 296 | $new_nicety = PRIO_MIN; | |
| 299 | | |
| 297 | } | |
| 300 | 298 | |
| 301 | | |
| 299 | if ($new_nicety > PRIO_MAX) { | |
| 302 | | |
| 300 | carp sprintf | |
| 303 | | |
| 301 | "WARNING: priority %d greater than maximum system priority %d", | |
| 304 | | |
| 302 | $new_nicety, PRIO_MAX if $^W; | |
| 305 | | |
| 303 | $new_nicety = PRIO_MAX; | |
| 306 | | |
| 304 | } | |
| 307 | 305 | |
| 308 | | |
| 306 | unless (defined setpriority(PRIO_PROCESS, | |
| 309 | | |
| 307 | $$self, | |
| 310 | | |
| 308 | $new_nicety)) | |
| 311 | | |
| 309 | { | |
| 310 | confess "setpriority failed: $!"; | |
| 311 | } | |
| 312 | } | |
| 312 | 313 | |
| 313 | 314 | =item UNTIE this |
| 314 | 315 | X<UNTIE> |
| 315 | 316 | |
| 316 | 317 | =begin original |
| 317 | 318 | |
| 318 | 319 | This method will be triggered when the C<untie> occurs. This can be useful |
| 319 | 320 | if the class needs to know when no further calls will be made. (Except DESTROY |
| 320 | 321 | of course.) See L<The C<untie> Gotcha> below for more details. |
| 321 | 322 | |
| 322 | 323 | =end original |
| 323 | 324 | |
| 324 | 325 | このメソッドは、C<untie> が発生すると起動されます。 |
| 325 | 326 | これは、クラスが、もはや呼び出されなくなるのはいつかを知る必要がある場合に |
| 326 | 327 | 便利です。 |
| 327 | 328 | (もちろん DESTROY を除いてです。) |
| 328 | 329 | さらなる詳細については後述する L<The C<untie> Gotcha> を参照してください。 |
| 329 | 330 | |
| 330 | 331 | =item DESTROY this |
| 331 | 332 | X<DESTROY> |
| 332 | 333 | |
| 333 | 334 | =begin original |
| 334 | 335 | |
| 335 | 336 | This method will be triggered when the tied variable needs to be destructed. |
| 336 | 337 | As with other object classes, such a method is seldom necessary, because Perl |
| 337 | 338 | deallocates its moribund object's memory for you automatically--this isn't |
| 338 | 339 | C++, you know. We'll use a DESTROY method here for debugging purposes only. |
| 339 | 340 | |
| 340 | 341 | =end original |
| 341 | 342 | |
| 342 | このメソッドは tie された変数を破棄する必要があるときに | |
| 343 | このメソッドは tie された変数を破棄する必要があるときに呼び出されます。 | |
| 343 | 他のオブジェクトクラスと同じように、このようなメソッドは | |
| 344 | 他のオブジェクトクラスと同じように、このようなメソッドはほとんど | |
| 344 | ||
| 345 | 必要ありません; それは、Perl は消滅しかかったオブジェクトのメモリを自動的に | |
| 345 | ||
| 346 | 解放するからです--これは C++ ではないのです; いいですね? | |
| 346 | 解放するからです。 | |
| 347 | これは C++ ではないのです。 | |
| 348 | いいですね?。 | |
| 349 | 347 | 私たちはここでは DESTROY メソッドをデバッグのためだけに使います。 |
| 350 | 348 | |
| 351 | 349 | sub DESTROY { |
| 352 | 350 | my $self = shift; |
| 353 | 351 | confess "wrong type" unless ref $self; |
| 354 | 352 | carp "[ Nice::DESTROY pid $$self ]" if $Nice::DEBUG; |
| 355 | 353 | } |
| 356 | 354 | |
| 357 | 355 | =back |
| 358 | 356 | |
| 359 | 357 | =begin original |
| 360 | 358 | |
| 361 | 359 | That's about all there is to it. Actually, it's more than all there |
| 362 | 360 | is to it, because we've done a few nice things here for the sake |
| 363 | 361 | of completeness, robustness, and general aesthetics. Simpler |
| 364 | 362 | TIESCALAR classes are certainly possible. |
| 365 | 363 | |
| 366 | 364 | =end original |
| 367 | 365 | |
| 368 | 366 | これがすべきことの全てです。 |
| 369 | 実際のところ、それよりも多くのことがあります | |
| 367 | 実際のところ、それよりも多くのことがあります; ですから、私たちはここで | |
| 370 | ||
| 368 | ちょっとした完全性、堅牢性、一般的な美しさというものを込めました。 | |
| 371 | いうものを込めました。 | |
| 372 | 369 | もっと簡単な TIESCALAR クラスを作ることも可能です。 |
| 373 | 370 | |
| 374 | 371 | =head2 Tying Arrays |
| 375 | 372 | X<array, tying> |
| 376 | 373 | |
| 377 | 374 | (配列を tie する) |
| 378 | 375 | |
| 379 | 376 | =begin original |
| 380 | 377 | |
| 381 | 378 | A class implementing a tied ordinary array should define the following |
| 382 | methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE | |
| 379 | methods: TIEARRAY, FETCH, STORE, FETCHSIZE, STORESIZE, CLEAR | |
| 380 | and perhaps UNTIE and/or DESTROY. | |
| 383 | 381 | |
| 384 | 382 | =end original |
| 385 | 383 | |
| 386 | tie された配列を実装するクラスは TIEARRAY, FETCH, STORE, FETCHSIZE, | |
| 384 | tie された配列を実装するクラスは TIEARRAY, FETCH, STORE, FETCHSIZE, CLEAR, | |
| 387 | 385 | STORESIZE、そしておそらく UNTIE や DESTROY といったメソッドを |
| 388 | 386 | 実装すべきでしょう。 |
| 389 | 387 | |
| 390 | 388 | =begin original |
| 391 | 389 | |
| 392 | 390 | FETCHSIZE and STORESIZE are used to provide C<$#array> and |
| 393 | 391 | equivalent C<scalar(@array)> access. |
| 394 | 392 | |
| 395 | 393 | =end original |
| 396 | 394 | |
| 397 | 395 | FETCHSIZE と STORESIZE は C<$#array> と |
| 398 | 396 | C<scalar(@array)> アクセスに等価なものを提供します。 |
| 399 | 397 | |
| 400 | 398 | =begin original |
| 401 | 399 | |
| 402 | 400 | The methods POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, and EXISTS are |
| 403 | 401 | required if the perl operator with the corresponding (but lowercase) name |
| 404 | 402 | is to operate on the tied array. The B<Tie::Array> class can be used as a |
| 405 | 403 | base class to implement the first five of these in terms of the basic |
| 406 | 404 | methods above. The default implementations of DELETE and EXISTS in |
| 407 | 405 | B<Tie::Array> simply C<croak>. |
| 408 | 406 | |
| 409 | 407 | =end original |
| 410 | 408 | |
| 411 | 409 | POP, PUSH, SHIFT, UNSHIFT, SPLICE, DELETE, EXIST といったメソッドは |
| 412 | 410 | 同名の perl の演算子(ただし小文字)が tie された配列に対して |
| 413 | 411 | 操作を行うときに必要となります。 |
| 414 | 412 | B<Tie::Array> クラスは、これらのうち、最初の 5 つの基本的なメソッドを |
| 415 | 413 | 実装するための基底クラスとして使用できます。 |
| 416 | 414 | B<Tie::Array> での DELETE と EXISTS のデフォルトの実装は |
| 417 | 415 | 単なる C<croak> です。 |
| 418 | 416 | |
| 419 | 417 | =begin original |
| 420 | 418 | |
| 421 | 419 | In addition EXTEND will be called when perl would have pre-extended |
| 422 | 420 | allocation in a real array. |
| 423 | 421 | |
| 424 | 422 | =end original |
| 425 | 423 | |
| 426 | 424 | それに加え、EXTEND は perl が実際の配列中であらかじめ |
| 427 | 425 | 拡張するようなときに呼び出されます。 |
| 428 | 426 | |
| 429 | 427 | =begin original |
| 430 | 428 | |
| 431 | 429 | For this discussion, we'll implement an array whose elements are a fixed |
| 432 | 430 | size at creation. If you try to create an element larger than the fixed |
| 433 | 431 | size, you'll take an exception. For example: |
| 434 | 432 | |
| 435 | 433 | =end original |
| 436 | 434 | |
| 437 | 435 | ここでの説明のため、要素数が生成時に固定されたサイズである配列を実装します。 |
| 438 | 436 | 固定サイズを越えた要素を作ろうとすると、例外が発生します。 |
| 439 | 437 | 例えば: |
| 440 | 438 | |
| 441 | 439 | use FixedElem_Array; |
| 442 | 440 | tie @array, 'FixedElem_Array', 3; |
| 443 | 441 | $array[0] = 'cat'; # ok. |
| 444 | 442 | $array[1] = 'dogs'; # exception, length('dogs') > 3. |
| 445 | 443 | |
| 446 | 444 | =begin original |
| 447 | 445 | |
| 448 | 446 | The preamble code for the class is as follows: |
| 449 | 447 | |
| 450 | 448 | =end original |
| 451 | 449 | |
| 452 | 450 | このクラスに対する 前置きコードは以下の通りです。 |
| 453 | 451 | |
| 454 | 452 | package FixedElem_Array; |
| 455 | 453 | use Carp; |
| 456 | 454 | use strict; |
| 457 | 455 | |
| 458 | 456 | =over 4 |
| 459 | 457 | |
| 460 | 458 | =item TIEARRAY classname, LIST |
| 461 | 459 | X<TIEARRAY> |
| 462 | 460 | |
| 463 | 461 | =begin original |
| 464 | 462 | |
| 465 | 463 | This is the constructor for the class. That means it is expected to |
| 466 | 464 | return a blessed reference through which the new array (probably an |
| 467 | 465 | anonymous ARRAY ref) will be accessed. |
| 468 | 466 | |
| 469 | 467 | =end original |
| 470 | 468 | |
| 471 | これはクラス | |
| 469 | これはこのクラスのコンストラクタです。 | |
| 472 | 470 | その役割は作成された新たな(おそらくは無名の配列の参照)配列への |
| 473 | 471 | bless された参照を返すことです。 |
| 474 | 472 | |
| 475 | 473 | =begin original |
| 476 | 474 | |
| 477 | 475 | In our example, just to show you that you don't I<really> have to return an |
| 478 | 476 | ARRAY reference, we'll choose a HASH reference to represent our object. |
| 479 | 477 | A HASH works out well as a generic record type: the C<{ELEMSIZE}> field will |
| 480 | 478 | store the maximum element size allowed, and the C<{ARRAY}> field will hold the |
| 481 | 479 | true ARRAY ref. If someone outside the class tries to dereference the |
| 482 | 480 | object returned (doubtless thinking it an ARRAY ref), they'll blow up. |
| 483 | 481 | This just goes to show you that you should respect an object's privacy. |
| 484 | 482 | |
| 485 | 483 | =end original |
| 486 | 484 | |
| 487 | 485 | 私たちの例では、あなたにあなたが I<実際には> ARRAY のリファレンスを |
| 488 | 486 | 返さなくてもよいということを示すためだけに、使用するオブジェクトを |
| 489 | 487 | 表わす HASH の参照を選びました。 |
| 490 | HASH は汎用的なレコード型と同じように働きます | |
| 488 | HASH は汎用的なレコード型と同じように働きます: C<{ELEMSIZE}> フィールドは | |
| 491 | ||
| 489 | 許される最大の要素の数を格納し、C<{ARRAY}> フィールドは本物の ARRAY の | |
| 492 | ||
| 490 | リファレンスを保持します。 | |
| 493 | 491 | 誰かがクラスの外側で返されたオブジェクトのデリファレンスを試みた場合 |
| 494 | 492 | (それが ARRAY のリファレンスであると疑いなく考えて)、それは失敗します。 |
| 495 | 493 | これはあなたがオブジェクトのプライバシーを尊重すべきであるという |
| 496 | 494 | ことなのです。 |
| 497 | 495 | |
| 498 | 496 | sub TIEARRAY { |
| 499 | 497 | my $class = shift; |
| 500 | 498 | my $elemsize = shift; |
| 501 | 499 | if ( @_ || $elemsize =~ /\D/ ) { |
| 502 | 500 | croak "usage: tie ARRAY, '" . __PACKAGE__ . "', elem_size"; |
| 503 | 501 | } |
| 504 | 502 | return bless { |
| 505 | 503 | ELEMSIZE => $elemsize, |
| 506 | 504 | ARRAY => [], |
| 507 | 505 | }, $class; |
| 508 | 506 | } |
| 509 | 507 | |
| 510 | 508 | =item FETCH this, index |
| 511 | 509 | X<FETCH> |
| 512 | 510 | |
| 513 | 511 | =begin original |
| 514 | 512 | |
| 515 | 513 | This method will be triggered every time an individual element the tied array |
| 516 | 514 | is accessed (read). It takes one argument beyond its self reference: the |
| 517 | 515 | index whose value we're trying to fetch. |
| 518 | 516 | |
| 519 | 517 | =end original |
| 520 | 518 | |
| 521 | 519 | このメソッドは tie された配列の個々の要素がアクセス(読み出し)される毎に |
| 522 | 520 | 起動されます。 |
| 523 | 521 | これは自分の参照のほかに、一つの引数、フェッチしようとする値の |
| 524 | 522 | インデックスをとります。 |
| 525 | 523 | |
| 526 | 524 | sub FETCH { |
| 527 | 525 | my $self = shift; |
| 528 | 526 | my $index = shift; |
| 529 | 527 | return $self->{ARRAY}->[$index]; |
| 530 | 528 | } |
| 531 | 529 | |
| 532 | 530 | =begin original |
| 533 | 531 | |
| 534 | 532 | If a negative array index is used to read from an array, the index |
| 535 | 533 | will be translated to a positive one internally by calling FETCHSIZE |
| 536 | 534 | before being passed to FETCH. You may disable this feature by |
| 537 | 535 | assigning a true value to the variable C<$NEGATIVE_INDICES> in the |
| 538 | 536 | tied array class. |
| 539 | 537 | |
| 540 | 538 | =end original |
| 541 | 539 | |
| 542 | 540 | 配列からの読み込みに負数の添え字が使われると、添え字は |
| 543 | 541 | FETCH に渡される前に FETCHSIZE を呼び出すことで正の数に変換されます。 |
| 544 | 542 | tie された配列クラスの C<$NEGATIVE_INDICES> に真の値を代入することで |
| 545 | 543 | この機能を無効にできます。 |
| 546 | 544 | |
| 547 | 545 | =begin original |
| 548 | 546 | |
| 549 | 547 | As you may have noticed, the name of the FETCH method (et al.) is the same |
| 550 | 548 | for all accesses, even though the constructors differ in names (TIESCALAR |
| 551 | 549 | vs TIEARRAY). While in theory you could have the same class servicing |
| 552 | 550 | several tied types, in practice this becomes cumbersome, and it's easiest |
| 553 | 551 | to keep them at simply one tie type per class. |
| 554 | 552 | |
| 555 | 553 | =end original |
| 556 | 554 | |
| 557 | 555 | すでに気がついたかもしれませんが、FETCH メソッド(など)の名前は全ての |
| 558 | 556 | アクセスについて、たとえコンストラクタが別の名前であった |
| 559 | 557 | (TIESCALAR と TIEARRAY)としても同じ名前になっています。 |
| 560 | 558 | 理論的には、幾つかの tie されたクラスをサービスする同じクラスを |
| 561 | 559 | 持つこともできるでしょうが、実際にはこれは厄介なものになり、 |
| 562 | 560 | 単にクラスあたり一つの状態にするのが最も簡単です。 |
| 563 | 561 | |
| 564 | 562 | =item STORE this, index, value |
| 565 | 563 | X<STORE> |
| 566 | 564 | |
| 567 | 565 | =begin original |
| 568 | 566 | |
| 569 | 567 | This method will be triggered every time an element in the tied array is set |
| 570 | 568 | (written). It takes two arguments beyond its self reference: the index at |
| 571 | 569 | which we're trying to store something and the value we're trying to put |
| 572 | 570 | there. |
| 573 | 571 | |
| 574 | 572 | =end original |
| 575 | 573 | |
| 576 | 574 | このメソッドは、tie された配列にある要素に対する書き込みがある度毎に |
| 577 | 575 | 起動されます。 |
| 578 | 576 | これは自分の参照のほかに、何かを格納しようとする場所の添え字と、 |
| 579 | 577 | 格納しようとしている値という二つの引数を取ります。 |
| 580 | 578 | |
| 581 | 579 | =begin original |
| 582 | 580 | |
| 583 | 581 | In our example, C<undef> is really C<$self-E<gt>{ELEMSIZE}> number of |
| 584 | 582 | spaces so we have a little more work to do here: |
| 585 | 583 | |
| 586 | 584 | =end original |
| 587 | 585 | |
| 588 | 586 | この例では、C<undef> は実際は C<$self-E<gt>{ELEMSIZE}> 個の空白なので、 |
| 589 | 587 | ここでもう少し作業が必要です: |
| 590 | 588 | |
| 591 | | |
| 589 | sub STORE { | |
| 592 | | |
| 590 | my $self = shift; | |
| 593 | | |
| 591 | my( $index, $value ) = @_; | |
| 594 | | |
| 592 | if ( length $value > $self->{ELEMSIZE} ) { | |
| 595 | | |
| 593 | croak "length of $value is greater than $self->{ELEMSIZE}"; | |
| 596 | | |
| 594 | } | |
| 597 | | |
| 595 | # fill in the blanks | |
| 598 | | |
| 596 | $self->EXTEND( $index ) if $index > $self->FETCHSIZE(); | |
| 599 | | |
| 597 | # right justify to keep element size for smaller elements | |
| 600 | | |
| 598 | $self->{ARRAY}->[$index] = sprintf "%$self->{ELEMSIZE}s", $value; | |
| 601 | | |
| 599 | } | |
| 602 | 600 | |
| 603 | 601 | =begin original |
| 604 | 602 | |
| 605 | 603 | Negative indexes are treated the same as with FETCH. |
| 606 | 604 | |
| 607 | 605 | =end original |
| 608 | 606 | |
| 609 | 607 | インデックスの値が負数の場合、FETCH と同様に扱われます。 |
| 610 | 608 | |
| 611 | 609 | =item FETCHSIZE this |
| 612 | 610 | X<FETCHSIZE> |
| 613 | 611 | |
| 614 | 612 | =begin original |
| 615 | 613 | |
| 616 | 614 | Returns the total number of items in the tied array associated with |
| 617 | 615 | object I<this>. (Equivalent to C<scalar(@array)>). For example: |
| 618 | 616 | |
| 619 | 617 | =end original |
| 620 | 618 | |
| 621 | 619 | オブジェクト I<this> と結び付けられた tie された配列の合計要素数を返します。 |
| 622 | 620 | (C<scalar(@array)> と等価です)。 |
| 623 | 621 | 例えば: |
| 624 | 622 | |
| 625 | 623 | sub FETCHSIZE { |
| 626 | 624 | my $self = shift; |
| 627 | 625 | return scalar @{$self->{ARRAY}}; |
| 628 | 626 | } |
| 629 | 627 | |
| 630 | 628 | =item STORESIZE this, count |
| 631 | 629 | X<STORESIZE> |
| 632 | 630 | |
| 633 | 631 | =begin original |
| 634 | 632 | |
| 635 | 633 | Sets the total number of items in the tied array associated with |
| 636 | 634 | object I<this> to be I<count>. If this makes the array larger then |
| 637 | 635 | class's mapping of C<undef> should be returned for new positions. |
| 638 | 636 | If the array becomes smaller then entries beyond count should be |
| 639 | 637 | deleted. |
| 640 | 638 | |
| 641 | 639 | =end original |
| 642 | 640 | |
| 643 | 641 | オブジェクト I<this> に結び付けられた tie された配列のアイテムの合計数を |
| 644 | 642 | I<count> にセットします。 |
| 645 | 643 | もし配列がより大きくなるなら、新しい位置ではクラスのマッピングは |
| 646 | 644 | C<undef> を返すべきです。 |
| 647 | 645 | もし配列がより小さくなるなら、count を超えたエントリは削除されるべきです。 |
| 648 | 646 | |
| 649 | 647 | =begin original |
| 650 | 648 | |
| 651 | 649 | In our example, 'undef' is really an element containing |
| 652 | 650 | C<$self-E<gt>{ELEMSIZE}> number of spaces. Observe: |
| 653 | 651 | |
| 654 | 652 | =end original |
| 655 | 653 | |
| 656 | 654 | この例では、'undef' というのは実際には C<$self-E<gt>{ELEMSIZE}> 個の空白を |
| 657 | 655 | 含む要素です。 |
| 658 | 656 | これを見てください: |
| 659 | 657 | |
| 660 | 658 | sub STORESIZE { |
| 661 | 659 | my $self = shift; |
| 662 | 660 | my $count = shift; |
| 663 | 661 | if ( $count > $self->FETCHSIZE() ) { |
| 664 | 662 | foreach ( $count - $self->FETCHSIZE() .. $count ) { |
| 665 | 663 | $self->STORE( $_, '' ); |
| 666 | 664 | } |
| 667 | 665 | } elsif ( $count < $self->FETCHSIZE() ) { |
| 668 | 666 | foreach ( 0 .. $self->FETCHSIZE() - $count - 2 ) { |
| 669 | 667 | $self->POP(); |
| 670 | 668 | } |
| 671 | 669 | } |
| 672 | 670 | } |
| 673 | 671 | |
| 674 | 672 | =item EXTEND this, count |
| 675 | 673 | X<EXTEND> |
| 676 | 674 | |
| 677 | 675 | =begin original |
| 678 | 676 | |
| 679 | 677 | Informative call that array is likely to grow to have I<count> entries. |
| 680 | 678 | Can be used to optimize allocation. This method need do nothing. |
| 681 | 679 | |
| 682 | 680 | =end original |
| 683 | 681 | |
| 684 | 682 | 配列が、I<count> エントリに大きくなりそうだということを通知する |
| 685 | 683 | 呼び出しです。 |
| 686 | 684 | 割り当ての最適化に使えます。 |
| 687 | 685 | このメソッドで何かをしなければならないということはありません。 |
| 688 | 686 | |
| 689 | 687 | =begin original |
| 690 | 688 | |
| 691 | 689 | In our example, we want to make sure there are no blank (C<undef>) |
| 692 | 690 | entries, so C<EXTEND> will make use of C<STORESIZE> to fill elements |
| 693 | 691 | as needed: |
| 694 | 692 | |
| 695 | 693 | =end original |
| 696 | 694 | |
| 697 | 695 | 例では、空白 (C<undef>) のエントリがないことを確実にしたいので、 |
| 698 | 696 | C<EXTEND> は必要に応じて要素を埋めるために C<STORESIZE> を使います: |
| 699 | 697 | |
| 700 | 698 | sub EXTEND { |
| 701 | 699 | my $self = shift; |
| 702 | 700 | my $count = shift; |
| 703 | 701 | $self->STORESIZE( $count ); |
| 704 | 702 | } |
| 705 | 703 | |
| 706 | 704 | =item EXISTS this, key |
| 707 | 705 | X<EXISTS> |
| 708 | 706 | |
| 709 | 707 | =begin original |
| 710 | 708 | |
| 711 | 709 | Verify that the element at index I<key> exists in the tied array I<this>. |
| 712 | 710 | |
| 713 | 711 | =end original |
| 714 | 712 | |
| 715 | 713 | tie された配列 I<this> にインデックスが I<key> である要素が存在するかを |
| 716 | 714 | 検証します。 |
| 717 | 715 | |
| 718 | 716 | =begin original |
| 719 | 717 | |
| 720 | 718 | In our example, we will determine that if an element consists of |
| 721 | 719 | C<$self-E<gt>{ELEMSIZE}> spaces only, it does not exist: |
| 722 | 720 | |
| 723 | 721 | =end original |
| 724 | 722 | |
| 725 | 723 | この例では、要素が C<$self-E<gt>{ELEMSIZE}> 個の空白のみで構成されていれば、 |
| 726 | 724 | これは存在しません: |
| 727 | 725 | |
| 728 | | |
| 726 | sub EXISTS { | |
| 729 | | |
| 727 | my $self = shift; | |
| 730 | | |
| 728 | my $index = shift; | |
| 731 | | |
| 729 | return 0 if ! defined $self->{ARRAY}->[$index] || | |
| 732 | | |
| 730 | $self->{ARRAY}->[$index] eq ' ' x $self->{ELEMSIZE}; | |
| 733 | | |
| 731 | return 1; | |
| 734 | | |
| 732 | } | |
| 735 | 733 | |
| 736 | 734 | =item DELETE this, key |
| 737 | 735 | X<DELETE> |
| 738 | 736 | |
| 739 | 737 | =begin original |
| 740 | 738 | |
| 741 | 739 | Delete the element at index I<key> from the tied array I<this>. |
| 742 | 740 | |
| 743 | 741 | =end original |
| 744 | 742 | |
| 745 | 743 | インデックス I<key> の要素を tie された配列 I<this> から削除します。 |
| 746 | 744 | |
| 747 | 745 | =begin original |
| 748 | 746 | |
| 749 | 747 | In our example, a deleted item is C<$self-E<gt>{ELEMSIZE}> spaces: |
| 750 | 748 | |
| 751 | 749 | =end original |
| 752 | 750 | |
| 753 | 751 | この例では、削除された要素は C<$self-E<gt>{ELEMSIZE}> 個の空白です: |
| 754 | 752 | |
| 755 | 753 | sub DELETE { |
| 756 | 754 | my $self = shift; |
| 757 | 755 | my $index = shift; |
| 758 | 756 | return $self->STORE( $index, '' ); |
| 759 | 757 | } |
| 760 | 758 | |
| 761 | 759 | =item CLEAR this |
| 762 | 760 | X<CLEAR> |
| 763 | 761 | |
| 764 | 762 | =begin original |
| 765 | 763 | |
| 766 | 764 | Clear (remove, delete, ...) all values from the tied array associated with |
| 767 | 765 | object I<this>. For example: |
| 768 | 766 | |
| 769 | 767 | =end original |
| 770 | 768 | |
| 771 | 769 | オブジェクト I<this> に関連付けられた tie された配列から全ての値を |
| 772 | 770 | 削除します。 |
| 773 | 771 | 例えば: |
| 774 | 772 | |
| 775 | 773 | sub CLEAR { |
| 776 | 774 | my $self = shift; |
| 777 | 775 | return $self->{ARRAY} = []; |
| 778 | 776 | } |
| 779 | 777 | |
| 780 | 778 | =item PUSH this, LIST |
| 781 | 779 | X<PUSH> |
| 782 | 780 | |
| 783 | 781 | =begin original |
| 784 | 782 | |
| 785 | 783 | Append elements of I<LIST> to the array. For example: |
| 786 | 784 | |
| 787 | 785 | =end original |
| 788 | 786 | |
| 789 | 787 | I<LIST> の要素を配列に追加します。 |
| 790 | 788 | 例えば: |
| 791 | 789 | |
| 792 | 790 | sub PUSH { |
| 793 | 791 | my $self = shift; |
| 794 | 792 | my @list = @_; |
| 795 | 793 | my $last = $self->FETCHSIZE(); |
| 796 | 794 | $self->STORE( $last + $_, $list[$_] ) foreach 0 .. $#list; |
| 797 | 795 | return $self->FETCHSIZE(); |
| 798 | 796 | } |
| 799 | 797 | |
| 800 | 798 | =item POP this |
| 801 | 799 | X<POP> |
| 802 | 800 | |
| 803 | 801 | =begin original |
| 804 | 802 | |
| 805 | 803 | Remove last element of the array and return it. For example: |
| 806 | 804 | |
| 807 | 805 | =end original |
| 808 | 806 | |
| 809 | 807 | 配列の最後の要素を取り除いてそれを返します。 |
| 810 | 808 | 例えば: |
| 811 | 809 | |
| 812 | 810 | sub POP { |
| 813 | 811 | my $self = shift; |
| 814 | 812 | return pop @{$self->{ARRAY}}; |
| 815 | 813 | } |
| 816 | 814 | |
| 817 | 815 | =item SHIFT this |
| 818 | 816 | X<SHIFT> |
| 819 | 817 | |
| 820 | 818 | =begin original |
| 821 | 819 | |
| 822 | 820 | Remove the first element of the array (shifting other elements down) |
| 823 | 821 | and return it. For example: |
| 824 | 822 | |
| 825 | 823 | =end original |
| 826 | 824 | |
| 827 | 825 | 配列の最初の要素を取り除いて(残りの要素はシフトします)、その要素を返します。 |
| 828 | 826 | 例えば: |
| 829 | 827 | |
| 830 | 828 | sub SHIFT { |
| 831 | 829 | my $self = shift; |
| 832 | 830 | return shift @{$self->{ARRAY}}; |
| 833 | 831 | } |
| 834 | 832 | |
| 835 | 833 | =item UNSHIFT this, LIST |
| 836 | 834 | X<UNSHIFT> |
| 837 | 835 | |
| 838 | 836 | =begin original |
| 839 | 837 | |
| 840 | 838 | Insert LIST elements at the beginning of the array, moving existing elements |
| 841 | 839 | up to make room. For example: |
| 842 | 840 | |
| 843 | 841 | =end original |
| 844 | 842 | |
| 845 | 843 | LIST 要素を配列の先頭に挿入し、すでにある要素は場所を空けるために |
| 846 | 844 | 移動します。 |
| 847 | 845 | 例えば: |
| 848 | 846 | |
| 849 | 847 | sub UNSHIFT { |
| 850 | 848 | my $self = shift; |
| 851 | 849 | my @list = @_; |
| 852 | 850 | my $size = scalar( @list ); |
| 853 | 851 | # make room for our list |
| 854 | 852 | @{$self->{ARRAY}}[ $size .. $#{$self->{ARRAY}} + $size ] |
| 855 | 853 | = @{$self->{ARRAY}}; |
| 856 | 854 | $self->STORE( $_, $list[$_] ) foreach 0 .. $#list; |
| 857 | 855 | } |
| 858 | 856 | |
| 859 | 857 | =item SPLICE this, offset, length, LIST |
| 860 | 858 | X<SPLICE> |
| 861 | 859 | |
| 862 | 860 | =begin original |
| 863 | 861 | |
| 864 | 862 | Perform the equivalent of C<splice> on the array. |
| 865 | 863 | |
| 866 | 864 | =end original |
| 867 | 865 | |
| 868 | 866 | 配列に対する C<splice> と等価に振る舞います。 |
| 869 | 867 | |
| 870 | 868 | =begin original |
| 871 | 869 | |
| 872 | 870 | I<offset> is optional and defaults to zero, negative values count back |
| 873 | 871 | from the end of the array. |
| 874 | 872 | |
| 875 | 873 | =end original |
| 876 | 874 | |
| 877 | 875 | I<offset> はオプションでデフォルトは 0 です; 負数は配列の最後からの |
| 878 | 876 | 位置を示します。 |
| 879 | 877 | |
| 880 | 878 | =begin original |
| 881 | 879 | |
| 882 | 880 | I<length> is optional and defaults to rest of the array. |
| 883 | 881 | |
| 884 | 882 | =end original |
| 885 | 883 | |
| 886 | 884 | I<length> はオプションで、デフォルトは配列の残りです。 |
| 887 | 885 | |
| 888 | 886 | =begin original |
| 889 | 887 | |
| 890 | 888 | I<LIST> may be empty. |
| 891 | 889 | |
| 892 | 890 | =end original |
| 893 | 891 | |
| 894 | 892 | I<LIST> は空かもしれません。 |
| 895 | 893 | |
| 896 | 894 | =begin original |
| 897 | 895 | |
| 898 | 896 | Returns a list of the original I<length> elements at I<offset>. |
| 899 | 897 | |
| 900 | 898 | =end original |
| 901 | 899 | |
| 902 | 900 | 元の、I<offset> の位置から I<length> 要素分のリストを返します。 |
| 903 | 901 | |
| 904 | 902 | =begin original |
| 905 | 903 | |
| 906 | 904 | In our example, we'll use a little shortcut if there is a I<LIST>: |
| 907 | 905 | |
| 908 | 906 | =end original |
| 909 | 907 | |
| 910 | 908 | この例では、I<LIST> がある場合は少し近道をします: |
| 911 | 909 | |
| 912 | 910 | sub SPLICE { |
| 913 | 911 | my $self = shift; |
| 914 | 912 | my $offset = shift || 0; |
| 915 | 913 | my $length = shift || $self->FETCHSIZE() - $offset; |
| 916 | 914 | my @list = (); |
| 917 | 915 | if ( @_ ) { |
| 918 | 916 | tie @list, __PACKAGE__, $self->{ELEMSIZE}; |
| 919 | 917 | @list = @_; |
| 920 | 918 | } |
| 921 | 919 | return splice @{$self->{ARRAY}}, $offset, $length, @list; |
| 922 | 920 | } |
| 923 | 921 | |
| 924 | 922 | =item UNTIE this |
| 925 | 923 | X<UNTIE> |
| 926 | 924 | |
| 927 | 925 | =begin original |
| 928 | 926 | |
| 929 | 927 | Will be called when C<untie> happens. (See L<The C<untie> Gotcha> below.) |
| 930 | 928 | |
| 931 | 929 | =end original |
| 932 | 930 | |
| 933 | 931 | C<untie> が起きると呼び出されます。 |
| 934 | 932 | (後述する L<The C<untie> Gotcha> を参照してください。) |
| 935 | 933 | |
| 936 | 934 | =item DESTROY this |
| 937 | 935 | X<DESTROY> |
| 938 | 936 | |
| 939 | 937 | =begin original |
| 940 | 938 | |
| 941 | 939 | This method will be triggered when the tied variable needs to be destructed. |
| 942 | 940 | As with the scalar tie class, this is almost never needed in a |
| 943 | 941 | language that does its own garbage collection, so this time we'll |
| 944 | 942 | just leave it out. |
| 945 | 943 | |
| 946 | 944 | =end original |
| 947 | 945 | |
| 948 | 946 | このメソッドは tie された変数を破棄する必要があるときに呼び出されます。 |
| 949 | 947 | スカラを tie したクラスと同様、このメソッドはガベージコレクションを |
| 950 | 言語自体が行っているのでほとんど必要ありません | |
| 948 | 言語自体が行っているのでほとんど必要ありません; ですから、今回はこのまま | |
| 951 | ||
| 949 | 放っておきます。 | |
| 952 | 950 | |
| 953 | 951 | =back |
| 954 | 952 | |
| 955 | 953 | =head2 Tying Hashes |
| 956 | 954 | X<hash, tying> |
| 957 | 955 | |
| 958 | 956 | (ハッシュを tie する) |
| 959 | 957 | |
| 960 | 958 | =begin original |
| 961 | 959 | |
| 962 | 960 | Hashes were the first Perl data type to be tied (see dbmopen()). A class |
| 963 | 961 | implementing a tied hash should define the following methods: TIEHASH is |
| 964 | 962 | the constructor. FETCH and STORE access the key and value pairs. EXISTS |
| 965 | 963 | reports whether a key is present in the hash, and DELETE deletes one. |
| 966 | 964 | CLEAR empties the hash by deleting all the key and value pairs. FIRSTKEY |
| 967 | 965 | and NEXTKEY implement the keys() and each() functions to iterate over all |
| 968 | 966 | the keys. SCALAR is triggered when the tied hash is evaluated in scalar |
| 969 | context | |
| 967 | context, and in 5.28 onwards, by C<keys> in boolean context. UNTIE is | |
| 970 | ||
| 968 | called when C<untie> happens, and DESTROY is called when the tied variable | |
| 969 | is garbage collected. | |
| 971 | 970 | |
| 972 | 971 | =end original |
| 973 | 972 | |
| 974 | 973 | ハッシュは tie される最初の Perl データ型でした(dbmopen() を参照)。 |
| 975 | tie されたハッシュを実装するクラスは、以下のメソッドを定義すべきです | |
| 974 | tie されたハッシュを実装するクラスは、以下のメソッドを定義すべきです: | |
| 976 | 975 | TIEHASH はコンストラクタです。 |
| 977 | 976 | FETCH と STORE はキーと値のペアにアクセスします。 |
| 978 | 977 | EXIST はキーがハッシュにあるかどうかを報告し、DELETE はキーを削除します。 |
| 979 | 978 | CLEAR はすべてのキーと値のペアを削除することによりハッシュを空にします。 |
| 980 | 979 | FIRSTKEY と NEXTKEY は全てのキーを反復するための関数 keys() と each() を |
| 981 | 980 | 実装します。 |
| 982 | SCALAR は tie されたハッシュがスカラコンテキストで評価されたとき | |
| 981 | SCALAR は tie されたハッシュがスカラコンテキストで評価されたとき、 | |
| 982 | また 5.28 以降では、真偽値コンテキストで C<keys> が評価されたときに | |
| 983 | 983 | 呼び出されます。 |
| 984 | 984 | UNTIE は C<untie> が起きたときに呼び出され、DESTROY は tie された変数が |
| 985 | 985 | ガーベジコレクションされるときに呼び出されます。 |
| 986 | 986 | |
| 987 | 987 | =begin original |
| 988 | 988 | |
| 989 | 989 | If this seems like a lot, then feel free to inherit from merely the |
| 990 | 990 | standard Tie::StdHash module for most of your methods, redefining only the |
| 991 | 991 | interesting ones. See L<Tie::Hash> for details. |
| 992 | 992 | |
| 993 | 993 | =end original |
| 994 | 994 | |
| 995 | 995 | もしこれがたくさんありすぎると感じられるのなら、標準の Tie::StdHash |
| 996 | 996 | モジュールを単純に継承し、再定義を必要とするものだけを自分で |
| 997 | 997 | 実装することもできます。 |
| 998 | 998 | 詳しくは L<Tie::Hash> を参照してください。 |
| 999 | 999 | |
| 1000 | 1000 | =begin original |
| 1001 | 1001 | |
| 1002 | 1002 | Remember that Perl distinguishes between a key not existing in the hash, |
| 1003 | 1003 | and the key existing in the hash but having a corresponding value of |
| 1004 | 1004 | C<undef>. The two possibilities can be tested with the C<exists()> and |
| 1005 | 1005 | C<defined()> functions. |
| 1006 | 1006 | |
| 1007 | 1007 | =end original |
| 1008 | 1008 | |
| 1009 | 1009 | Perl がハッシュに存在していないキーと、ハッシュに存在しているけれども |
| 1010 | 1010 | C<undef> という値を持っているキーとを明確に区別しているということを |
| 1011 | 1011 | 忘れないでください。 |
| 1012 | 1012 | これら二つの可能性は、C<exists()> と |
| 1013 | 1013 | C<defined()> という関数を使って検査できます。 |
| 1014 | 1014 | |
| 1015 | 1015 | =begin original |
| 1016 | 1016 | |
| 1017 | 1017 | Here's an example of a somewhat interesting tied hash class: it gives you |
| 1018 | 1018 | a hash representing a particular user's dot files. You index into the hash |
| 1019 | 1019 | with the name of the file (minus the dot) and you get back that dot file's |
| 1020 | 1020 | contents. For example: |
| 1021 | 1021 | |
| 1022 | 1022 | =end original |
| 1023 | 1023 | |
| 1024 | 次の例は tie されたハッシュクラスを使ったものです | |
| 1024 | 次の例は tie されたハッシュクラスを使ったものです: この例では特定の | |
| 1025 | ||
| 1025 | ユーザーのドットファイルを表わすハッシュを提供します。 | |
| 1026 | 1026 | あなたはハッシュをファイルの名前(からドットを取り除いたもの)によって |
| 1027 | 1027 | 添え字付けを行い、そのドットファイルの内容を取得します。 |
| 1028 | 1028 | 例えば: |
| 1029 | 1029 | |
| 1030 | 1030 | use DotFiles; |
| 1031 | 1031 | tie %dot, 'DotFiles'; |
| 1032 | 1032 | if ( $dot{profile} =~ /MANPATH/ || |
| 1033 | 1033 | $dot{login} =~ /MANPATH/ || |
| 1034 | 1034 | $dot{cshrc} =~ /MANPATH/ ) |
| 1035 | 1035 | { |
| 1036 | 1036 | print "you seem to set your MANPATH\n"; |
| 1037 | 1037 | } |
| 1038 | 1038 | |
| 1039 | 1039 | =begin original |
| 1040 | 1040 | |
| 1041 | 1041 | Or here's another sample of using our tied class: |
| 1042 | 1042 | |
| 1043 | 1043 | =end original |
| 1044 | 1044 | |
| 1045 | 1045 | tie されたクラスを使ったもう一つの例です。 |
| 1046 | 1046 | |
| 1047 | 1047 | tie %him, 'DotFiles', 'daemon'; |
| 1048 | 1048 | foreach $f ( keys %him ) { |
| 1049 | 1049 | printf "daemon dot file %s is size %d\n", |
| 1050 | 1050 | $f, length $him{$f}; |
| 1051 | 1051 | } |
| 1052 | 1052 | |
| 1053 | 1053 | =begin original |
| 1054 | 1054 | |
| 1055 | 1055 | In our tied hash DotFiles example, we use a regular |
| 1056 | 1056 | hash for the object containing several important |
| 1057 | 1057 | fields, of which only the C<{LIST}> field will be what the |
| 1058 | 1058 | user thinks of as the real hash. |
| 1059 | 1059 | |
| 1060 | 1060 | =end original |
| 1061 | 1061 | |
| 1062 | 1062 | この DotFiles という tie されたハッシュでは、私たちは C<{LIST}> |
| 1063 | 1063 | フィールドのみをユーザーが本当のハッシュであると考えるであろう幾つかの |
| 1064 | 1064 | 重要なフィールドを持ったオブジェクトのために、通常のハッシュを |
| 1065 | 1065 | 使いました。 |
| 1066 | 1066 | |
| 1067 | 1067 | =over 5 |
| 1068 | 1068 | |
| 1069 | 1069 | =item USER |
| 1070 | 1070 | |
| 1071 | 1071 | =begin original |
| 1072 | 1072 | |
| 1073 | 1073 | whose dot files this object represents |
| 1074 | 1074 | |
| 1075 | 1075 | =end original |
| 1076 | 1076 | |
| 1077 | 1077 | このオブジェクトが表わしているドットファイルの所有者 |
| 1078 | 1078 | |
| 1079 | 1079 | =item HOME |
| 1080 | 1080 | |
| 1081 | 1081 | =begin original |
| 1082 | 1082 | |
| 1083 | 1083 | where those dot files live |
| 1084 | 1084 | |
| 1085 | 1085 | =end original |
| 1086 | 1086 | |
| 1087 | 1087 | ドットファイルがある場所 |
| 1088 | 1088 | |
| 1089 | 1089 | =item CLOBBER |
| 1090 | 1090 | |
| 1091 | 1091 | =begin original |
| 1092 | 1092 | |
| 1093 | 1093 | whether we should try to change or remove those dot files |
| 1094 | 1094 | |
| 1095 | 1095 | =end original |
| 1096 | 1096 | |
| 1097 | 1097 | これらのドットファイルを変更したり削除することをしようとすべきか |
| 1098 | 1098 | を表わすフラグ |
| 1099 | 1099 | |
| 1100 | 1100 | =item LIST |
| 1101 | 1101 | |
| 1102 | 1102 | =begin original |
| 1103 | 1103 | |
| 1104 | 1104 | the hash of dot file names and content mappings |
| 1105 | 1105 | |
| 1106 | 1106 | =end original |
| 1107 | 1107 | |
| 1108 | 1108 | ドットファイルの名前と内容のマッピングをしたハッシュ |
| 1109 | 1109 | |
| 1110 | 1110 | =back |
| 1111 | 1111 | |
| 1112 | 1112 | =begin original |
| 1113 | 1113 | |
| 1114 | 1114 | Here's the start of F<Dotfiles.pm>: |
| 1115 | 1115 | |
| 1116 | 1116 | =end original |
| 1117 | 1117 | |
| 1118 | 1118 | 次は F<Dotfiles.pm> の先頭です: |
| 1119 | 1119 | |
| 1120 | 1120 | package DotFiles; |
| 1121 | 1121 | use Carp; |
| 1122 | 1122 | sub whowasi { (caller(1))[3] . '()' } |
| 1123 | 1123 | my $DEBUG = 0; |
| 1124 | 1124 | sub debug { $DEBUG = @_ ? shift : 1 } |
| 1125 | 1125 | |
| 1126 | 1126 | =begin original |
| 1127 | 1127 | |
| 1128 | 1128 | For our example, we want to be able to emit debugging info to help in tracing |
| 1129 | 1129 | during development. We keep also one convenience function around |
| 1130 | 1130 | internally to help print out warnings; whowasi() returns the function name |
| 1131 | 1131 | that calls it. |
| 1132 | 1132 | |
| 1133 | 1133 | =end original |
| 1134 | 1134 | |
| 1135 | 1135 | この例では、私たちは開発の間トレースがしやすいようにデバッグ情報を |
| 1136 | 1136 | 出力できるようにしたいと考えました。 |
| 1137 | 同様に、警告を出力するのを助ける一つの便利な内部関数を残しました | |
| 1137 | 同様に、警告を出力するのを助ける一つの便利な内部関数を残しました; | |
| 1138 | 1138 | whowasi() は呼び出した関数の名前を返します。 |
| 1139 | 1139 | |
| 1140 | 1140 | =begin original |
| 1141 | 1141 | |
| 1142 | 1142 | Here are the methods for the DotFiles tied hash. |
| 1143 | 1143 | |
| 1144 | 1144 | =end original |
| 1145 | 1145 | |
| 1146 | 1146 | 以下は、DotoFiles に tie されたハッシュのためのメソッドです。 |
| 1147 | 1147 | |
| 1148 | 1148 | =over 4 |
| 1149 | 1149 | |
| 1150 | 1150 | =item TIEHASH classname, LIST |
| 1151 | 1151 | X<TIEHASH> |
| 1152 | 1152 | |
| 1153 | 1153 | =begin original |
| 1154 | 1154 | |
| 1155 | 1155 | This is the constructor for the class. That means it is expected to |
| 1156 | 1156 | return a blessed reference through which the new object (probably but not |
| 1157 | 1157 | necessarily an anonymous hash) will be accessed. |
| 1158 | 1158 | |
| 1159 | 1159 | =end original |
| 1160 | 1160 | |
| 1161 | これはクラス | |
| 1161 | これはこのクラスのコンストラクタです。 | |
| 1162 | 1162 | その役割は、アクセスされる(おそらくは無名のハッシュ、 |
| 1163 | 1163 | ただしそうする必要はない)オブジェクトへの bless された参照を返すことです。 |
| 1164 | 1164 | |
| 1165 | 1165 | =begin original |
| 1166 | 1166 | |
| 1167 | 1167 | Here's the constructor: |
| 1168 | 1168 | |
| 1169 | 1169 | =end original |
| 1170 | 1170 | |
| 1171 | 1171 | コンストラクタの例です。 |
| 1172 | 1172 | |
| 1173 | 1173 | sub TIEHASH { |
| 1174 | 1174 | my $self = shift; |
| 1175 | 1175 | my $user = shift || $>; |
| 1176 | 1176 | my $dotdir = shift || ''; |
| 1177 | 1177 | croak "usage: @{[&whowasi]} [USER [DOTDIR]]" if @_; |
| 1178 | 1178 | $user = getpwuid($user) if $user =~ /^\d+$/; |
| 1179 | 1179 | my $dir = (getpwnam($user))[7] |
| 1180 | 1180 | || croak "@{[&whowasi]}: no user $user"; |
| 1181 | 1181 | $dir .= "/$dotdir" if $dotdir; |
| 1182 | 1182 | |
| 1183 | 1183 | my $node = { |
| 1184 | 1184 | USER => $user, |
| 1185 | 1185 | HOME => $dir, |
| 1186 | 1186 | LIST => {}, |
| 1187 | 1187 | CLOBBER => 0, |
| 1188 | 1188 | }; |
| 1189 | 1189 | |
| 1190 | 1190 | opendir(DIR, $dir) |
| 1191 | 1191 | || croak "@{[&whowasi]}: can't opendir $dir: $!"; |
| 1192 | 1192 | foreach $dot ( grep /^\./ && -f "$dir/$_", readdir(DIR)) { |
| 1193 | 1193 | $dot =~ s/^\.//; |
| 1194 | 1194 | $node->{LIST}{$dot} = undef; |
| 1195 | 1195 | } |
| 1196 | 1196 | closedir DIR; |
| 1197 | 1197 | return bless $node, $self; |
| 1198 | 1198 | } |
| 1199 | 1199 | |
| 1200 | 1200 | =begin original |
| 1201 | 1201 | |
| 1202 | 1202 | It's probably worth mentioning that if you're going to filetest the |
| 1203 | 1203 | return values out of a readdir, you'd better prepend the directory |
| 1204 | 1204 | in question. Otherwise, because we didn't chdir() there, it would |
| 1205 | 1205 | have been testing the wrong file. |
| 1206 | 1206 | |
| 1207 | 1207 | =end original |
| 1208 | 1208 | |
| 1209 | 1209 | readdir が返した値をつかってファイルテストをしようという場合、 |
| 1210 | 1210 | 問い合わせにディレクトリを付加すべきでしょう。 |
| 1211 | 1211 | そうしなければ、chdir() をしていないので間違ったファイルを |
| 1212 | 1212 | テストしてしまうこととなります。 |
| 1213 | 1213 | |
| 1214 | 1214 | =item FETCH this, key |
| 1215 | 1215 | X<FETCH> |
| 1216 | 1216 | |
| 1217 | 1217 | =begin original |
| 1218 | 1218 | |
| 1219 | 1219 | This method will be triggered every time an element in the tied hash is |
| 1220 | 1220 | accessed (read). It takes one argument beyond its self reference: the key |
| 1221 | 1221 | whose value we're trying to fetch. |
| 1222 | 1222 | |
| 1223 | 1223 | =end original |
| 1224 | 1224 | |
| 1225 | 1225 | このメソッドは tie されたハッシュがアクセス(読み出し)される度毎に |
| 1226 | 1226 | 呼び出されます。 |
| 1227 | 1227 | これは自分の参照のほかに、フェッチしようとしている値に対するキーを、 |
| 1228 | 1228 | ただ一つの引数としてとります。 |
| 1229 | 1229 | |
| 1230 | 1230 | =begin original |
| 1231 | 1231 | |
| 1232 | 1232 | Here's the fetch for our DotFiles example. |
| 1233 | 1233 | |
| 1234 | 1234 | =end original |
| 1235 | 1235 | |
| 1236 | 1236 | 以下に示すのは、私たちの DotFiles サンプルのためのフェッチです。 |
| 1237 | 1237 | |
| 1238 | 1238 | sub FETCH { |
| 1239 | 1239 | carp &whowasi if $DEBUG; |
| 1240 | 1240 | my $self = shift; |
| 1241 | 1241 | my $dot = shift; |
| 1242 | 1242 | my $dir = $self->{HOME}; |
| 1243 | 1243 | my $file = "$dir/.$dot"; |
| 1244 | 1244 | |
| 1245 | 1245 | unless (exists $self->{LIST}->{$dot} || -f $file) { |
| 1246 | 1246 | carp "@{[&whowasi]}: no $dot file" if $DEBUG; |
| 1247 | 1247 | return undef; |
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | 1250 | if (defined $self->{LIST}->{$dot}) { |
| 1251 | 1251 | return $self->{LIST}->{$dot}; |
| 1252 | 1252 | } else { |
| 1253 | 1253 | return $self->{LIST}->{$dot} = `cat $dir/.$dot`; |
| 1254 | 1254 | } |
| 1255 | 1255 | } |
| 1256 | 1256 | |
| 1257 | 1257 | =begin original |
| 1258 | 1258 | |
| 1259 | 1259 | It was easy to write by having it call the Unix cat(1) command, but it |
| 1260 | 1260 | would probably be more portable to open the file manually (and somewhat |
| 1261 | 1261 | more efficient). Of course, because dot files are a Unixy concept, we're |
| 1262 | 1262 | not that concerned. |
| 1263 | 1263 | |
| 1264 | 1264 | =end original |
| 1265 | 1265 | |
| 1266 | 1266 | UNIX の cat(1) コマンドを呼んでいるので記述するのは簡単でしたが、 |
| 1267 | 1267 | ファイルを自分でオープンすることによってよりポータブル(かつ、より高効率)に |
| 1268 | 1268 | できます。 |
| 1269 | 1269 | もちろん、ドットファイルは UNIX 的なコンセプトですから、 |
| 1270 | 1270 | 私たちは気にしませんでした。 |
| 1271 | 1271 | |
| 1272 | 1272 | =item STORE this, key, value |
| 1273 | 1273 | X<STORE> |
| 1274 | 1274 | |
| 1275 | 1275 | =begin original |
| 1276 | 1276 | |
| 1277 | 1277 | This method will be triggered every time an element in the tied hash is set |
| 1278 | 1278 | (written). It takes two arguments beyond its self reference: the index at |
| 1279 | 1279 | which we're trying to store something, and the value we're trying to put |
| 1280 | 1280 | there. |
| 1281 | 1281 | |
| 1282 | 1282 | =end original |
| 1283 | 1283 | |
| 1284 | 1284 | このメソッドは tie されたハッシュの要素がセット(書き込み)される度に |
| 1285 | 1285 | 呼び出されます。 |
| 1286 | 1286 | これは自分の参照の他に二つの引数、何かを格納しようとする場所の添え字と、 |
| 1287 | 1287 | 格納しようとする値をとります。 |
| 1288 | 1288 | |
| 1289 | 1289 | =begin original |
| 1290 | 1290 | |
| 1291 | 1291 | Here in our DotFiles example, we'll be careful not to let |
| 1292 | 1292 | them try to overwrite the file unless they've called the clobber() |
| 1293 | 1293 | method on the original object reference returned by tie(). |
| 1294 | 1294 | |
| 1295 | 1295 | =end original |
| 1296 | 1296 | |
| 1297 | 以下は DotFiles のサンプルです | |
| 1297 | 以下は DotFiles のサンプルです; tie() で返されたオブジェクトの | |
| 1298 | ||
| 1298 | リファレンス上で clobber() メソッドが呼び出されない限り、ファイルを | |
| 1299 | ||
| 1299 | 上書きしないようにしています。 | |
| 1300 | 1300 | |
| 1301 | 1301 | sub STORE { |
| 1302 | 1302 | carp &whowasi if $DEBUG; |
| 1303 | 1303 | my $self = shift; |
| 1304 | 1304 | my $dot = shift; |
| 1305 | 1305 | my $value = shift; |
| 1306 | 1306 | my $file = $self->{HOME} . "/.$dot"; |
| 1307 | 1307 | my $user = $self->{USER}; |
| 1308 | 1308 | |
| 1309 | 1309 | croak "@{[&whowasi]}: $file not clobberable" |
| 1310 | 1310 | unless $self->{CLOBBER}; |
| 1311 | 1311 | |
| 1312 | open( | |
| 1312 | open(my $f, '>', $file) || croak "can't open $file: $!"; | |
| 1313 | print | |
| 1313 | print $f $value; | |
| 1314 | close( | |
| 1314 | close($f); | |
| 1315 | 1315 | } |
| 1316 | 1316 | |
| 1317 | 1317 | =begin original |
| 1318 | 1318 | |
| 1319 | 1319 | If they wanted to clobber something, they might say: |
| 1320 | 1320 | |
| 1321 | 1321 | =end original |
| 1322 | 1322 | |
| 1323 | 1323 | もし何かを変更したいというのであれば、このようにします。 |
| 1324 | 1324 | |
| 1325 | 1325 | $ob = tie %daemon_dots, 'daemon'; |
| 1326 | 1326 | $ob->clobber(1); |
| 1327 | 1327 | $daemon_dots{signature} = "A true daemon\n"; |
| 1328 | 1328 | |
| 1329 | 1329 | =begin original |
| 1330 | 1330 | |
| 1331 | 1331 | Another way to lay hands on a reference to the underlying object is to |
| 1332 | 1332 | use the tied() function, so they might alternately have set clobber |
| 1333 | 1333 | using: |
| 1334 | 1334 | |
| 1335 | 1335 | =end original |
| 1336 | 1336 | |
| 1337 | 1337 | 基礎をなすオブジェクトへの参照を扱うもう一つの方法は tied() 関数を |
| 1338 | 1338 | 使うことで、これによって clobber を以下の様に使ってセットできます。 |
| 1339 | 1339 | |
| 1340 | 1340 | tie %daemon_dots, 'daemon'; |
| 1341 | 1341 | tied(%daemon_dots)->clobber(1); |
| 1342 | 1342 | |
| 1343 | 1343 | =begin original |
| 1344 | 1344 | |
| 1345 | 1345 | The clobber method is simply: |
| 1346 | 1346 | |
| 1347 | 1347 | =end original |
| 1348 | 1348 | |
| 1349 | 1349 | clobber メソッドは単純です。 |
| 1350 | 1350 | |
| 1351 | 1351 | sub clobber { |
| 1352 | 1352 | my $self = shift; |
| 1353 | 1353 | $self->{CLOBBER} = @_ ? shift : 1; |
| 1354 | 1354 | } |
| 1355 | 1355 | |
| 1356 | 1356 | =item DELETE this, key |
| 1357 | 1357 | X<DELETE> |
| 1358 | 1358 | |
| 1359 | 1359 | =begin original |
| 1360 | 1360 | |
| 1361 | 1361 | This method is triggered when we remove an element from the hash, |
| 1362 | 1362 | typically by using the delete() function. Again, we'll |
| 1363 | 1363 | be careful to check whether they really want to clobber files. |
| 1364 | 1364 | |
| 1365 | 1365 | =end original |
| 1366 | 1366 | |
| 1367 | 1367 | このメソッドはハッシュから要素を取り除くとき、典型的には delete() 関数を |
| 1368 | 1368 | 使ったときに呼び出されます。 |
| 1369 | 1369 | 繰り返しますが、本当にファイルを clobber したいのかを注意深く検査しています。 |
| 1370 | 1370 | |
| 1371 | | |
| 1371 | sub DELETE { | |
| 1372 | ||
| 1372 | carp &whowasi if $DEBUG; | |
| 1373 | 1373 | |
| 1374 | ||
| 1374 | my $self = shift; | |
| 1375 | ||
| 1375 | my $dot = shift; | |
| 1376 | ||
| 1376 | my $file = $self->{HOME} . "/.$dot"; | |
| 1377 | ||
| 1377 | croak "@{[&whowasi]}: won't remove file $file" | |
| 1378 | ||
| 1378 | unless $self->{CLOBBER}; | |
| 1379 | ||
| 1379 | delete $self->{LIST}->{$dot}; | |
| 1380 | ||
| 1380 | my $success = unlink($file); | |
| 1381 | ||
| 1381 | carp "@{[&whowasi]}: can't unlink $file: $!" unless $success; | |
| 1382 | ||
| 1382 | $success; | |
| 1383 | | |
| 1383 | } | |
| 1384 | 1384 | |
| 1385 | 1385 | =begin original |
| 1386 | 1386 | |
| 1387 | 1387 | The value returned by DELETE becomes the return value of the call |
| 1388 | 1388 | to delete(). If you want to emulate the normal behavior of delete(), |
| 1389 | 1389 | you should return whatever FETCH would have returned for this key. |
| 1390 | 1390 | In this example, we have chosen instead to return a value which tells |
| 1391 | 1391 | the caller whether the file was successfully deleted. |
| 1392 | 1392 | |
| 1393 | 1393 | =end original |
| 1394 | 1394 | |
| 1395 | 1395 | DELETE の 返す値は delete() の戻り値から来ています。 |
| 1396 | 1396 | もしあなたが通常の delete() の動作をまねしたいというのであれば、 |
| 1397 | 1397 | FETCH がこのキーに対して返すであろう値を返すべきでしょう。 |
| 1398 | 1398 | この例では、戻り値としてファイルの削除に成功したかどうかを |
| 1399 | 1399 | 返すことを選択しました。 |
| 1400 | 1400 | |
| 1401 | 1401 | =item CLEAR this |
| 1402 | 1402 | X<CLEAR> |
| 1403 | 1403 | |
| 1404 | 1404 | =begin original |
| 1405 | 1405 | |
| 1406 | 1406 | This method is triggered when the whole hash is to be cleared, usually by |
| 1407 | 1407 | assigning the empty list to it. |
| 1408 | 1408 | |
| 1409 | 1409 | =end original |
| 1410 | 1410 | |
| 1411 | 1411 | このメソッドはハッシュ全体が消去されるとき、通常は空リストが代入されたときに |
| 1412 | 1412 | 呼び出されます。 |
| 1413 | 1413 | |
| 1414 | 1414 | =begin original |
| 1415 | 1415 | |
| 1416 | 1416 | In our example, that would remove all the user's dot files! It's such a |
| 1417 | 1417 | dangerous thing that they'll have to set CLOBBER to something higher than |
| 1418 | 1418 | 1 to make it happen. |
| 1419 | 1419 | |
| 1420 | 1420 | =end original |
| 1421 | 1421 | |
| 1422 | 1422 | 私たちの例では、これはユーザーのすべてのドットファイルを |
| 1423 | 1423 | 削除してしまいます! |
| 1424 | 1424 | これはとても危険なことで、実際に削除するには CLOBBER に 1 を超える値を |
| 1425 | 1425 | セットすることが必要となります。 |
| 1426 | 1426 | |
| 1427 | | |
| 1427 | sub CLEAR { | |
| 1428 | ||
| 1428 | carp &whowasi if $DEBUG; | |
| 1429 | ||
| 1429 | my $self = shift; | |
| 1430 | ||
| 1430 | croak "@{[&whowasi]}: won't remove all dot files for $self->{USER}" | |
| 1431 | ||
| 1431 | unless $self->{CLOBBER} > 1; | |
| 1432 | ||
| 1432 | my $dot; | |
| 1433 | ||
| 1433 | foreach $dot ( keys %{$self->{LIST}}) { | |
| 1434 | ||
| 1434 | $self->DELETE($dot); | |
| 1435 | ||
| 1435 | } | |
| 1436 | | |
| 1436 | } | |
| 1437 | 1437 | |
| 1438 | 1438 | =item EXISTS this, key |
| 1439 | 1439 | X<EXISTS> |
| 1440 | 1440 | |
| 1441 | 1441 | =begin original |
| 1442 | 1442 | |
| 1443 | 1443 | This method is triggered when the user uses the exists() function |
| 1444 | 1444 | on a particular hash. In our example, we'll look at the C<{LIST}> |
| 1445 | 1445 | hash element for this: |
| 1446 | 1446 | |
| 1447 | 1447 | =end original |
| 1448 | 1448 | |
| 1449 | 1449 | このメソッドは特定のハッシュにおいて、exists() 関数が使われたときに |
| 1450 | 1450 | 呼び出されます。 |
| 1451 | 1451 | 私たちの例では、このためにハッシュ要素 C<{LIST}> を参照します。 |
| 1452 | 1452 | |
| 1453 | 1453 | sub EXISTS { |
| 1454 | 1454 | carp &whowasi if $DEBUG; |
| 1455 | 1455 | my $self = shift; |
| 1456 | 1456 | my $dot = shift; |
| 1457 | 1457 | return exists $self->{LIST}->{$dot}; |
| 1458 | 1458 | } |
| 1459 | 1459 | |
| 1460 | 1460 | =item FIRSTKEY this |
| 1461 | 1461 | X<FIRSTKEY> |
| 1462 | 1462 | |
| 1463 | 1463 | =begin original |
| 1464 | 1464 | |
| 1465 | 1465 | This method will be triggered when the user is going |
| 1466 | to iterate through the hash, such as via a keys() or each() | |
| 1466 | to iterate through the hash, such as via a keys(), values(), or each() call. | |
| 1467 | call. | |
| 1468 | 1467 | |
| 1469 | 1468 | =end original |
| 1470 | 1469 | |
| 1471 | このメソッドは keys() | |
| 1470 | このメソッドは keys(), values(), each() を呼び出すのと同様に、ハッシュを | |
| 1472 | 反復をユーザーが行おうとするときに呼び出されます。 | |
| 1471 | 通じた反復をユーザーが行おうとするときに呼び出されます。 | |
| 1473 | 1472 | |
| 1474 | 1473 | sub FIRSTKEY { |
| 1475 | 1474 | carp &whowasi if $DEBUG; |
| 1476 | 1475 | my $self = shift; |
| 1477 | my $a = keys %{$self->{LIST}}; | |
| 1476 | my $a = keys %{$self->{LIST}}; # reset each() iterator | |
| 1478 | 1477 | each %{$self->{LIST}} |
| 1479 | 1478 | } |
| 1480 | 1479 | |
| 1480 | =begin original | |
| 1481 | ||
| 1482 | FIRSTKEY is always called in scalar context and it should just | |
| 1483 | return the first key. values(), and each() in list context, | |
| 1484 | will call FETCH for the returned keys. | |
| 1485 | ||
| 1486 | =end original | |
| 1487 | ||
| 1488 | FIRSTKEY は常にスカラコンテキストで呼び出され、単に最初のキーを | |
| 1489 | 返すべきです。 | |
| 1490 | values() と each() はリストコンテキストでは返された値に対して FETCH を | |
| 1491 | 呼び出します。 | |
| 1492 | ||
| 1481 | 1493 | =item NEXTKEY this, lastkey |
| 1482 | 1494 | X<NEXTKEY> |
| 1483 | 1495 | |
| 1484 | 1496 | =begin original |
| 1485 | 1497 | |
| 1486 | This method gets triggered during a keys() or each() iteration. It has a | |
| 1498 | This method gets triggered during a keys(), values(), or each() iteration. It has a | |
| 1487 | 1499 | second argument which is the last key that had been accessed. This is |
| 1488 | useful if you're car | |
| 1500 | useful if you're caring about ordering or calling the iterator from more | |
| 1489 | 1501 | than one sequence, or not really storing things in a hash anywhere. |
| 1490 | 1502 | |
| 1491 | 1503 | =end original |
| 1492 | 1504 | |
| 1493 | このメソッドは keys() | |
| 1505 | このメソッドは keys(), values(), each() 反復の間に呼び出されます。 | |
| 1494 | 1506 | 二番目の引数として、最後にアクセスしたキーをとります。 |
| 1495 | 1507 | これは、あなたが順番に取り出すとか、二度以上反復子を呼び出したり、 |
| 1496 | 1508 | あるいは実際にはハッシュのどこにも格納されていないものであるときに |
| 1497 | 1509 | 便利です。 |
| 1498 | 1510 | |
| 1499 | 1511 | =begin original |
| 1500 | 1512 | |
| 1513 | NEXTKEY is always called in scalar context and it should just | |
| 1514 | return the next key. values(), and each() in list context, | |
| 1515 | will call FETCH for the returned keys. | |
| 1516 | ||
| 1517 | =end original | |
| 1518 | ||
| 1519 | NEXTKEY は常にスカラコンテキストで呼び出され、単に次のキーを | |
| 1520 | 返すべきです。 | |
| 1521 | values() と each() はリストコンテキストでは返された値に対して FETCH を | |
| 1522 | 呼び出します。 | |
| 1523 | ||
| 1524 | =begin original | |
| 1525 | ||
| 1501 | 1526 | For our example, we're using a real hash so we'll do just the simple |
| 1502 | 1527 | thing, but we'll have to go through the LIST field indirectly. |
| 1503 | 1528 | |
| 1504 | 1529 | =end original |
| 1505 | 1530 | |
| 1506 | 私たちの例では本当のハッシュを使うので、やることは簡単です | |
| 1531 | 私たちの例では本当のハッシュを使うので、やることは簡単です; しかし、 | |
| 1507 | ||
| 1532 | LIST フィールドを間接的に扱わなければなりません。 | |
| 1508 | 1533 | |
| 1509 | 1534 | sub NEXTKEY { |
| 1510 | 1535 | carp &whowasi if $DEBUG; |
| 1511 | 1536 | my $self = shift; |
| 1512 | 1537 | return each %{ $self->{LIST} } |
| 1513 | 1538 | } |
| 1514 | 1539 | |
| 1515 | 1540 | =item SCALAR this |
| 1516 | 1541 | X<SCALAR> |
| 1517 | 1542 | |
| 1518 | 1543 | =begin original |
| 1519 | 1544 | |
| 1520 | This is called when the hash is evaluated in scalar context | |
| 1545 | This is called when the hash is evaluated in scalar context, and in 5.28 | |
| 1521 | ||
| 1546 | onwards, by C<keys> in boolean context. In order to mimic the behaviour of | |
| 1522 | ||
| 1547 | untied hashes, this method must return a value which when used as boolean, | |
| 1548 | indicates whether the tied hash is considered empty. If this method does | |
| 1523 | 1549 | not exist, perl will make some educated guesses and return true when |
| 1524 | 1550 | the hash is inside an iteration. If this isn't the case, FIRSTKEY is |
| 1525 | 1551 | called, and the result will be a false value if FIRSTKEY returns the empty |
| 1526 | 1552 | list, true otherwise. |
| 1527 | 1553 | |
| 1528 | 1554 | =end original |
| 1529 | 1555 | |
| 1530 | これはハッシュがスカラコンテキストで評価されたとき | |
| 1556 | これはハッシュがスカラコンテキストで評価されたとき、および | |
| 1531 | ||
| 1557 | 5.28 以降は真偽値コンテキストの C<keys> で評価されたときに | |
| 1532 | ||
| 1558 | 呼び出されます。 | |
| 1559 | tie されていないハッシュの振る舞いを真似るために、 | |
| 1560 | 真偽値として使われるときに、tie されたハッシュが空と | |
| 1561 | 考えられるかどうかを示す値を返さなければなりません。 | |
| 1533 | 1562 | このメソッドが存在しない場合、perl はいくらかの教育された推測を行い、 |
| 1534 | 1563 | ハッシュが反復中である場合は真を返します。 |
| 1535 | 1564 | もしそうでない場合は、FIRSTKEY が呼び出され、これが空リストを返した場合は |
| 1536 | 1565 | 偽の値を返し、さもなければ真の値を返します。 |
| 1537 | 1566 | |
| 1538 | 1567 | =begin original |
| 1539 | 1568 | |
| 1540 | 1569 | However, you should B<not> blindly rely on perl always doing the right |
| 1541 | 1570 | thing. Particularly, perl will mistakenly return true when you clear the |
| 1542 | 1571 | hash by repeatedly calling DELETE until it is empty. You are therefore |
| 1543 | 1572 | advised to supply your own SCALAR method when you want to be absolutely |
| 1544 | 1573 | sure that your hash behaves nicely in scalar context. |
| 1545 | 1574 | |
| 1546 | 1575 | =end original |
| 1547 | 1576 | |
| 1548 | 1577 | しかし、perl が常に正しいことを行うと盲目的に信頼しては B<いけません>。 |
| 1549 | 1578 | 特に、ハッシュが空になるまで繰り返し DELETE を呼び出すことでハッシュを |
| 1550 | 1579 | クリアした場合、perl は間違って真を返します。 |
| 1551 | 1580 | 従って、ハッシュがスカラコンテキストでもうまく振舞うことを完全に確実に |
| 1552 | 1581 | したい場合は、独自の SCALAR メソッドを作ることを勧めます。 |
| 1553 | 1582 | |
| 1554 | 1583 | =begin original |
| 1555 | 1584 | |
| 1556 | 1585 | In our example we can just call C<scalar> on the underlying hash |
| 1557 | 1586 | referenced by C<$self-E<gt>{LIST}>: |
| 1558 | 1587 | |
| 1559 | 1588 | =end original |
| 1560 | 1589 | |
| 1561 | 1590 | この例では、C<$self-E<gt>{LIST}> でリファレンスされている、元となる |
| 1562 | 1591 | ハッシュで C<scalar> を呼び出しています: |
| 1563 | 1592 | |
| 1564 | 1593 | sub SCALAR { |
| 1565 | 1594 | carp &whowasi if $DEBUG; |
| 1566 | 1595 | my $self = shift; |
| 1567 | 1596 | return scalar %{ $self->{LIST} } |
| 1568 | 1597 | } |
| 1569 | 1598 | |
| 1599 | =begin original | |
| 1600 | ||
| 1601 | NOTE: In perl 5.25 the behavior of scalar %hash on an untied hash changed | |
| 1602 | to return the count of keys. Prior to this it returned a string containing | |
| 1603 | information about the bucket setup of the hash. See | |
| 1604 | L<Hash::Util/bucket_ratio> for a backwards compatibility path. | |
| 1605 | ||
| 1606 | =end original | |
| 1607 | ||
| 1608 | 注意: perl 5.25 以降、 | |
| 1609 | tie されていないハッシュに関する scalar %hash の振る舞いは、 | |
| 1610 | キーの数を返すように変更されました。 | |
| 1611 | これ以前は、ハッシュのバケツ設定に関する情報を含む文字列を返していました。 | |
| 1612 | 後方互換性のためには L<Hash::Util/bucket_ratio> を参照してください。 | |
| 1613 | ||
| 1570 | 1614 | =item UNTIE this |
| 1571 | 1615 | X<UNTIE> |
| 1572 | 1616 | |
| 1573 | 1617 | =begin original |
| 1574 | 1618 | |
| 1575 | 1619 | This is called when C<untie> occurs. See L<The C<untie> Gotcha> below. |
| 1576 | 1620 | |
| 1577 | 1621 | =end original |
| 1578 | 1622 | |
| 1579 | 1623 | これは C<untie> が発生した時に呼び出されます。 |
| 1580 | ||
| 1624 | 後述する L<The C<untie> Gotcha> を参照してください。 | |
| 1581 | 1625 | |
| 1582 | 1626 | =item DESTROY this |
| 1583 | 1627 | X<DESTROY> |
| 1584 | 1628 | |
| 1585 | 1629 | =begin original |
| 1586 | 1630 | |
| 1587 | 1631 | This method is triggered when a tied hash is about to go out of |
| 1588 | 1632 | scope. You don't really need it unless you're trying to add debugging |
| 1589 | 1633 | or have auxiliary state to clean up. Here's a very simple function: |
| 1590 | 1634 | |
| 1591 | 1635 | =end original |
| 1592 | 1636 | |
| 1593 | このメソッドは tie されたハッシュがスコープの外に出るときに | |
| 1637 | このメソッドは tie されたハッシュがスコープの外に出るときに呼び出されます。 | |
| 1594 | 呼び出されます。 | |
| 1595 | 1638 | 実際には、デバッグ情報を足そうとするとか、後始末のための |
| 1596 | 1639 | 補助的な情報を持っていなければ、必要になりません。 |
| 1640 | 以下はとても単純な関数です: | |
| 1597 | 1641 | |
| 1598 | 1642 | sub DESTROY { |
| 1599 | 1643 | carp &whowasi if $DEBUG; |
| 1600 | 1644 | } |
| 1601 | 1645 | |
| 1602 | 1646 | =back |
| 1603 | 1647 | |
| 1604 | 1648 | =begin original |
| 1605 | 1649 | |
| 1606 | 1650 | Note that functions such as keys() and values() may return huge lists |
| 1607 | 1651 | when used on large objects, like DBM files. You may prefer to use the |
| 1608 | 1652 | each() function to iterate over such. Example: |
| 1609 | 1653 | |
| 1610 | 1654 | =end original |
| 1611 | 1655 | |
| 1612 | 1656 | keys() や values() といった関数は、DBM ファイルのような大きなオブジェクトに |
| 1613 | 1657 | 対して使ったときに大きなリストを返す可能性があるということに |
| 1614 | 1658 | 注意してください。 |
| 1615 | 1659 | そういったものに対して繰り返しの処理を行うには、each() を使うのが |
| 1616 | 1660 | 良いでしょう。 |
| 1617 | 1661 | 例: |
| 1618 | 1662 | |
| 1619 | 1663 | # print out history file offsets |
| 1620 | 1664 | use NDBM_File; |
| 1621 | 1665 | tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0); |
| 1622 | 1666 | while (($key,$val) = each %HIST) { |
| 1623 | 1667 | print $key, ' = ', unpack('L',$val), "\n"; |
| 1624 | 1668 | } |
| 1625 | 1669 | untie(%HIST); |
| 1626 | 1670 | |
| 1627 | 1671 | =head2 Tying FileHandles |
| 1628 | 1672 | X<filehandle, tying> |
| 1629 | 1673 | |
| 1630 | 1674 | (ファイルハンドルを tie する) |
| 1631 | 1675 | |
| 1632 | 1676 | =begin original |
| 1633 | 1677 | |
| 1634 | 1678 | This is partially implemented now. |
| 1635 | 1679 | |
| 1636 | 1680 | =end original |
| 1637 | 1681 | |
| 1638 | 1682 | これは現時点ではまだ部分的にしか実装されていません。 |
| 1639 | 1683 | |
| 1640 | 1684 | =begin original |
| 1641 | 1685 | |
| 1642 | 1686 | A class implementing a tied filehandle should define the following |
| 1643 | 1687 | methods: TIEHANDLE, at least one of PRINT, PRINTF, WRITE, READLINE, GETC, |
| 1644 | 1688 | READ, and possibly CLOSE, UNTIE and DESTROY. The class can also provide: BINMODE, |
| 1645 | 1689 | OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators are |
| 1646 | 1690 | used on the handle. |
| 1647 | 1691 | |
| 1648 | 1692 | =end original |
| 1649 | 1693 | |
| 1650 | tie されたファイルハンドルを実装するクラスは以下のメソッドを定義すべきです | |
| 1694 | tie されたファイルハンドルを実装するクラスは以下のメソッドを定義すべきです: | |
| 1651 | 1695 | TIEHANDLE と、PRINT, PRINTF, WRITE, READLINE, GETC, READ の |
| 1652 | 1696 | 中の少なくともいずれか一つ、そして可能であればCLOSE, UNTIE, DESTROY。 |
| 1653 | 1697 | また、クラスは以下のものも提供できます: BINMODE, |
| 1654 | 1698 | OPEN, EOF, FILENO, SEEK, TELL - もし対応する perl の演算子がハンドルで |
| 1655 | 1699 | つかわれるならです。 |
| 1656 | 1700 | |
| 1657 | 1701 | =begin original |
| 1658 | 1702 | |
| 1659 | 1703 | When STDERR is tied, its PRINT method will be called to issue warnings |
| 1660 | 1704 | and error messages. This feature is temporarily disabled during the call, |
| 1661 | 1705 | which means you can use C<warn()> inside PRINT without starting a recursive |
| 1662 | 1706 | loop. And just like C<__WARN__> and C<__DIE__> handlers, STDERR's PRINT |
| 1663 | 1707 | method may be called to report parser errors, so the caveats mentioned under |
| 1664 | 1708 | L<perlvar/%SIG> apply. |
| 1665 | 1709 | |
| 1666 | 1710 | =end original |
| 1667 | 1711 | |
| 1668 | 1712 | STDERR が tie されると、その PRINT メソッドが、警告とエラーのメッセージを |
| 1669 | 1713 | 出力するために呼び出されます。 |
| 1670 | 1714 | この機能は呼び出しの最中には一時的に無効にされているので、再帰ループを |
| 1671 | 1715 | 作ることなく PRINT の内部で C<warn()> を使えることを意味します。 |
| 1672 | 1716 | また、C<__WARN__> や C<__DIE__> のハンドラと同様に、STDERR の |
| 1673 | 1717 | PRINT メソッドはパーサーエラーの報告に呼び出されるので、L<perlvar/%SIG> で |
| 1674 | 1718 | 言及した問題点が適用されます。 |
| 1675 | 1719 | |
| 1676 | 1720 | =begin original |
| 1677 | 1721 | |
| 1678 | 1722 | All of this is especially useful when perl is embedded in some other |
| 1679 | 1723 | program, where output to STDOUT and STDERR may have to be redirected |
| 1680 | 1724 | in some special way. See nvi and the Apache module for examples. |
| 1681 | 1725 | |
| 1682 | 1726 | =end original |
| 1683 | 1727 | |
| 1684 | 1728 | これら全ては perl が他のプログラムに埋め込まれていて、 |
| 1685 | 1729 | STDOUT や STDERR で出力する場所はなんらかの特殊なやり方でリダイレクトする |
| 1686 | 1730 | 必要があるときに特に便利です。 |
| 1687 | 1731 | 実際の例は nvi や Apache モジュールを参照してください。 |
| 1688 | 1732 | |
| 1689 | 1733 | =begin original |
| 1690 | 1734 | |
| 1735 | When tying a handle, the first argument to C<tie> should begin with an | |
| 1736 | asterisk. So, if you are tying STDOUT, use C<*STDOUT>. If you have | |
| 1737 | assigned it to a scalar variable, say C<$handle>, use C<*$handle>. | |
| 1738 | C<tie $handle> ties the scalar variable C<$handle>, not the handle inside | |
| 1739 | it. | |
| 1740 | ||
| 1741 | =end original | |
| 1742 | ||
| 1743 | ハンドルを tie するとき、C<tie> への最初の引数はアスタリスクで始まります。 | |
| 1744 | それで、STDOUT を tie するときは、C<*STDOUT> を使います。 | |
| 1745 | これを、例えば C<$handle> というスカラ変数に代入するときは、C<*$handle> を | |
| 1746 | 使います。 | |
| 1747 | C<tie $handle> はスカラ変数 C<$handle> の内部にあるハンドルではなく、スカラ | |
| 1748 | 自身を tie します。 | |
| 1749 | ||
| 1750 | =begin original | |
| 1751 | ||
| 1691 | 1752 | In our example we're going to create a shouting handle. |
| 1692 | 1753 | |
| 1693 | 1754 | =end original |
| 1694 | 1755 | |
| 1695 | 1756 | 私たちの例では、叫ぶハンドルを生成します。 |
| 1696 | 1757 | |
| 1697 | 1758 | package Shout; |
| 1698 | 1759 | |
| 1699 | 1760 | =over 4 |
| 1700 | 1761 | |
| 1701 | 1762 | =item TIEHANDLE classname, LIST |
| 1702 | 1763 | X<TIEHANDLE> |
| 1703 | 1764 | |
| 1704 | 1765 | =begin original |
| 1705 | 1766 | |
| 1706 | 1767 | This is the constructor for the class. That means it is expected to |
| 1707 | 1768 | return a blessed reference of some sort. The reference can be used to |
| 1708 | 1769 | hold some internal information. |
| 1709 | 1770 | |
| 1710 | 1771 | =end original |
| 1711 | 1772 | |
| 1712 | 1773 | これはこのクラスのコンストラクタです。 |
| 1713 | 1774 | その働きはなにかの bless されたリファレンスを返すことです。 |
| 1714 | 1775 | そのリファレンスは内部情報を保持するために使うことができます。 |
| 1715 | 1776 | |
| 1716 | 1777 | sub TIEHANDLE { print "<shout>\n"; my $i; bless \$i, shift } |
| 1717 | 1778 | |
| 1718 | 1779 | =item WRITE this, LIST |
| 1719 | 1780 | X<WRITE> |
| 1720 | 1781 | |
| 1721 | 1782 | =begin original |
| 1722 | 1783 | |
| 1723 | 1784 | This method will be called when the handle is written to via the |
| 1724 | 1785 | C<syswrite> function. |
| 1725 | 1786 | |
| 1726 | 1787 | =end original |
| 1727 | 1788 | |
| 1728 | 1789 | このメソッドは C<syswrite> 関数を通じてハンドルが書き出されるときに |
| 1729 | 1790 | 呼び出されます。 |
| 1730 | 1791 | |
| 1731 | | |
| 1792 | sub WRITE { | |
| 1732 | ||
| 1793 | $r = shift; | |
| 1733 | ||
| 1794 | my($buf,$len,$offset) = @_; | |
| 1734 | ||
| 1795 | print "WRITE called, \$buf=$buf, \$len=$len, \$offset=$offset"; | |
| 1735 | | |
| 1796 | } | |
| 1736 | 1797 | |
| 1737 | 1798 | =item PRINT this, LIST |
| 1738 | 1799 | X<PRINT> |
| 1739 | 1800 | |
| 1740 | 1801 | =begin original |
| 1741 | 1802 | |
| 1742 | 1803 | This method will be triggered every time the tied handle is printed to |
| 1743 | with the C<print()> function. | |
| 1804 | with the C<print()> or C<say()> functions. Beyond its self reference | |
| 1744 | ||
| 1805 | it also expects the list that was passed to the print function. | |
| 1745 | the print function. | |
| 1746 | 1806 | |
| 1747 | 1807 | =end original |
| 1748 | 1808 | |
| 1749 | このメソッドは tie されたハンドルに C<print> 関数を | |
| 1809 | このメソッドは tie されたハンドルに C<print> 関数または C<say()> 関数を | |
| 1750 | 度に呼び出されます。 | |
| 1810 | 使って出力される度に呼び出されます。 | |
| 1751 | 1811 | このメソッドは自分の参照のほか、print 関数に渡すリストを受け取ります。 |
| 1752 | 1812 | |
| 1753 | | |
| 1813 | sub PRINT { $r = shift; $$r++; print join($,,map(uc($_),@_)),$\ } | |
| 1754 | 1814 | |
| 1815 | =begin original | |
| 1816 | ||
| 1817 | C<say()> acts just like C<print()> except $\ will be localized to C<\n> so | |
| 1818 | you need do nothing special to handle C<say()> in C<PRINT()>. | |
| 1819 | ||
| 1820 | =end original | |
| 1821 | ||
| 1822 | C<say()> は C<print()> と同様に動作しますが、$\ は C<\n> に | |
| 1823 | ローカル化されるので C<PRINT()> の中で C<say()> を扱うために何も特別な | |
| 1824 | ことをする必要はありません。 | |
| 1825 | ||
| 1755 | 1826 | =item PRINTF this, LIST |
| 1756 | 1827 | X<PRINTF> |
| 1757 | 1828 | |
| 1758 | 1829 | =begin original |
| 1759 | 1830 | |
| 1760 | 1831 | This method will be triggered every time the tied handle is printed to |
| 1761 | 1832 | with the C<printf()> function. |
| 1762 | 1833 | Beyond its self reference it also expects the format and list that was |
| 1763 | 1834 | passed to the printf function. |
| 1764 | 1835 | |
| 1765 | 1836 | =end original |
| 1766 | 1837 | |
| 1767 | 1838 | このメソッドは tie されたハンドルに C<printf> 関数を使って |
| 1768 | 1839 | 出力される度に呼び出されます。 |
| 1769 | 1840 | このメソッドは自分の参照のほか、printf 関数に渡すリストを受け取ります。 |
| 1770 | 1841 | |
| 1771 | 1842 | sub PRINTF { |
| 1772 | 1843 | shift; |
| 1773 | 1844 | my $fmt = shift; |
| 1774 | 1845 | print sprintf($fmt, @_); |
| 1775 | 1846 | } |
| 1776 | 1847 | |
| 1777 | 1848 | =item READ this, LIST |
| 1778 | 1849 | X<READ> |
| 1779 | 1850 | |
| 1780 | 1851 | =begin original |
| 1781 | 1852 | |
| 1782 | 1853 | This method will be called when the handle is read from via the C<read> |
| 1783 | 1854 | or C<sysread> functions. |
| 1784 | 1855 | |
| 1785 | 1856 | =end original |
| 1786 | 1857 | |
| 1787 | 1858 | このメソッドはハンドルが C<read> や C<sysread> といった関数を通じて |
| 1788 | 1859 | 読まれたときに呼び出されます。 |
| 1789 | 1860 | |
| 1790 | | |
| 1861 | sub READ { | |
| 1791 | ||
| 1862 | my $self = shift; | |
| 1792 | ||
| 1863 | my $bufref = \$_[0]; | |
| 1793 | ||
| 1864 | my(undef,$len,$offset) = @_; | |
| 1794 | ||
| 1865 | print "READ called, \$buf=$bufref, \$len=$len, \$offset=$offset"; | |
| 1795 | ||
| 1866 | # add to $$bufref, set $len to number of characters read | |
| 1796 | ||
| 1867 | $len; | |
| 1797 | | |
| 1868 | } | |
| 1798 | 1869 | |
| 1799 | 1870 | =item READLINE this |
| 1800 | 1871 | X<READLINE> |
| 1801 | 1872 | |
| 1802 | 1873 | =begin original |
| 1803 | 1874 | |
| 1804 | This method | |
| 1875 | This method is called when the handle is read via C<E<lt>HANDLEE<gt>> | |
| 1805 | ||
| 1876 | or C<readline HANDLE>. | |
| 1806 | 1877 | |
| 1807 | 1878 | =end original |
| 1808 | 1879 | |
| 1809 | このメソッドは <HANDLE> を通して | |
| 1880 | このメソッドは C<E<lt>HANDLEE<gt>> または C<readline HANDLE> を通して | |
| 1810 | ||
| 1881 | ハンドルが読まれたときに呼び出されます。 | |
| 1811 | 1882 | |
| 1812 | ||
| 1883 | =begin original | |
| 1813 | 1884 | |
| 1885 | As per L<C<readline>|perlfunc/readline>, in scalar context it should return | |
| 1886 | the next line, or C<undef> for no more data. In list context it should | |
| 1887 | return all remaining lines, or an empty list for no more data. The strings | |
| 1888 | returned should include the input record separator C<$/> (see L<perlvar>), | |
| 1889 | unless it is C<undef> (which means "slurp" mode). | |
| 1890 | ||
| 1891 | =end original | |
| 1892 | ||
| 1893 | L<C<readline>|perlfunc/readline> のように、スカラコンテキストでは次の行を | |
| 1894 | 返すか、もうデータが無いときには C<undef> を返します。 | |
| 1895 | リストコンテキストでは残り全ての行を返すか、もうデータが無いときには | |
| 1896 | 空リストを返します。 | |
| 1897 | 返された文字列は入力レコードセパレータ C<$/> (L<perlvar> 参照) を | |
| 1898 | 含んでいます(C<undef> の場合 (「吸い込み」(slurp) モード) を除きます)。 | |
| 1899 | ||
| 1900 | sub READLINE { | |
| 1901 | my $r = shift; | |
| 1902 | if (wantarray) { | |
| 1903 | return ("all remaining\n", | |
| 1904 | "lines up\n", | |
| 1905 | "to eof\n"); | |
| 1906 | } else { | |
| 1907 | return "READLINE called " . ++$$r . " times\n"; | |
| 1908 | } | |
| 1909 | } | |
| 1910 | ||
| 1814 | 1911 | =item GETC this |
| 1815 | 1912 | X<GETC> |
| 1816 | 1913 | |
| 1817 | 1914 | =begin original |
| 1818 | 1915 | |
| 1819 | 1916 | This method will be called when the C<getc> function is called. |
| 1820 | 1917 | |
| 1821 | 1918 | =end original |
| 1822 | 1919 | |
| 1823 | 1920 | このメソッドは関数 C<getc> が呼ばれたときに呼び出されます。 |
| 1824 | 1921 | |
| 1825 | 1922 | sub GETC { print "Don't GETC, Get Perl"; return "a"; } |
| 1826 | 1923 | |
| 1924 | =item EOF this | |
| 1925 | X<EOF> | |
| 1926 | ||
| 1927 | =begin original | |
| 1928 | ||
| 1929 | This method will be called when the C<eof> function is called. | |
| 1930 | ||
| 1931 | =end original | |
| 1932 | ||
| 1933 | このメソッドは、C<eof> 関数が呼び出されたときに呼び出されます。 | |
| 1934 | ||
| 1935 | =begin original | |
| 1936 | ||
| 1937 | Starting with Perl 5.12, an additional integer parameter will be passed. It | |
| 1938 | will be zero if C<eof> is called without parameter; C<1> if C<eof> is given | |
| 1939 | a filehandle as a parameter, e.g. C<eof(FH)>; and C<2> in the very special | |
| 1940 | case that the tied filehandle is C<ARGV> and C<eof> is called with an empty | |
| 1941 | parameter list, e.g. C<eof()>. | |
| 1942 | ||
| 1943 | =end original | |
| 1944 | ||
| 1945 | Perl 5.12 から、追加の整数引数が渡されます。 | |
| 1946 | これは、C<eof> が引数なしで呼び出されたら 0 です; C<eof(FH)> のように、 | |
| 1947 | C<eof> がファイルハンドルを引数として呼び出されたら C<1> です; | |
| 1948 | tie されたファイルハンドルが C<ARGV> で、C<eof()> のように、C<eof> が | |
| 1949 | 空リストを引数として呼び出されるというとても特殊な場合は C<2> です。 | |
| 1950 | ||
| 1951 | sub EOF { not length $stringbuf } | |
| 1952 | ||
| 1827 | 1953 | =item CLOSE this |
| 1828 | 1954 | X<CLOSE> |
| 1829 | 1955 | |
| 1830 | 1956 | =begin original |
| 1831 | 1957 | |
| 1832 | 1958 | This method will be called when the handle is closed via the C<close> |
| 1833 | 1959 | function. |
| 1834 | 1960 | |
| 1835 | 1961 | =end original |
| 1836 | 1962 | |
| 1837 | 1963 | このメソッドは、C<close> 関数を通してハンドルがクローズされるときに |
| 1838 | 1964 | 呼び出されます。 |
| 1839 | 1965 | |
| 1840 | 1966 | sub CLOSE { print "CLOSE called.\n" } |
| 1841 | 1967 | |
| 1842 | 1968 | =item UNTIE this |
| 1843 | 1969 | X<UNTIE> |
| 1844 | 1970 | |
| 1845 | 1971 | =begin original |
| 1846 | 1972 | |
| 1847 | 1973 | As with the other types of ties, this method will be called when C<untie> happens. |
| 1848 | 1974 | It may be appropriate to "auto CLOSE" when this occurs. See |
| 1849 | 1975 | L<The C<untie> Gotcha> below. |
| 1850 | 1976 | |
| 1851 | 1977 | =end original |
| 1852 | 1978 | |
| 1853 | 1979 | その他の種類の tie と同様に、このメソッドは C<untie> が起きたときに |
| 1854 | 1980 | 呼び出されます。 |
| 1855 | 1981 | これが起きたときに、「自動 CLOSE」を行うのに適切です。 |
| 1856 | 1982 | 後述する L<The C<untie> Gotcha> を参照してください。 |
| 1857 | 1983 | |
| 1858 | 1984 | =item DESTROY this |
| 1859 | 1985 | X<DESTROY> |
| 1860 | 1986 | |
| 1861 | 1987 | =begin original |
| 1862 | 1988 | |
| 1863 | 1989 | As with the other types of ties, this method will be called when the |
| 1864 | 1990 | tied handle is about to be destroyed. This is useful for debugging and |
| 1865 | 1991 | possibly cleaning up. |
| 1866 | 1992 | |
| 1867 | 1993 | =end original |
| 1868 | 1994 | |
| 1869 | 1995 | 他の型に対する tie と同様に、このメソッドは tie されたハンドルが |
| 1870 | 1996 | 破棄されるときに呼び出されます。 |
| 1871 | 1997 | これはデバッグや後始末をするのに便利です。 |
| 1872 | 1998 | |
| 1873 | 1999 | sub DESTROY { print "</shout>\n" } |
| 1874 | 2000 | |
| 1875 | 2001 | =back |
| 1876 | 2002 | |
| 1877 | 2003 | =begin original |
| 1878 | 2004 | |
| 1879 | 2005 | Here's how to use our little example: |
| 1880 | 2006 | |
| 1881 | 2007 | =end original |
| 1882 | 2008 | |
| 1883 | 2009 | 以下は私たちのサンプルをどのように使うかの例です。 |
| 1884 | 2010 | |
| 1885 | 2011 | tie(*FOO,'Shout'); |
| 1886 | 2012 | print FOO "hello\n"; |
| 1887 | 2013 | $a = 4; $b = 6; |
| 1888 | 2014 | print FOO $a, " plus ", $b, " equals ", $a + $b, "\n"; |
| 1889 | 2015 | print <FOO>; |
| 1890 | 2016 | |
| 1891 | 2017 | =head2 UNTIE this |
| 1892 | 2018 | X<UNTIE> |
| 1893 | 2019 | |
| 1894 | 2020 | =begin original |
| 1895 | 2021 | |
| 1896 | 2022 | You can define for all tie types an UNTIE method that will be called |
| 1897 | 2023 | at untie(). See L<The C<untie> Gotcha> below. |
| 1898 | 2024 | |
| 1899 | 2025 | =end original |
| 1900 | 2026 | |
| 1901 | 2027 | 全ての型に対する tie について、untie() で呼び出される UNTIE メソッドを |
| 1902 | 2028 | 定義できます。 |
| 1903 | ||
| 2029 | 後述する L<The C<untie> Gotcha> を参照してください。 | |
| 1904 | 2030 | |
| 1905 | 2031 | =head2 The C<untie> Gotcha |
| 1906 | 2032 | X<untie> |
| 1907 | 2033 | |
| 1908 | 2034 | (C<untie> のコツ) |
| 1909 | 2035 | |
| 1910 | 2036 | =begin original |
| 1911 | 2037 | |
| 1912 | 2038 | If you intend making use of the object returned from either tie() or |
| 1913 | 2039 | tied(), and if the tie's target class defines a destructor, there is a |
| 1914 | 2040 | subtle gotcha you I<must> guard against. |
| 1915 | 2041 | |
| 1916 | 2042 | =end original |
| 1917 | 2043 | |
| 1918 | 2044 | tie() や tied() が返したオブジェクトを使おうとするならば、また、 |
| 1919 | 2045 | その tie されているターゲットクラスがデストラクタを |
| 1920 | 2046 | 定義しているのであれば、あなたが I<しなければならない> |
| 1921 | 2047 | 微妙なコツがあります。 |
| 1922 | 2048 | |
| 1923 | 2049 | =begin original |
| 1924 | 2050 | |
| 1925 | 2051 | As setup, consider this (admittedly rather contrived) example of a |
| 1926 | 2052 | tie; all it does is use a file to keep a log of the values assigned to |
| 1927 | 2053 | a scalar. |
| 1928 | 2054 | |
| 1929 | 2055 | =end original |
| 1930 | 2056 | |
| 1931 | セットアップとして、以下の tie の例を考えてみましょう | |
| 2057 | セットアップとして、以下の tie の例を考えてみましょう; これはファイルを | |
| 1932 | ||
| 2058 | 使って、スカラに代入された値を記録し続けるというものです。 | |
| 1933 | 2059 | |
| 1934 | 2060 | package Remember; |
| 1935 | 2061 | |
| 1936 | 2062 | use strict; |
| 1937 | 2063 | use warnings; |
| 1938 | 2064 | use IO::File; |
| 1939 | 2065 | |
| 1940 | 2066 | sub TIESCALAR { |
| 1941 | 2067 | my $class = shift; |
| 1942 | 2068 | my $filename = shift; |
| 1943 | 2069 | my $handle = IO::File->new( "> $filename" ) |
| 1944 | 2070 | or die "Cannot open $filename: $!\n"; |
| 1945 | 2071 | |
| 1946 | 2072 | print $handle "The Start\n"; |
| 1947 | 2073 | bless {FH => $handle, Value => 0}, $class; |
| 1948 | 2074 | } |
| 1949 | 2075 | |
| 1950 | 2076 | sub FETCH { |
| 1951 | 2077 | my $self = shift; |
| 1952 | 2078 | return $self->{Value}; |
| 1953 | 2079 | } |
| 1954 | 2080 | |
| 1955 | 2081 | sub STORE { |
| 1956 | 2082 | my $self = shift; |
| 1957 | 2083 | my $value = shift; |
| 1958 | 2084 | my $handle = $self->{FH}; |
| 1959 | 2085 | print $handle "$value\n"; |
| 1960 | 2086 | $self->{Value} = $value; |
| 1961 | 2087 | } |
| 1962 | 2088 | |
| 1963 | 2089 | sub DESTROY { |
| 1964 | 2090 | my $self = shift; |
| 1965 | 2091 | my $handle = $self->{FH}; |
| 1966 | 2092 | print $handle "The End\n"; |
| 1967 | 2093 | close $handle; |
| 1968 | 2094 | } |
| 1969 | 2095 | |
| 1970 | 2096 | 1; |
| 1971 | 2097 | |
| 1972 | 2098 | =begin original |
| 1973 | 2099 | |
| 1974 | 2100 | Here is an example that makes use of this tie: |
| 1975 | 2101 | |
| 1976 | 2102 | =end original |
| 1977 | 2103 | |
| 1978 | 2104 | 次に挙げるのは、この tie を使った例です。 |
| 1979 | 2105 | |
| 1980 | 2106 | use strict; |
| 1981 | 2107 | use Remember; |
| 1982 | 2108 | |
| 1983 | 2109 | my $fred; |
| 1984 | 2110 | tie $fred, 'Remember', 'myfile.txt'; |
| 1985 | 2111 | $fred = 1; |
| 1986 | 2112 | $fred = 4; |
| 1987 | 2113 | $fred = 5; |
| 1988 | 2114 | untie $fred; |
| 1989 | 2115 | system "cat myfile.txt"; |
| 1990 | 2116 | |
| 1991 | 2117 | =begin original |
| 1992 | 2118 | |
| 1993 | 2119 | This is the output when it is executed: |
| 1994 | 2120 | |
| 1995 | 2121 | =end original |
| 1996 | 2122 | |
| 1997 | 2123 | これを実行したときの出力は次のようになります。 |
| 1998 | 2124 | |
| 1999 | 2125 | The Start |
| 2000 | 2126 | 1 |
| 2001 | 2127 | 4 |
| 2002 | 2128 | 5 |
| 2003 | 2129 | The End |
| 2004 | 2130 | |
| 2005 | 2131 | =begin original |
| 2006 | 2132 | |
| 2007 | 2133 | So far so good. Those of you who have been paying attention will have |
| 2008 | 2134 | spotted that the tied object hasn't been used so far. So lets add an |
| 2009 | 2135 | extra method to the Remember class to allow comments to be included in |
| 2010 | the file | |
| 2136 | the file; say, something like this: | |
| 2011 | 2137 | |
| 2012 | 2138 | =end original |
| 2013 | 2139 | |
| 2014 | 2140 | まずまずですね。 |
| 2015 | 2141 | 注意深い人は tie されたオブジェクトがここでは使われていないことを |
| 2016 | 2142 | 指摘するでしょう。 |
| 2017 | 2143 | そこで、Remember クラスにファイルがコメントを含むことを |
| 2018 | できるようにするメソッドを追加しましょう | |
| 2144 | できるようにするメソッドを追加しましょう; そう、このような: | |
| 2019 | 2145 | |
| 2020 | 2146 | sub comment { |
| 2021 | 2147 | my $self = shift; |
| 2022 | 2148 | my $text = shift; |
| 2023 | 2149 | my $handle = $self->{FH}; |
| 2024 | 2150 | print $handle $text, "\n"; |
| 2025 | 2151 | } |
| 2026 | 2152 | |
| 2027 | 2153 | =begin original |
| 2028 | 2154 | |
| 2029 | 2155 | And here is the previous example modified to use the C<comment> method |
| 2030 | 2156 | (which requires the tied object): |
| 2031 | 2157 | |
| 2032 | 2158 | =end original |
| 2033 | 2159 | |
| 2034 | 2160 | 次の例は、前の例を C<comment> メソッド(tie されたオブジェクトを |
| 2035 | 2161 | 必要とします)を使うために変更したものです。 |
| 2036 | 2162 | |
| 2037 | 2163 | use strict; |
| 2038 | 2164 | use Remember; |
| 2039 | 2165 | |
| 2040 | 2166 | my ($fred, $x); |
| 2041 | 2167 | $x = tie $fred, 'Remember', 'myfile.txt'; |
| 2042 | 2168 | $fred = 1; |
| 2043 | 2169 | $fred = 4; |
| 2044 | 2170 | comment $x "changing..."; |
| 2045 | 2171 | $fred = 5; |
| 2046 | 2172 | untie $fred; |
| 2047 | 2173 | system "cat myfile.txt"; |
| 2048 | 2174 | |
| 2049 | 2175 | =begin original |
| 2050 | 2176 | |
| 2051 | 2177 | When this code is executed there is no output. Here's why: |
| 2052 | 2178 | |
| 2053 | 2179 | =end original |
| 2054 | 2180 | |
| 2055 | 2181 | このコードを実行したとき、なにも出力されません。 |
| 2056 | 2182 | その理由はこうです。 |
| 2057 | 2183 | |
| 2058 | 2184 | =begin original |
| 2059 | 2185 | |
| 2060 | 2186 | When a variable is tied, it is associated with the object which is the |
| 2061 | 2187 | return value of the TIESCALAR, TIEARRAY, or TIEHASH function. This |
| 2062 | 2188 | object normally has only one reference, namely, the implicit reference |
| 2063 | 2189 | from the tied variable. When untie() is called, that reference is |
| 2064 | 2190 | destroyed. Then, as in the first example above, the object's |
| 2065 | 2191 | destructor (DESTROY) is called, which is normal for objects that have |
| 2066 | 2192 | no more valid references; and thus the file is closed. |
| 2067 | 2193 | |
| 2068 | 2194 | =end original |
| 2069 | 2195 | |
| 2070 | 2196 | 変数が tie されたとき、それは TIESCALAR, TIEARRAY, TIEHASH といった |
| 2071 | 2197 | 関数のいずれかの返した値であるオブジェクトに結び付けられます。 |
| 2072 | 2198 | このオブジェクトは、通常はただ一つのリファレンス、すなわち tie され |
| 2073 | 2199 | た変数からの暗黙のリファレンスだけを持っています。 |
| 2074 | 2200 | untie() が呼ばれたとき、このリファレンスは破棄されます。 |
| 2075 | 2201 | したがって、最初の例にあったように、オブジェクトのデストラクタ (DESTROY) が |
| 2076 | 2202 | 呼び出されてオブジェクトはもはや正当なリファレンスを持たないようになり、 |
| 2077 | 2203 | さらにファイルがクローズされます。 |
| 2078 | 2204 | |
| 2079 | 2205 | =begin original |
| 2080 | 2206 | |
| 2081 | 2207 | In the second example, however, we have stored another reference to |
| 2082 | 2208 | the tied object in $x. That means that when untie() gets called |
| 2083 | 2209 | there will still be a valid reference to the object in existence, so |
| 2084 | 2210 | the destructor is not called at that time, and thus the file is not |
| 2085 | 2211 | closed. The reason there is no output is because the file buffers |
| 2086 | 2212 | have not been flushed to disk. |
| 2087 | 2213 | |
| 2088 | 2214 | =end original |
| 2089 | 2215 | |
| 2090 | 2216 | しかしながら二番目の例においては、私たちはもう一つの tie された |
| 2091 | 2217 | オブジェクトへのリファレンスを $x の中に格納しました。 |
| 2092 | 2218 | これは untie() されたときに、存在するオブジェクトに対する正当な |
| 2093 | リファレンスがまだ存在しているということです | |
| 2219 | リファレンスがまだ存在しているということです; このためデストラクタは | |
| 2094 | ||
| 2220 | その時には呼び出されません; そしてファイルはクローズされないのです。 | |
| 2095 | そしてファイルはクローズされないのです。 | |
| 2096 | 2221 | 何の出力も無かった理由は、ファイルバッファがディスクに |
| 2097 | 2222 | フラッシュされていなかったからです。 |
| 2098 | 2223 | |
| 2099 | 2224 | =begin original |
| 2100 | 2225 | |
| 2101 | 2226 | Now that you know what the problem is, what can you do to avoid it? |
| 2102 | 2227 | Prior to the introduction of the optional UNTIE method the only way |
| 2103 | 2228 | was the good old C<-w> flag. Which will spot any instances where you call |
| 2104 | 2229 | untie() and there are still valid references to the tied object. If |
| 2105 | 2230 | the second script above this near the top C<use warnings 'untie'> |
| 2106 | 2231 | or was run with the C<-w> flag, Perl prints this |
| 2107 | 2232 | warning message: |
| 2108 | 2233 | |
| 2109 | 2234 | =end original |
| 2110 | 2235 | |
| 2111 | さて、あなたはもうこれが問題であることがわかったでしょう | |
| 2236 | さて、あなたはもうこれが問題であることがわかったでしょう; | |
| 2112 | 2237 | では、これを避けるにはどうすればいいでしょうか? |
| 2113 | 2238 | 省略可能な UNTIE メソッドが導入される前は、唯一の方法は |
| 2114 | 2239 | 古き良き C<-w> オプションだけです。 |
| 2115 | 2240 | これははあなたが untie() を呼んだそのときに、(untie() の対象となっている) |
| 2116 | 2241 | tie されたオブジェクトに対する正当なリファレンスがまだ存在している場合には |
| 2117 | 2242 | それを指摘してくれます。 |
| 2118 | 2243 | もし二番目のスクリプトを先頭の方に C<use warnings 'untie'> を付けるか、 |
| 2119 | 2244 | C<-w> オプションをつけた状態で実行していれば、 |
| 2120 | 2245 | Perl は次のような警告メッセージを出力します。 |
| 2121 | 2246 | |
| 2122 | 2247 | untie attempted while 1 inner references still exist |
| 2123 | 2248 | |
| 2124 | 2249 | =begin original |
| 2125 | 2250 | |
| 2126 | 2251 | To get the script to work properly and silence the warning make sure |
| 2127 | 2252 | there are no valid references to the tied object I<before> untie() is |
| 2128 | 2253 | called: |
| 2129 | 2254 | |
| 2130 | 2255 | =end original |
| 2131 | 2256 | |
| 2132 | 2257 | スクリプトを正しく動作させ、警告を黙らせるには tie されたオブジェクトが |
| 2133 | 2258 | untie() を呼び出すより I<前に> 正当なリファレンスをなくすようにします。 |
| 2134 | 2259 | |
| 2135 | 2260 | undef $x; |
| 2136 | 2261 | untie $fred; |
| 2137 | 2262 | |
| 2138 | 2263 | =begin original |
| 2139 | 2264 | |
| 2140 | 2265 | Now that UNTIE exists the class designer can decide which parts of the |
| 2141 | 2266 | class functionality are really associated with C<untie> and which with |
| 2142 | 2267 | the object being destroyed. What makes sense for a given class depends |
| 2143 | 2268 | on whether the inner references are being kept so that non-tie-related |
| 2144 | 2269 | methods can be called on the object. But in most cases it probably makes |
| 2145 | 2270 | sense to move the functionality that would have been in DESTROY to the UNTIE |
| 2146 | 2271 | method. |
| 2147 | 2272 | |
| 2148 | 2273 | =end original |
| 2149 | 2274 | |
| 2150 | 2275 | 今や UNTIE が存在するので、クラスデザイナーはクラス機能のどの部分が |
| 2151 | 2276 | 本当に C<untie> に関連付けられ、どの部分がオブジェクトが破壊されたときに |
| 2152 | 2277 | 関連付けられるかを決定できます。 |
| 2153 | 2278 | 与えられたクラスについてどんな意味があるかは内部のリファレンスが |
| 2154 | 2279 | 維持されているかどうかに依存しているので、tie に関係ないメソッドは |
| 2155 | 2280 | オブジェクトで呼び出しできます。 |
| 2156 | 2281 | しかし、ほとんどの場合、DESTROY にある機能を UNTIE メソッドに移すのが |
| 2157 | 2282 | 意味のあることでしょう。 |
| 2158 | 2283 | |
| 2159 | 2284 | =begin original |
| 2160 | 2285 | |
| 2161 | 2286 | If the UNTIE method exists then the warning above does not occur. Instead the |
| 2162 | 2287 | UNTIE method is passed the count of "extra" references and can issue its own |
| 2163 | 2288 | warning if appropriate. e.g. to replicate the no UNTIE case this method can |
| 2164 | 2289 | be used: |
| 2165 | 2290 | |
| 2166 | 2291 | =end original |
| 2167 | 2292 | |
| 2168 | 2293 | もし UNTIE メソッドが存在するなら、上記の警告は起こりません。 |
| 2169 | 2294 | 代わりに UNTIE メソッドは「追加の」リファレンスの数が渡され、もし適切なら |
| 2170 | 自身の警告を出力できます | |
| 2295 | 自身の警告を出力できます。 | |
| 2171 | このメソッドが使えます: | |
| 2296 | 例えば、UNTIE がない場合を複製するには、このメソッドが使えます: | |
| 2172 | 2297 | |
| 2173 | | |
| 2298 | sub UNTIE | |
| 2174 | | |
| 2299 | { | |
| 2175 | | |
| 2300 | my ($obj,$count) = @_; | |
| 2176 | | |
| 2301 | carp "untie attempted while $count inner references still exist" | |
| 2177 | | |
| 2302 | if $count; | |
| 2303 | } | |
| 2178 | 2304 | |
| 2179 | 2305 | =head1 SEE ALSO |
| 2180 | 2306 | |
| 2181 | 2307 | =begin original |
| 2182 | 2308 | |
| 2183 | 2309 | See L<DB_File> or L<Config> for some interesting tie() implementations. |
| 2184 | 2310 | A good starting point for many tie() implementations is with one of the |
| 2185 | 2311 | modules L<Tie::Scalar>, L<Tie::Array>, L<Tie::Hash>, or L<Tie::Handle>. |
| 2186 | 2312 | |
| 2187 | 2313 | =end original |
| 2188 | 2314 | |
| 2189 | 2315 | 興味深い幾つかの tie() の実装については L<DB_File> や L<Config> を |
| 2190 | 2316 | 参照してください。 |
| 2191 | 2317 | 多くの tie() 実装のためのよい開始点は、モジュール L<Tie::Scalar>, |
| 2192 | 2318 | L<Tie::Array>, L<Tie::Hash>, L<Tie::Handle> のいずれかです。 |
| 2193 | 2319 | |
| 2194 | 2320 | =head1 BUGS |
| 2195 | 2321 | |
| 2196 | 2322 | =begin original |
| 2197 | 2323 | |
| 2198 | The | |
| 2324 | The normal return provided by C<scalar(%hash)> is not | |
| 2199 | 2325 | available. What this means is that using %tied_hash in boolean |
| 2200 | 2326 | context doesn't work right (currently this always tests false, |
| 2201 | 2327 | regardless of whether the hash is empty or hash elements). |
| 2328 | [ This paragraph needs review in light of changes in 5.25 ] | |
| 2202 | 2329 | |
| 2203 | 2330 | =end original |
| 2204 | 2331 | |
| 2205 | C<scalar(%hash)> で提供される | |
| 2332 | C<scalar(%hash)> で提供される通常の返り値は利用できません。 | |
| 2206 | 2333 | これが意味することは、真偽値コンテキストで %tied_hash を使っても正しく |
| 2207 | 2334 | 動作しないということです(現在のところ、ハッシュが空かハッシュ要素かに |
| 2208 | 2335 | 関わらず、このテストは常に偽となります)。 |
| 2336 | [ This paragraph needs review in light of changes in 5.25 ] | |
| 2209 | 2337 | |
| 2210 | 2338 | =begin original |
| 2211 | 2339 | |
| 2212 | 2340 | Localizing tied arrays or hashes does not work. After exiting the |
| 2213 | 2341 | scope the arrays or the hashes are not restored. |
| 2214 | 2342 | |
| 2215 | 2343 | =end original |
| 2216 | 2344 | |
| 2217 | 2345 | 配列やハッシュのローカル化は動作しません。 |
| 2218 | 2346 | スコープの終了後、配列やハッシュの値は元に戻りません。 |
| 2219 | 2347 | |
| 2220 | 2348 | =begin original |
| 2221 | 2349 | |
| 2222 | 2350 | Counting the number of entries in a hash via C<scalar(keys(%hash))> |
| 2223 | 2351 | or C<scalar(values(%hash)>) is inefficient since it needs to iterate |
| 2224 | 2352 | through all the entries with FIRSTKEY/NEXTKEY. |
| 2225 | 2353 | |
| 2226 | 2354 | =end original |
| 2227 | 2355 | |
| 2228 | 2356 | C<scalar(keys(%hash))> や C<scalar(values(%hash))> を使ってハッシュ内の |
| 2229 | 2357 | エントリの数を数えることは非効率的です; 全てのエントリに対して |
| 2230 | 2358 | FIRSTKEY/NEXTKEY を使って反復する必要があるからです。 |
| 2231 | 2359 | |
| 2232 | 2360 | =begin original |
| 2233 | 2361 | |
| 2234 | 2362 | Tied hash/array slices cause multiple FETCH/STORE pairs, there are no |
| 2235 | 2363 | tie methods for slice operations. |
| 2236 | 2364 | |
| 2237 | 2365 | =end original |
| 2238 | 2366 | |
| 2239 | 2367 | tie されたハッシュや配列のスライスは複数回の FETCH/STORE の組を引き起こします; |
| 2240 | 2368 | スライス操作のための tie メソッドはありません。 |
| 2241 | 2369 | |
| 2242 | 2370 | =begin original |
| 2243 | 2371 | |
| 2244 | 2372 | You cannot easily tie a multilevel data structure (such as a hash of |
| 2245 | 2373 | hashes) to a dbm file. The first problem is that all but GDBM and |
| 2246 | 2374 | Berkeley DB have size limitations, but beyond that, you also have problems |
| 2247 | with how references are to be represented on disk. One | |
| 2375 | with how references are to be represented on disk. One | |
| 2248 | 2376 | module that does attempt to address this need is DBM::Deep. Check your |
| 2249 | 2377 | nearest CPAN site as described in L<perlmodlib> for source code. Note |
| 2250 | 2378 | that despite its name, DBM::Deep does not use dbm. Another earlier attempt |
| 2251 | 2379 | at solving the problem is MLDBM, which is also available on the CPAN, but |
| 2252 | 2380 | which has some fairly serious limitations. |
| 2253 | 2381 | |
| 2254 | 2382 | =end original |
| 2255 | 2383 | |
| 2256 | 2384 | (ハッシュのハッシュのような)複数レベルのデータ構造を dbm ファイルに |
| 2257 | 2385 | tie することは簡単にはできません。 |
| 2258 | 2386 | 問題は、GDBM と Berkeley DB はサイズに制限があり、それを超えることが |
| 2259 | 2387 | できないということで、また、ディスク上にあるものを参照する方法についても |
| 2260 | 2388 | 問題があります。 |
| 2261 | これを解決しようとしている | |
| 2389 | これを解決しようとしているモジュールの一つに、DBM::Deep というものが | |
| 2262 | ||
| 2390 | あります。 | |
| 2263 | 2391 | ソースコードは L<perlmodlib> にあるように、 |
| 2264 | 2392 | あなたのお近くの CPAN サイトを確かめてください。 |
| 2265 | 2393 | その名前にも関わらず、DBM::Deep は DBM を使わないことに注意してください。 |
| 2266 | 2394 | 問題を解決するためのもう一つの初期の試みは MLDBM で、これも CPAN から |
| 2267 | 2395 | 利用可能ですが、かなり重大な制限があります。 |
| 2268 | 2396 | |
| 2269 | 2397 | =begin original |
| 2270 | 2398 | |
| 2271 | 2399 | Tied filehandles are still incomplete. sysopen(), truncate(), |
| 2272 | 2400 | flock(), fcntl(), stat() and -X can't currently be trapped. |
| 2273 | 2401 | |
| 2274 | 2402 | =end original |
| 2275 | 2403 | |
| 2276 | 2404 | ファイルハンドルの tie はまだ不完全です。 |
| 2277 | 2405 | 現在のところ、sysopen(), truncate(), flock(), fcntl(), stat(), -X は |
| 2278 | 2406 | トラップできません。 |
| 2279 | 2407 | |
| 2280 | 2408 | =head1 AUTHOR |
| 2281 | 2409 | |
| 2282 | 2410 | Tom Christiansen |
| 2283 | 2411 | |
| 2284 | 2412 | TIEHANDLE by Sven Verdoolaege <F<skimo@dns.ufsia.ac.be>> and Doug MacEachern <F<dougm@osf.org>> |
| 2285 | 2413 | |
| 2286 | 2414 | UNTIE by Nick Ing-Simmons <F<nick@ing-simmons.net>> |
| 2287 | 2415 | |
| 2288 | 2416 | SCALAR by Tassilo von Parseval <F<tassilo.von.parseval@rwth-aachen.de>> |
| 2289 | 2417 | |
| 2290 | 2418 | Tying Arrays by Casey West <F<casey@geeknest.com>> |
| 2291 | 2419 | |
| 2292 | 2420 | =begin meta |
| 2293 | 2421 | |
| 2294 | 2422 | Translate: KIMURA Koichi |
| 2295 | Update: Kentaro | |
| 2423 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.8.8-) | |
| 2424 | Status: completed | |
| 2296 | 2425 | |
| 2297 | 2426 | =end meta |