common argument handling in functions
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 31 Mar 2007 01:13:23 +0000 (03:13 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 31 Mar 2007 01:13:23 +0000 (03:13 +0200)
Slightly cleaner and faster, and accepts read-only values in all functions.

PLP/Functions.pm

index 25022b5db1e9a6270d9ed900d2de262d6d98aaab..b4891ee56453ae832b532f92aef31f5969cf6ae9 100644 (file)
@@ -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/&/&amp;/g;
@@ -51,49 +44,30 @@ sub Entity (@) {
                        s/\t/&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/g;
                        s/  /&nbsp;&nbsp;/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/&quot;/"\cC"/g; # Single characters are easier to match :)
                $$ref =~ s/&gt;/>\cC>/g;   # so we can just use a character class []