Mojo::Base get all attributes names

51 views
Skip to first unread message

Konstantin Cherednichenko

unread,
Feb 5, 2018, 5:14:06 AM2/5/18
to Mojolicious
Hello!

Is there any easy way to get all attributes names?

I tried to get it via ISA but I also got methods names... I just want to get "propA" and "propB"...
Could someone please help me?

My example:

#!/usr/bin/env perl

package A;
use Mojo::Base -base;

has propA => 123;
has propB => 'val';

sub get_props_names
{
    my $self = shift;

    my %names = ();

    {
        my $base_class = ref($self);

        no strict 'refs';

        foreach my $class ($base_class, @{"${base_class}::ISA"})
        {
            foreach my $name (keys %{"${class}::"})
            {
                $names{$name} ||= 1;
            }
        }
    };

    return [sort keys %names];
}

package main;
use Mojo::Base -strict;
use Data::Dumper;

my $obj = A->new;

say Dumper($obj->get_props_names);

sri

unread,
Feb 5, 2018, 5:21:10 AM2/5/18
to Mojolicious
Is there any easy way to get all attributes names?

No.

--
sebastian 

Dan Book

unread,
Feb 5, 2018, 11:16:46 AM2/5/18
to mojol...@googlegroups.com
Introspection capabilities are a feature of Moose.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Charlie Brady

unread,
Feb 5, 2018, 1:13:19 PM2/5/18
to mojol...@googlegroups.com

On Mon, 5 Feb 2018, Dan Book wrote:

> Introspection capabilities are a feature of Moose.

Moose, however, adds many, many more dependencies than Mojo::Base...
> > email to mojolicious...@googlegroups.com.

Илья Рассадин

unread,
Feb 6, 2018, 10:04:40 AM2/6/18
to Mojolicious
To be fare, Mouse can do it as well. And Moo too (but not with support of Moose Meta Object Protocol).

понедельник, 5 февраля 2018 г., 21:13:19 UTC+3 пользователь Charlie Brady написал:

Dan Book

unread,
Feb 6, 2018, 11:03:03 AM2/6/18
to mojol...@googlegroups.com
If you want the introspection you're going to be expanding objects to Moose anyway so you might want to start there (though if you rarely do the introspection, using Moo for everything else may be a benefit). As for Mouse I would not recommend its usage to be combined with any other modules. https://shadow.cat/blog/matt-s-trout/moo-versus-any-moose/

-Dan

To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.

Jan Henning Thorsen

unread,
Feb 14, 2018, 11:02:42 AM2/14/18
to Mojolicious
Not quite sure what to call such a module, but I just wrote a proof of concept here: https://gist.github.com/jhthorsen/0ad4c5c84661588e89da910a08460e88
Reply all
Reply to author
Forward
0 new messages