develooper Front page | perl.moose | Postings from September 2014

Re: Coerce and messages

Thread Previous
From:
Bill Moseley
Date:
September 29, 2014 01:14
Subject:
Re: Coerce and messages
Message ID:
CAKhN_m7=dtD5n-UeZ49Mg-w=w6z+769H=rFi65fzOeoFvN19VQ@mail.gmail.com
On Sun, Sep 28, 2014 at 4:52 PM, Karen Etheridge <perl@froods.org> wrote:

> On Sun, Sep 28, 2014 at 01:27:53PM -0700, Bill Moseley wrote:
> > I'm going to build a request with HTTP::Request::Common and want to use
> its
> > existing methods (POST, PUT, GET, etc.)
> >
> > I'm curious how to control the error message better when coercing.
>
> I'm lost - what is the advantage to storing the HTTP request method as a
> subref, vs. as a string?
>

No advantage, really.    It was more of a learning exercise with Moose, not
so much solving the specific problem.

The code came from a non-Moose script that did this:

    my $sub = HTTP::Request::Common->can( $method )
        || die qq'Could not figure out how to process method "$method"';

   my $req = $sub->( @args );

So, moving it over to Moose I wanted to provide some validation, so I
created the HttpMethod subtype.

subtype HttpMethod =>
    as 'Str',
    where { HTTP::Request::Common->can( $_ ) },
    message { "The method provided [$_] does not appear to be a valid HTTP
method" };

That makes sense to validate it that way because that's how $method is
going to get used, plus it provides a nice error message.

Since I needed that code ref anyway I thought I'd coerce it and then call
->can only once.   But, once I did that I didn't have my nice error message
any more.

So, I was wondering if I could coerce yet still use my validation.

Thanks,

-- 
Bill Moseley
moseley@hank.org

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About