From df5933195f9dbe0fc69a1bd9752b84f267e4ec06 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 31 Mar 2007 03:13:23 +0200 Subject: [PATCH] common argument handling in functions Slightly cleaner and faster, and accepts read-only values in all functions. --- PLP/Functions.pm | 63 ++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 47 deletions(-) diff --git a/PLP/Functions.pm b/PLP/Functions.pm index 25022b5..b4891ee 100644 --- a/PLP/Functions.pm +++ b/PLP/Functions.pm @@ -33,14 +33,7 @@ sub PLP_END (&) { } sub Entity (@) { - my $ref; - my @copy; - if (defined wantarray) { - @copy = @_; - $ref = \@copy; - } else { - $ref = \@_; - } + my $ref = defined wantarray ? [@_] : \@_; for (@$ref) { eval { s/&/&/g; @@ -51,49 +44,30 @@ sub Entity (@) { s/\t/        /g; s/ /  /g; }; -# if ($@){ return defined wantarray ? @_ : undef } } return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef; } sub DecodeURI (@) { - # Browsers do s/ /+/ - I don't care about RFC's, but I do care about real-life - # situations. - my @r; - local $_; - for (@_) { - s/\+/%20/g; - my $dec = $_; - $dec =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge; - if (defined wantarray) { - push @r, $dec; - } else { - eval {$_ = $dec}; -# return undef if $@; # ;DecodeURI("foo"); - } + my $ref = defined wantarray ? [@_] : \@_; + for (@$ref) { + eval { + s/\+/%20/g; # Browsers do y/ /+/ - I don't care about RFC's, but + # I do care about real-life situations. + s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge; + }; } - return defined wantarray ? (wantarray ? @r : "@r") : undef; + return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef; } sub EncodeURI (@) { - my @r; - local $_; - for (@_) { - my $esc = $_; - $esc =~ - s{ - ([^\/?:@\$,A-Za-z0-9\-_.!~*\'()]) - }{ - sprintf("%%%02x", ord($1)) - }xge; - if (defined wantarray) { - push @r, $esc; - } else { - eval {$_ = $esc}; -# return undef if $@; # ;EncodeURI("foo"); - } + my $ref = defined wantarray ? [@_] : \@_; + for (@$ref) { + eval { + s{([^A-Za-z0-9\-_.!~*'()/?:@\$,])}{sprintf("%%%02x", ord $1)}ge; + }; } - return defined wantarray ? (wantarray ? @r : "@r") : undef; + return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef; } sub AddCookie ($) { @@ -149,12 +123,7 @@ sub Counter ($) { sub AutoURL ($) { # This sub assumes your string does not match /(["<>])\cC\1/ - my $ref; - if (defined wantarray){ - $ref = \(my $copy = $_[0]); - }else{ - $ref = \$_[0]; - } + my $ref = defined wantarray ? \(my $copy = $_[0]) : \$_[0]; eval { $$ref =~ s/"/"\cC"/g; # Single characters are easier to match :) $$ref =~ s/>/>\cC>/g; # so we can just use a character class [] -- 2.30.0