だるろぐ

だるいぶろぐです

values関数が使いようによっては元のハッシュを書き換える件

会社で同僚が見つけてた。

use strict;
use warnings;
use Data::Dumper;sub p {warn Dumper @_;my @c = caller;print STDERR "  at $c[1]:$c[2]\n\n"}

my %h = qw/h hoge f foo b bar/;
p \%h;
for (values %h) {
    s/hoge/----------------/;
}
p \%h;
% perl h.pl
$VAR1 = {
          'h' => 'hoge',
          'b' => 'bar',
          'f' => 'foo'
        };
  at h.pl:7

$VAR1 = {
          'h' => '----------------',
          'b' => 'bar',
          'f' => 'foo'
        };
  at h.pl:11

知らなかった。何なのこれ。

% perldoc -f values

訳。
http://perldoc.jp/docs/perl/5.10.0/perlfunc.pod

値はコピーされないので、返されたリストを変更するとハッシュの中身が変更されることに注意してください。

まあこんな使い方する事は無かろうけど。