Skip to content

Instantly share code, notes, and snippets.

@benkasminbullock
Last active March 12, 2021 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkasminbullock/a55283a3b74aac94b8eedbf45e892260 to your computer and use it in GitHub Desktop.
Save benkasminbullock/a55283a3b74aac94b8eedbf45e892260 to your computer and use it in GitHub Desktop.
Regex to match Perl package syntax
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use utf8;
use FindBin '$Bin';
use Test::More;
my @stuff;
# package NAMESPACE
$stuff[0] =<<EOF;
package switch;
EOF
# package NAMESPACE VERSION
$stuff[1] =<<EOF;
package switch 0.001;
EOF
# package NAMESPACE BLOCK
$stuff[2] =<<EOF;
package switch
{ }
EOF
# package NAMESPACE VERSION BLOCK
$stuff[3] =<<EOF;
package switch 0.001
{ }
EOF
my $gokeyw =<<EOF;
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
EOF
my $re = qr!package\s+.*?\s*(;|\{)!;
for (@stuff) {
ok (m!$re!);
}
ok ($gokeyw !~ m!$re!);
done_testing ();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment