Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3277866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:25:03+00:00 2026-05-17T19:25:03+00:00

I’m creating a class which will contain a list of IP addresses, as Net::IP

  • 0

I’m creating a class which will contain a list of IP addresses, as Net::IP objects.

I’ve wrapped the Net::IP object as a subtype (IPAddress), and defined a coercion from a string to IPAddress. Then I’ve added an attribute to the class called ip_list with the type ArrayRef[IPAddress], and delegated to the push method of the Array trait.

use MooseX::Declare;
use Moose::Util::TypeConstraints;

use Net::IP;

subtype 'IPAddress'
    => as 'Object'
    => where { $_->isa('Net::IP') };

coerce 'IPAddress'
    => from 'Str'
    => via { Net::IP->new( $_ ) };

class IPs {

    has 'ip_list' => ( traits  => ['Array'],
                       isa    => 'ArrayRef[IPAddress]',
                       is     => 'rw',
                       coerce => 1,
                       auto_deref => 1,
                       default => sub { [] },
                       handles => {
                           add_ip    => 'push'
                       }
                       );

}

However if I try to call the delegated method like so:

my $o = IPs->new();
$o->add_ip( '192.168.0.1' );

I get the error “Value SCALAR(0x8017e8) did not pass container type constraint ‘IPAddress’ at …”

So obviously the parameter to add_ip is not being coerced.

Is it possible to do what I’m attempting, or should I just do all this manually? I’ve trawled through the Moose manuals but I’ve not seen anything that would indicate either way, but I am probably missing something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:25:04+00:00Added an answer on May 17, 2026 at 7:25 pm

    Unfortunately Moose does not chain coercions (it would be really complicated to parse these internally and figure out what the “right thing to do” is in an automatic fashion), so you need to define the chain yourself:

    use Net::IP;
    
    class_type 'Net::IP';
    
    coerce 'Net::IP'
        => from 'Str'
        => via { Net::IP->new( $_ ) };
    
    subtype 'ArrayRefOfIPAddresses'
        => as 'ArrayRef[Net::IP]';
    
    coerce 'ArrayRefOfIPAddresses'
        => from 'ArrayRef[Str]'
        => via { [ map { Net::IP->new($_) } @$_ ] };
    
    coerce 'ArrayRefOfIPAddresses'
        => from 'Str'
        => via { [ Net::IP->new($_) ] };
    
    coerce 'ArrayRefOfIPAddresses'
        => from 'Net::IP'
        => via { [ $_ ] };
    
    class IPs {
    
        has 'ip_list' => ( traits  => ['Array'],
                           isa    => 'ArrayRefOfIPAddresses',
                           # ... rest of declaration as before
                         );
    
    }
    

    PS. since you are using the Array native delegation trait, I would recommend you avoid auto_deref – add a handler instead:

    has ip_list => (
        is => 'bare',
        # ...
        handles => {
            # ...
            ip_list => 'elements',
        },
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.