Skip Menu |

This queue is for tickets about the Params-Validate CPAN distribution.

Report information
The Basics
Id: 32646
Status: rejected
Priority: 0/
Queue: Params-Validate

People
Owner: Nobody in particular
Requestors: STEFFENW [...] cpan.org
Cc:
AdminCc:

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



Subject: Question about overload
Here is an example including a lot of code only to accept the overload problem: I have a subroutine named "print_string", 1 parameter, a string. Now this subroutine is called with an overloaded object "MyString". Without Params::Validate there is no problem, I accept SCALAR. Otherwise I have to accept OBJECT to but not every object. What should I do? ----------------------------------------------------------- package MyString; use strict; use warnings; use overload q{""} => 'stringify'; sub new { my ($class, $string) = @_; return bless \$string, $class; } sub stringify { my $self = shift; return ${$self}; } package main; use strict; use warnings; use Params::Validate qw(:all); use Carp qw(confess); sub print_string { my ($string) = validate_pos( @_, { type => SCALAR | OBJECT, callbacks => { stringify => sub { UNIVERSAL::can( shift, q{(""} ) or confess q{Only objects with overloaded "" are allowed}; $_[0] = q{} . $_[0]; 1; } }, }, ); print $string, "\n"; } print_string(MyString->new('This is a string!')); print_string(bless {}, __PACKAGE__);
Subject: Re: [rt.cpan.org #32646] Question about overload
Date: Fri, 25 Jan 2008 10:19:14 -0600 (CST)
To: Steffen Winkler via RT <bug-Params-Validate [...] rt.cpan.org>
From: Dave Rolsky <autarch [...] urth.org>
On Fri, 25 Jan 2008, Steffen Winkler via RT wrote: Show quoted text
> I have a subroutine named "print_string", 1 parameter, a string. > Now this subroutine is called with an overloaded object "MyString". > > Without Params::Validate there is no problem, I accept SCALAR. > Otherwise I have to accept OBJECT to but not every object. > > What should I do?
Well, it _is_ an object, not a scalar (an unblessed non-reference scalar, that is) Show quoted text
> { > type => SCALAR | OBJECT, > callbacks => { > stringify => sub { > UNIVERSAL::can( shift, q{(""} ) > or confess q{Only objects with overloaded "" > are allowed}; > $_[0] = q{} . $_[0]; > 1; > }
Don't use UNIVERSAL::can. Better to do this: sub { return 1 unless blessed $_[0]; return 1 if overload::Method( $_[0], q{""} ); return 0; } Note that this code is not tested. This is basically a limitation of Perl in that there's no real String class so you can't say "I'll accept anything that can act like the built-in String class". Anyway, this isn't a bug so I'm closing the ticket (please respond to me, not RT, or it will re-open the ticket). -dave /*=================================================== VegGuide.Org www.BookIRead.com Your guide to all that's veg. My book blog ===================================================*/