Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Data::PostfixDeref (also a lame name, but never mind) will allow you to write

    sub add_args {
        my $self = shift;
        push $self->{args}[], @_;

    instead. It has its limitations: there is no slice syntax, because I haven't decided whether $foo->{bar}[][1, 2, 3] is too weird or not yet, and because it's somewhat trickier to implement; and it doesn't allow a direct deref $foo->[] due to the optimizer not getting run on the ops involved.

    It doesn't use a source filter, instead it h

  • That would be List::oo.

    But Class::Accessor::Classy should do what you want without even needing to write the add_args() method.

    • List::oo does look very similar to what I want, except that I want is simply to have all the the ease of use of objects for arrays and hashes (maybe scalars?) without the messiness. Also, by using nicely de-coupled functions to build things, I wouldn't have to "buy into" a particular object system. This would work with Moose as well as with hand-rolled code (which is why Class::Accessor::Classy doesn't seem like the right fit for me).

      • "All the ease of use of objects without the messiness"? What's the messy part? List::oo blesses an arrayref, so you're never far from punching through the abstraction. If the constructor is the messy part, then you have L().

        Or do you mean cognitive messiness? Abstraction layers do tend to be abstract, and everything's a waterbed, so...

        As for object systems - C::A::Classy really isn't much of one. And that is intentional. I'm not sure what the benefit of de-coupling is supposed to be - you get to write

  • You’re saying it’d be easier to port your codebase to Perl 6 than to upgrade your perl to a version that supports autobox []? :-)

    • Except that if I distribute modules, I want to minimize the dependencies. How many times have people failed to install a module because they don't like the the number of dependencies, have an aversion to a particular dependency or can't risk upgrading a particular dependency? The dependency problem is a very real one in Perl 5 and suggesting (admittedly sexy) XS code to solve this problem seems like overkill :)

      A very tiny, lightweight embeddable utility would be nice here (yes, there's the risk of duplica

  • Ya do know that Moose + MooseX::AttributeHelpers would abstract away all that code you wrote, right?

    Boxing is nice, but not really what you need for your example.

    Anyway, here's the same code with Moose:

    package Stuff;
    use Moose;
    use MooseX::AttributeHelpers;

    has args => (
            is => 'ro',
            isa => 'ArrayRef',
            metaclass => 'Collection::Array',
            auto_deref => 1,

    • BTW, normally I align the => arrows, but use.perl killed the spaces in the <code> block. Yay.
  • package Stuff;
    use Moose::Autobox (); # don't actually autobox

    sub new {
      my ($class, @args) = @_;
      bless {
        args =>
          # this is the class that autoboxing would use anyway
          bless \@args => 'Moose::Autobox::ARRAY',
      } => $class;

    sub add_args {
      my $self = shift;
      $self->{args}->push(@_); # NOT UGLY!!!

    # Likewise, use Moose::Autobox::HASH for hashes.