DEV Community

Yuki Kimoto - SPVM Author
Yuki Kimoto - SPVM Author

Posted on

SPVM::File::Basename is released. This is the first module of SPVM using regular expressions.

SPVM::File::Basename has been released. SPVM::File::Basename is a porting of Perl's File::Basename to SPVM.

It supports the fileparse, basename, and dirname methods.

Regular Expressions

SPVM::File::Basename is the first module of SPVM that uses regular expressions.

Many Perl modules rely on regular expressions. In particular, it is safe to say that modules that deal with file paths always use regular expressions.

My concern was about a reliable Perl-compatible regular expression for creating modules in SPVM. At first, I created my own, but they were not very reliable. There are limitations to creating them alone.

I searched for I found that there is a Perl compatible regular expression called Google RE2. It is written in C++, and with Google RE2, I can use Perl-compatible regular expressions as a library.

I created a module called SPVM::Resource::Re2 to make it available from SPVM.

Then, I created a module called SPVM::Regex to implement the regular expression methods of Perl's search and replace.

And the first module that uses SPVM::Regex in practice is SPVM::File::Basename, which handles file paths.

Please take a look at the code that handles regular expressions in SPVM.

use Regex;

  method fileparse : string[] ($path : string) {
    unless ($path) {
      die "The \$path must be defined";

    my $dirpath = (string)undef;
    my $basename = (string)undef;

    my $re = Regex->new("(?s)^(.*/)?(.*)");
    if ($re->match($path)) {
      $dirpath = $re->cap1;
      $basename = $re->cap2;

    unless (length $dirpath) {
      $dirpath = "./";

    return [$basename, $dirpath, ""];
Enter fullscreen mode Exit fullscreen mode


Translated with (free version)

Top comments (0)