Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

colomon (8994)

colomon
  (email not shown publicly)

Journal of colomon (8994)

Sunday February 22, 2009
06:26 AM

My First Useful Perl 6 Script!

[ #38528 ]
I wanted to extend TextMate to do a case-aware search-and-replace. I decided to try it in Perl 6 because I wanted to use the :samecase modifier. Unfortunately, I was not able to get :samecase to work, but a simple modification made the script equally useful for me. I know this is mind-bogglingly simple, but it's now a practical part of my development environment for work.

#!/Users/colomon/tools/parrot-latest/languages/rakudo/perl6
my $search_word = @*ARGS.shift;
my $replacement = @*ARGS.shift;

for =$*IN -> $x
{
    my $y = $x.subst($search_word.lc, $replacement.lc, :g);
    my $z = $y.subst($search_word.uc, $replacement.uc, :g);
    say $z.subst($search_word.lc.ucfirst, $replacement.lc.ucfirst, :g);
}

I'll probably tweak this with some further improvements, including one that :samecase couldn't have handled anyway. But I just wanted to celebrate my first practical Perl 6 script!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • $x, $y, $z?

    Really?

    • Eh, might be pathetic, but for a straightforward seven line script it's good enough. I wouldn't have used anything but $x if $x hadn't been read-only. I'm pondering making it about twice as complicated, and if I do, there will be better variable names, I promise.