読者です 読者をやめる 読者になる 読者になる

NeverBlog::Likk::Unexistable;

見なかったことにして下さい

CGI::escapeHTMLが貧相すぎるので

多重配列とかハッシュとかダメダメだったので、ついカッ(ry
再起使えよ という話。


関数部分

sub escapeHTML {
	@_ = @_ ? @_ : $_ if defined wantarray;
	for (@_ ? @_ : $_) {
		my $val = $_;
		my $type = ref $val;
		if($type eq 'ARRAY'){
			if ((scalar @_) > 1){
				escapeHTML(@$_);
			}else{
				my @array = @$_;
				@array = escapeHTML(@array);
				return \@array;
			}
		}elsif($type eq 'HASH'){
			if ((scalar @_) > 1){
				escapeHTML(%$_);
			}else{
				my %hash = %$_;
				%hash = escapeHTML(%hash);
				return \%hash;
			}
		}elsif($type eq 'SCALAR'){
			my $scalar = $$_;
			$scalar = escapeHTML($scalar);
			return \$scalar;
		}else{
			s{&}{&}gso;
			s{<}{<}gso;
			s{>}{>}gso;
		}
	}
	return wantarray ? @_ : "@_";
};

呼び出し側

my $scaler = "<STRING>";
my $scalar_ref = \"<STRING>";
my @array = (1,2,3,"<STRING>");
my @double_array = (
				[1,2,3,"<STRING>"],
				[1,2,3,"<STRING>"],
				[[1,2,3,"<STRING>"],1,2,3,"<STRING>"]
				);
my $array_ref = [1,2,3,"<STRING>"];
my $double_array_ref = [
				[1,2,3,"<STRING>"],
				[1,2,3,"<STRING>"],
				];
my %hash = (
		'str' , "<STRING>",
		'num' , 123,);

my $hash_ref = {
		'str' , "<STRING>",
		'num' , 123,};

$scaler = escapeHTML($scaler);
print "scaler =>".$scaler;

$scalar_ref = escapeHTML($scalar_ref);
print "scaler_ref =>".$$scalar_ref;
@array = escapeHTML(@array);
print "array =>".$array[3];
$array_ref = escapeHTML($array_ref);
print "array_ref =>".$array_ref->[3];
@double_array = escapeHTML(@double_array);
print "double_array =>".$double_array[2][0][3];
$double_array_ref = escapeHTML($double_array_ref);
print "double_array_ref =>".$double_array_ref->[1][3];
%hash = escapeHTML(%hash);
print "hash =>".$hash{'str'};
$hash_ref = escapeHTML($hash_ref);
print "hash_ref =>".$hash_ref->{'str'};

#複雑なレコードのハッシュの宣言
%TV = (
        flintstones => {
            series   => "<flintstones>",
            nights   => [ qw(monday thursday friday) ],
            members  => [
                { name => "<fred",    role => "lead", age  => 36, },
                { name => "<wilma",   role => "wife", age  => 31, },
                { name => "<pebbles", role => "kid",  age  =>  4, },
            ],
        },


        jetsons     => {
            series   => "jetsons",
            nights   => [ qw(wednesday saturday) ],
            members  => [
                { name => "george>",  role => "lead", age  => 41, },
                { name => "jane>",    role => "wife", age  => 39, },
                { name => "elroy>",   role => "kid>",  age  =>  9, },
            ],
         },


        simpsons    => {
            series   => "simpsons>",
            nights   => [ qw(monday) ],
            members  => [
                { name => "homer&", role => "lead", age  => 34, },
                { name => "marge&", role => "wife", age => 37, },
                { name => "bart&",  role => "kid",  age  =>  11, },
            ],
         },
      );

%TV = escapeHTML(%TV);
print "TV  =>".$TV{'flintstones'}{'members'}[2]{'name'};

実行結果

scaler =>&lt;STRING&gt;
scaler_ref =>&lt;STRING&gt;
array =>&lt;STRING&gt;
array_ref =>&lt;STRING&gt;
double_array =>&lt;STRING&gt;
double_array_ref =>&lt;STRING&gt;
hash =>&lt;STRING&gt;
hash_ref =>&lt;STRING&gt;
TV  =>&lt;pebbles

m/(スカラ(リファレンス)*)|((多次元)*[配列|ハッシュ](リファレンス)*)/ 対応しました。

($hash_ref,$array_ref) = escapeHTML($hash_ref,$array_ref);
とかもオッケーです。
複数配列やハッシュ渡したい場合は一度リファレンスにしてください。
というか、複数の配列/ハッシュを関数へ参照渡しするのはPerlの基本でした。