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 09, 2003
02:59 AM

porting Ruby's String class to Perl

[ #14015 ]

Not that Perl would be lacking string manipulation facilities, but this new feature proposal for (allegedly) perl5.10 simply cries for rubyisms::String:

        use autobox SCALAR => 'rubyisms::String';

        print " bla "->strip;

        my $foo = "bAR";
        print $foo;

Due to different lexer rules in Ruby I had to find a different nomenclature for string.strip! so I chose $string->stripI with 'I' being mnemonic for 'in-place' while maintaining some visual similarity to '!'.

So far done are capitalize, capitalizeI, strip, stripI and center. This will take another couple of days since it's being done in XS. After that I can finally run some benchmarks and see whether the XS approach makes up for some of the performance hits introduced by autobox (which I haven't yet benchmarked, either, so they might not even be severe at all).

For those being adventurous enough (and curious about how it feels like to have everything act like an object), grab perl5.8.1-RC4 and autobox from the CPAN, apply the patch, compile and have FUNNNNNN!

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.
  • See CPAN (Score:3, Interesting)

    by djberg96 (2603) on 2003.08.09 9:01 (#22935) Journal
    Too some degree that was my intention with Set::Array, Set::String and Set::Hash. I also used the Want module to make method chaining more sensical, passing $self if chained, or a value otherwise.

    Rather than using method vs methodI, I chose to use context to determine whether or not the receiver was modified, which seemed more Perlish.

    • Re:See CPAN (Score:2, Insightful)

      My first intention was to really duplicate Ruby's interface. On second thought, this might not be a good idea. One reason for that is that I am not yet sure whether is powerful enough. Also, I don't know what happens when you do a string.strip!.capitalize! in Ruby (that is, will string be capitalized and stripped in-place? If so, can this be done with autobox at all?)

      The main problem here is to determine whether something feels Perlish or not: Perl so far had no primitive objects and subsequentl
  • Have you seen Scalar::Properties?

    Seems to do some of what you propose.


    • No, actually I hadn't seen it. It seems to result in the same interface. Two differences though: One is the implementation. You use constant overloading and thus turn things into real objects. autobox on the other hand wont ever bless something. It invokes the method directly on the primitives (and thus wont suffer from the slowdown caused by blessed referents). Btw, it is lexically scoped, so it can be turned on and off selectively for blocks.

      Secondly, Scalar::Properties comes with its own methods whereau
  • Perhaps you might consider changing the module name to String::Ruby to force consistency with String::Rexx (I am the author). For other more specific ruby features related to strings, the naming could in sync with String::RexxParse, String::RexxStack (which will scheduled to reach cpan later next week). This way, i suspect it will be easier for visitors to find your module when they search for "String". Just a suggestion.
    • You are right, good suggestion. I don't like rubyisms::String very much either...I chose it because of Simon Cozen's rubyisms module.

      Hmmh, is there a rule that lexically scoped module usage has to relate to pragmata? I just implemented the import() and unimport() semantics that allow

                  use String::Ruby;
                  print "foo"->center(10);