Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Data-Printer CPAN distribution.

Report information
The Basics
Id: 79939
Status: resolved
Priority: 0/
Queue: Data-Printer

People
Owner: Nobody in particular
Requestors: dirkus [...] yopmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Add example of adding calculated field to original ref
Can you add an example of adding a calculated field to the orignal ref? An example would be if you had an HTTP::Response, but wanted to add the decoded_content to the ref if the _content field was encoded (e.g. compressed). I tried this, but of course it had recursion problems: 'HTTP::Response' => sub { $_[0]->{decoded_content} = $_[0]->decoded_content; return p($_[0]); },
From: dirkus [...] yopmail.com
On Sun Sep 30 19:36:28 2012, dirk wrote: Show quoted text
> Can you add an example of adding a calculated field to the orignal ref? > An example would be if you had an HTTP::Response, but wanted to add the > decoded_content to the ref if the _content field was encoded (e.g. > compressed). > > I tried this, but of course it had recursion problems: > > 'HTTP::Response' => sub { > $_[0]->{decoded_content} = $_[0]->decoded_content; > return p($_[0]); > },
I figured out how to do it, but I'm not sure how wise it is to directly use the the internal _class function. Is there an alternative to this? filters => { 'HTTP::Response' => sub { my $res = shift; return unless $res->content_encoding; # Don't modify the original object. $res = $res->clone; $res->{decoded_content} = $res->decoded_content; return _class($res, @_); } }
Hi there! Sorry for not replying to this earlier, I've been terribly busy :/ I haven't released an API to explicitly call the original filters yet, mainly because I didn't thing people would want that. But don't fear, just keep on reading! In your particular case, the simplest solution without breaking encapsulation is to create a variable/structure with the items you want from the original object, then dumping it using p(). For example: use DDP filters => { 'HTTP::Response' => sub { my ($res, $p) = @_; my $decoded = $res->decoded_content; return p( \$decoded, $p ); } }; Of course, it may be the case where you want to see details of the entire (cloned) object. Well, when you create a filter in Data::Printer, you're not replacing the original one, you're just stacking yours on top of it. As it is written under the 'Filters' section of the documentation: "Your filters are supposed to return a defined value (usually, the string you want to print). If you don't, Data::Printer will let the next filter of that same type have a go, or just fallback to the defaults." So, here's how you would change your data and still be able to forward it to the original handler: use DDP filters => { 'HTTP::Response' => sub { my ($res, $p) = @_; # been here before? Forward to the original handler! return if exists $res->{decoded_content}; # first timer? Come on in! my $clone = $res->clone; $clone->{decoded_content} = $clone->decoded_content; return p($clone, $p); } }; This should avoid the recursion issue! I have added this tip under 'creating fiddling filters' on Data::Printer 0.34, credited under 'dirk'. If you let me have your full name and/or cpan|irc handle, I'll gladly update it :) Thanks! On Mon Oct 01 05:52:48 2012, dirk wrote: Show quoted text
> On Sun Sep 30 19:36:28 2012, dirk wrote:
> > Can you add an example of adding a calculated field to the orignal ref? > > An example would be if you had an HTTP::Response, but wanted to add the > > decoded_content to the ref if the _content field was encoded (e.g. > > compressed). > > > > I tried this, but of course it had recursion problems: > > > > 'HTTP::Response' => sub { > > $_[0]->{decoded_content} = $_[0]->decoded_content; > > return p($_[0]); > > },
> > I figured out how to do it, but I'm not sure how wise it is to directly > use the the internal _class function. Is there an alternative to this? > > filters => { > 'HTTP::Response' => sub { > my $res = shift; > return unless $res->content_encoding; > # Don't modify the original object. > $res = $res->clone; > $res->{decoded_content} = $res->decoded_content; > return _class($res, @_); > } > }