Skip to content

Instantly share code, notes, and snippets.

@kirklewis
Created February 24, 2020 11:01
Show Gist options
  • Save kirklewis/cec92c16ec94d334e520d0ea51b443b6 to your computer and use it in GitHub Desktop.
Save kirklewis/cec92c16ec94d334e520d0ea51b443b6 to your computer and use it in GitHub Desktop.
A Perl module Regular Expression.
use v5.24;
use Data::Dump qw(pp);
use constant PERL_MODULE_REGEX => qr/\w+:{2}[\w:]+/;
use constant PERL_USE_MODULE_REGEX => qr/use\s+(${\PERL_MODULE_REGEX})/;
use constant PERL_PACAKGE_MODULE_REGEX => qr/package\s+(${\PERL_MODULE_REGEX})/;
my $str = <<'MODULES';
use My::ACME;
use My::ACME::Imported;
use My::ACME::Imported::Time;
package My::ACME;
package My::ACME::Package;
package My::ACME::Pacakge::Time::Moo;
my $timer = My::ACME::Thing::BeingUsed->make_timer();
# will match this because it won't compile anyway. This is not a concern for the Regular expression!
my $bad_namespace1 = My::Bad:Namespace:Hmmm->wont_compile_anyway();
# will not match because a pair of colons (:) have not been seen first by :{2}
my $bad_namespace2 = My:Bad:Namespace:Hmmm->wont_compile_anyway();
MODULES
my @all_modules = $str =~ m/${\PERL_MODULE_REGEX}/g;
my @imported_modules = $str =~ m/${\PERL_USE_MODULE_REGEX}/g;
my @pacakges = $str =~ m/${\PERL_PACAKGE_MODULE_REGEX}/g;
say 'All Modules', pp \@all_modules;
say 'Imported Modules:', pp \@imported_modules;
say 'Pacakge Declarations:', pp \@pacakges;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment