OK, so I'm being bugged again, bugged by some messiness and laziness - my two favorite pastimes...
# $q is CGI object # $foo and $ bar are hash references print $q->escapeHTML(Data::Dumper::Dumper($foo,$bar));
only prints $foo, because escapeHTML is expecting a string, and Dumper returns a list. So I use:
print $q->escapeHTML(join '', Data::Dumper::Dumper($foo,$bar));
and I think it's a bit ugly - ie, non intuitive. So, does anyone think escapeHTML should be amended in CGI.pm so that if you send it an array, it escapes all elements rather than just the first (and returns an array rather than a string)?

I just got a little perlexed - and I was bored enough to think about it a little... :)

.02

cLive ;-)

--
seek(JOB,$$LA,0);

Replies are listed 'Best First'.
Re: escapeHTML & scalar/array context
by Abigail-II (Bishop) on Jul 11, 2002 at 09:22 UTC
    I don't think one should modify CGI.pm. Just fix your call to Dumper. The Dumper subroutine is context sensitive, returning a list in list context and returning a single string in scalar context.

    Hence,

    print $q -> escapeHTML (scalar Data::Dumper::Dumper ($foo, $bar));
    ought to do the trick.

    Abigail

Re: escapeHTML & scalar/array context
by JayBonci (Curate) on Jul 11, 2002 at 08:14 UTC
    You could always just change the way you feed escapeHTML, without any sort of wonky join:
    map {print $q->escapeHTML($_)} Data::Dumper::Dumper($foo,$bar);
    Is that more of what you are looking for? It's a "clean" output of the answer, sans string munching.

        --jb

      map {print $q->escapeHTML($_)} Data::Dumper::Dumper($foo,$bar);

      map in void context is evil. Use map to generate a list, not to just loop over something. Use the for modifier for that.

      print $q->escapeHTML($_) for Data::Dumper::Dumper($foo, $bar);
      Or use map the way it is supposed to be used:
      print map $q->escapeHTML($_), Data::Dumper::Dumper($foo, $bar); print map { $q->escapeHTML($_) } Data::Dumper::Dumper($foo, $bar);
      (The blockless version is shorter and faster.)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: escapeHTML & scalar/array context
by Chmrr (Vicar) on Jul 11, 2002 at 17:36 UTC

    Though in general I'd use what Abigail-II suggests, it's worth mentioning that one can also do:

    print $q->escapeHTML(Data::Dumper::Dumper([$foo,$bar]));

    However, putting it in scalar context seems very much the Right Thing to do.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      ++, it just needs a little bit of extra polish to be perfect: print $q->escapeHTML(Data::Dumper::Dumper [$foo, $bar], [qw(*foo *bar)]); ____________
      Makeshifts last the longest.