DEV Community

Yuki Kimoto
Yuki Kimoto

Posted on

1

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

File::Basename::Unix

Translated with www.DeepL.com/Translator (free version)

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up