On Sat, Apr 25, 2009 at 21:45, Dan Fish <d...@ninemoons.com> wrote:
> I use Log::Dispatch frequently to set up both file and email logging.  In
> the past I have also used it with Data::Dumper for logging in debug mode
> doing something like:
>
>
>
> $dispatcher->log(level => 'debug', message
> =>sprintf("%s",Data::Dumper(\$somevar)));
>
>
>
> This makes logging hashes, arrays, objects, etc. very easy with no fuss, but
> it occurs to me that this is probably terribly inefficient since the entire
> contents of the variable being dumped are shoved into a string first,
> (effectively doubling the memory usage).
>
>
>
> I'm currently working on a project that can pull back a fairly large table
> from a database and would like to do something similar (at the debug log
> level only) and was just wondering if there were a more efficient way to
> pass the output of Data::Dumper to Log::Dispatch.
snip

I would say:

$dispatcher->log(
    level   => 'debug',
    message => Dumper $somevar,
);

The sprintf is pointless (the value is already a string).  Also,
Data::Dumper::Dumper can handle both reference and non-reference
scalars so there is no need to take a reference to a scalar variable.

If you are logging the data it might be a good idea to set
$Data::Dumper::Useqq to 1.  This will cause unprintable characters
(such as the DEL character) to print in either octal ("\177" for DEL)
or the familiar escape sequences (such as "\n" for linefeeds).  There
is a performance hit for this feature, so only turn it on if you think
you will need it.  Other nice options for logging include
$Data::Dumper::Terse, $Data::Dumper::Indent, and
$Data::Dumper::SortKeys.  More information can be found in the
documenation[1].

Also, the last comma is there on purpose.  Perl allows trailing
commas, and in cases like this it is a good practice to leave one so
new items can be added to the list easily.

1. http://perldoc.perl.org/Data/Dumper.html#Configuration-Variables-or-Methods

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to