Slash Boxes
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 ]

ethan (3163)

  reversethis-{ed. ... rap.nov.olissat}

Being a 25-year old chap living in the western-most town of Germany. Stuying communication and information science and being a huge fan of XS-related things.

Journal of ethan (3163)

Saturday August 23, 2003
04:19 AM

some things are even hard in Perl

[ #14278 ]


        print "foobar"->gsub( qr/.(.)/, sub { shift() . $1 x 2 } );

This is essentially what Ruby's gsub() does. So the function reference should receive one parameter (namely the string matched by the pattern) and its return value will replace the matched string (for each occurance).

For obvious reasons, usage of $& strictly forbidden.

Timelimit: 5 minutes.

Storage-complexity of resulting Perl code: O(1 line + $c) with 0 <= $c >= 4.

Would you guess that it took me about two hours and several miserable attempts (most of them involving lots of substr() and pos() operations) to do that? And even then, none of my solutions allowed to access $DIGIT directly in the function (I had to pass them as parameters).

The correct (I hope) solution however looks marvelously simple and elegant. You certainly can't guess from it how long I fought with it:

        sub gsub {
                my ($string, $p, $s) = @_;
                $string =~ s{$p}{ $sub->(substr $string, $-[0], $+[0] - $-[0]) }ge;
                return $string;

Nonetheless, the idea of matching patterns iteratively and thus be able to hook functions into it is solver better in Ruby. Perl's s//CODE/ge is not quite so nice. Things could improve once $& becomes a variable without its current shortcomings.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Grubbing around the blead reveals that if you enable copy on write when building blead it will use it for $`, $& and $'. Which, one presumes, will rectify the matter to some degree. Right?

      ---ict / Spoon
    • I heard contradictory things about COW so I am not sure. Isn't Nicholas the guy behind COW? He might know.

      Meanwhile, I thought I could use tie() to emulate $&. It works with a tied hash actually (a scalar wont work), where the key is taken as the string against which the match was carried out:

      #! /usr/bin/perl -w

      package Tie::MATCH;

      use Carp;

      sub TIEHASH { bless \my $dummy => __PACKAGE__ }
      sub STORE { croak "Variable is read-only" }
      sub FETCH { return substr $_[1], $-[0], $+[0] - $-[0] }