String:...tePrefix(3)

πŸ’… NAME πŸ’…

Okay bestie, welcome to String::RewritePrefix(3) β€” the User Contributed Perl Documentation page that's about to become your new fave. It literally rewrites strings based on a set of known prefixes. That's the whole vibe. No cap.


✨ VERSION ✨

version 0.009 β€” she's giving established, she's giving stable. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

So picture this, right β€” you just use her:

use String::RewritePrefix;
my @to_load = String::RewritePrefix->rewrite(
  { '' => 'MyApp::', '+' => '' },
  qw(Plugin Mixin Addon +Corporate::Thinger),
);

# now you have:
qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger)

Obsessed. The plain ones get MyApp:: slapped on the front, and the + ones get their + stripped and go through untouched. Iconic behavior honestly.

And plot twist, you can also import a rewrite routine directly:

use String::RewritePrefix rewrite => {
  -as => 'rewrite_dt_prefix',
  prefixes => { '' => 'MyApp::', '+' => '' },
};

my @to_load = rewrite_dt_prefix( qw(Plugin Mixin Addon +Corporate::Thinger));

# now you have:
qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger)

Same result, but now you've got your own lil function name. Main character energy fr fr. πŸ’–


🌸 PERL VERSION 🌸

Okay so the tea on compatibility: this module should work on any version of perl still receiving updates from the Perl 5 Porters. Which lowkey means it should work on any version of perl released in the last two to three years. (Like, if the most recently released version is v5.40, then this module should work on both v5.40 and v5.38.) Slay.

And here's the fine print, bestie, read it carefully:

Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl.

So don't come for her about your ancient perl, okay? 😭


πŸ‘‘ METHODS πŸ‘‘

πŸ”₯ rewrite πŸ”₯

String::RewritePrefix->rewrite(\%prefix, @strings);

This rewrites all the given strings using the rules in %prefix. The keys are the known prefixes, and the values are what gets substituted in their place. The whole thing is performed in longest-first order, and only one prefix will be rewritten. Periodt.

And the cute part: if the prefix value is a coderef, it will be executed with the remaining string as its only argument β€” and the return value gets used as the prefix. Chef's kiss. 🫢


⭐ AUTHOR ⭐

Ricardo Signes <cpan@semiotic.systems> β€” the icon, the legend. πŸ‘‘


🌈 CONTRIBUTORS 🌈

  • Florian Ragwitz <rafl@debian.org>
  • Olivier MenguΓ© <dolmen@cpan.org>
  • Ricardo Signes <rjbs@semiotic.systems>

Squad goals. πŸ’œ


This software is copyright (c) 2022 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Reuse rights, we love to see it. ✨


And that's the whole page, bestie! πŸ¦‹ This lil masterpiece comes to you courtesy of perl v5.42.0, dated 2023-01-01 β€” and yes, it's still the String:...tePrefix(3) page, she just got a glow-up. Mwah. πŸ’…πŸ’–

πŸ“¦ where this page comes from