Here's a metaprogramming solution... Instead of directly added regexps to the hash, we use a helper sub add_regexp to do it. add_regexp also does a little housework, including creating a sub for us (so there's no longer any need to use AUTOLOAD), and adding the name of that sub to @EXPORT_OK.

#!/usr/bin/env perl use strict; use warnings; BEGIN { package String; use Carp; our @ISA = "Exporter"; $INC{"String.pm"} = __FILE__; { my %reg; sub get_regexp { $reg{$_[1]} } sub has_regexp { exists $reg{$_[1]} } sub add_regexp { my $class = shift; my ($name, $re) = @_; croak "Already exists: $_[0]" if $class->has_regexp($name) +; $reg{$name} = $re; no strict "refs"; *{"is_$name"} = sub { !!($_[0] =~ $re) }; push our @EXPORT_OK, "is_$name"; } } __PACKAGE__->add_regexp(Url => qr[^http:]); __PACKAGE__->add_regexp(SecureUrl => qr[^https:]); }; use String qw(is_SecureUrl); print is_SecureUrl("https://github.com/"), "\n";

Or, because I like to pimp Type::Tiny...

#!/usr/bin/env perl use strict; use warnings; BEGIN { package String; use Type::Library -base; use Type::Utils; use Types::Standard qw(StrMatch); $INC{"String.pm"} = __FILE__; declare "Url", as StrMatch[qr(^http:)]; declare "SecureUrl", as StrMatch[qr(^https:)]; }; use String -all; print is_SecureUrl("https://github.com/"), "\n"; assert_SecureUrl("http://evil.com/");

In the spirit of full disclosure, I'll point out that I've changed your regexps slightly because Type::Tiny seems to have a few bugs compiling regexps containing slashes. I'll need to find a fix for that. :-/

Update: fixed in repo.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re: How to export un-exist subroutines by tobyink
in thread How to export un-exist subroutines by ryo0ka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.