Skip to content

[PPC0022] - naming convention for exceptional vs. undef-returning methods #45

@leonerd

Description

@leonerd

The original PPC doc suggested two sets of "fetch an element from a meta-package" methods, named "get_..." and "can_..." to reflect the difference between methods that throw exceptions and methods that return undef when the requested entity does not exist.

my $metasym = $metapkg->get_symbol($name);  # throws if missing
my $metasym = $metapkg->can_symbol($name);  # return undef if missing

My original inspiration for can_... came from Perl's own $pkg->can(...) which returns a coderef or undef. But perhaps it's not so great.

In addition, the API shape suggested by #44 leads to an alternative form of fetching metasymbols directly, by doing things like

my $metavar = meta::variable->get('$some::package::variable');
my $metavar = meta::variable->get($pkgname, $varname);

Under that style, using ->can would not work. Perahps instead take inspiration from https://metacpan.org/pod/Object::Pad::MOP::Class#try_for_class and use get_... vs try_get_...

my $metasym = $metapkg->get_symbol($name);
my $metasym = $metapkg->try_get_symbol($name);

That also works for the constructor-style ones:

my $metavar = meta::variable->get('$some::package::variable');
my $metavar = meta::variable->try_get('$some::package::variable');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions