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

  • Home
  • SEARCH
  • 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 6007233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:39:07+00:00 2026-05-23T01:39:07+00:00

I frequently use Moose to make sure my data have suitable default values, like

  • 0

I frequently use Moose to make sure my data have suitable default values, like here:

package Bla;
use Moose;
has eins => is => 'ro', isa => 'Int';
has zwei => is => 'ro', isa => 'Int', default => 2;
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use v5.10;
use Data::Dumper;
use URI;

my $bla = Bla->new( eins => 77 );
my $bl2 = Bla->new;
print Dumper $bla, $bl2;
say join "\t", %$bla;
say join "\t", %$bl2;

my $u = URI->new( 'http://www.example.com/ws' );
$u->query_form( %$bla );
say $u;
$u->query_form( %$bl2 );
say $u;

As long as that kind of data container doesn’t have any reference members (hence no nesting), would you say it is okay, or recommendable, to just use the object hashtable as in %$object if you want to get at the raw data, let’s say as initializer for serialization via URI->query_form or similar methods? Or is there a better way to achieve this that’s built into Moose?

UPDATE

Looks like I’ve been leading people on the wrong tracks by dropping the little word serialization in the lines above, and in the title, and even in the tags. Note that I’m not interested in finding serializers. In my example, the URI module is the serializer. The question is how to get at the data to feed URI->query_form (or any other serializer I might try). So what I want to know is whether for a given Moose object $object it is okay to grab the data (keys and values, or, if you prefer, property names and values) by just dereferencing the object reference, as in %$object? This will work, if my experience hitherto collected is anything to go by, as long as the objects don’t contain any reference values (like array references, other objects, etc) and – the thing I’m not sure about – Moose won’t use the instance reference to store data of its own, like __MOOSE_WHATNOT => $funky_moose_addon. So might Moose use the instance reference to store some of its own data, or is that precluded by design?

UPDATE 2

To answer the question in the title:

No, it’s not okay to use %$object to get at the data of a Moose object, even if it doesn’t contain any reference values so you’d get a copy of the strings and numbers that make up the $object. It’s not okay because it breaks encapsulation. It might even result in a runtime error because although the hash is the default data structure to form the base of a Moose object, there is no guarantee that it will always be a hash, and it might in fact be something else.

You should use MooseX::Storage instead, which will provide the object with a pack method.

  • 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-23T01:39:08+00:00Added an answer on May 23, 2026 at 1:39 am

    As jira has said the canonical way would be to use MooseX::Storage. The problem with %$object is that while the default storage for a Moose object instance is a blessed hash, Moose makes no formal promises that this is always the case. MooseX::GlobRef, MooseX::NonMoose, MooseX::InsideOut for example all allow for alterative instance structures.

    A package like MooseX::Storage uses the MOP to interrogate the instance meta-object and serialize the data structure properly. You can of course do this by hand by crawling the Moose::Meta::Class that is behind every Moose object, but honestly MooseX::Storage is well written and very stable at this point.

    The standard usage would be:

    package Class {
         use Moose;
         use MooseX::Storage;
         with Storage();
         ...
    }
    
    my $o = Class->new(...)
    my $u = URI->new( 'http://www.example.com/ws' );
    $u->query_form( $o->pack );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a package (really just one subroutine) I use frequently to parse config
People use frequently something like: <ListBox ItemsSource={Binding ElementName=thisControl, Path=ListIndexes}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Label Content={Binding
I frequently make use of Request.QueryString[] variables. In my Page_load I often do things
I have a 'foreach' macro I use frequently in C++ that works for most
I frequently use the STL containers but have never used the STL algorithms that
Frequently, I use functions to shape or numerically alter data that I'm passing to
I'm trying to collecting data from a frequently updating blog, so I simply use
I frequently use git stash and git stash pop to save and restore changes
I frequently use HTML output in applications, up to now I've used some simple
I use Visual C++ 2008 in Visual Studio 2008. I frequently use the following

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.