Sudarshan Raghavan wrote:

> simran wrote:
>
> > I wrote my self this subroutine... which comes in very handy :-)
> >
> > ..snip
> >
> > sub strip {
> >   my $self = shift;
> >   my $ref  = shift;
> >
> >   if (! ref($ref)) {
> >     $ref =~ s/(^[\s\t]*)|([\s\t]*$)//g if (defined $ref);
>
> Why is it better to do this in two steps? Read through this FAQ
> perldoc -q 'How do I strip blank space from the beginning/end of a string'
>
> \s includes \t, the character class [\s\t] is uneccessary. The capture in your regex
> match can also be avoided, you might want to a look at the '?:' extended pattern in
> perldoc perlre. The 'g' modifier does not server any purpose, it would if used along 
> with
> the 's' or 'm' modifier.
>
> >
> >     return $ref;
> >   }
> >   elsif (ref($ref) =~ /^ARRAY$/i) {
>
> This could just have been elsif (ref($ref) eq 'ARRAY'), avoid regexes if possible.
>
> >
> >     foreach my $i (0 .. $#$ref) {
> >       $ref->[$i] =~ s/(^[\s\t]*)|([\s\t]*$)//g;
> >     }
> >   }
> >   elsif (ref($ref) =~ /^HASH$/i) {
> >     while (my ($key, $value) = each %$ref) {
> >       delete $ref->{$key};
> >       $key         =~ s/(^[\s\t]*)|([\s\t]*$)//g;
> >       $value       =~ s/(^[\s\t]*)|([\s\t]*$)//g;
> >       $ref->{$key} = $value;
> >     }
> >   }
> >   elsif (ref($ref) =~ /^SCALAR$/i) {
> >     $$ref =~ s/(^[\s\t]*)|([\s\t]*$)//g;
> >   }
> >   else {
> >     die "Unknown reference type";
> >   }
> > }
> >
>
> This is another approach for the same problem, you can use this for starters :-)
> The arguments passed to a subroutine in the @_ arrays are aliases for the original
> parameters (perldoc perlsub). You can make use of that here.
>
> sub trim {
>     foreach (@_) {
>         my $ref_to = ref ($_);
>         SWITCH: {
>             (!$ref_to) && do {
>                 s/^\s+//;
>                 s/\s+$//;
>                 last SWITCH;
>             };
>             ($ref_to eq 'SCALAR') && do {
>                 ${$_} =~ s/^\s+//;
>                 ${$_} =~ s/\s+$//;
>                 last SWITCH;
>             };
>             ($ref_to eq 'ARRAY') && do {
>                 foreach my $arr_elem (@{$_}) {
>                     $arr_elem =~ s/^\s+//;
>                     $arr_elem =~ s/\s+$//;
>                 }
>                 last SWITCH;
>             };
>             ($ref_to eq 'HASH') && do {
>                 foreach my $hash_val (%{$_}) {

oops, the above statement should be
foreach my $hash_val (values (%{$_})) {


>
>                     $hash_val =~ s/^\s+//;
>                     $hash_val =~ s/\s+$//;
>                 }
>                 last SWITCH;
>             };
>             print "Invalid parameter\n";
>         }
>     }
> }
>
> HTH


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to