Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

RFC: Idiomless Perl

by bennymack (Pilgrim)
on Feb 02, 2007 at 02:33 UTC ( [id://597868]=perlmeditation: print w/replies, xml ) Need Help??

Greetings,

It's that time again. Time for another half-baked idea for a Perl extension. It's really not all that bad. In fact, I think it might be something that people may actually want to use.

That being said, like many of my ideas it's not really an idea but application of another programming language's paradigms to Perl. In this case, Ruby.

The reason I titled this "Idiomless Perl" is because it seems like it would be extremely useful to have a place to easily store snippets of code that are "idiomatic" and retrieved with as little extra effort as possible.

Enter "OO::All". It would define some basic types as objects at first. With a base Object that is the super class for Scalar, Array, Hash (to start). Each child class would be represented by an object which is a blessed version of the primitive it is named after. E.g. bless( [], 'Array' ), etc. With a little overloading one could treat the object as both a primitive and an object if desired.

A quick example of the usefullness of a such a module follows:

my $array = Object->new [ qw(1 .. 10) ]; my $array_length = $array->length;</p>

Which is effectively the same as:

my $array = [ qw(1 .. 10) ]; my $array_length = scalar @$array;</p>

Why you ask? Because keeping things like the correct context straight are "idiomatic". There are are so many other examples that show how useful this can be. Unfortunatetly this was the only one I could think of ATM.

I understand there are modules on CPAN to add this sort of functionality. Ideally the actual implementation will come from CPAN and it will be trivial to roll it into the objects.

Please don't take this as an OO fanboyism. It's really all about having somewhere convenient to store oft encountered snippets of idiomatic Perl. The more convenient it is, the more likely it is to be used, the less likely one might be to be burned by Perl idioms. I'm not scared of Perl idioms, don't get me wrong. I just think it would make code easier to write and get right as well as easier to read and understand.

The implementation of this I have in mind would be something that can be loaded all from one central location and then be available everywhere. It would of course utilize a mixin where all the subs from a given module could be added into any of the object classes.

Additionally, every method call (that I can foresee thus far) on these objects will return another object so they can easily be chained:

my $hash = Object->new { a => b }; print $hash->keys->count, "\n";

The keys method on Hash returns an Array whose count method returns a Scalar... Or there could just be a Hash method to return the key count but this is strictly illustrative.

Thanks for reading! Let me know what you think. I will try to put some more examples down in writing as I think of them.

Replies are listed 'Best First'.
Re: RFC: Idiomless Perl
by merlyn (Sage) on Feb 02, 2007 at 02:56 UTC

      Actually autobox itself only provides the means to do this, it provides no actual implementation for core types.

      For actual implementations there are two currently on CPAN. The first one was autobox::Core which attempts to map all the built in Perl functions as methods. Then came Moose::Autobox (which I wrote, so yes, I am biased) which tries to take an approach more akin to what Perl 6 might look like and build the core types out of roles.

      As for practical usage of any of these modules, I can't honestly say that I would use any of them in production. In my experience autobox is pretty stable, but it does seem to impose a noticable startup penalty and IIRC it (ab)uses an undocumented "feature" in the Perl core.

      Of course in the end, it comes down to how OO-pure do you want to be really? There is something to be said for a healthy dose of practical impurity :)

      -stvn
Re: RFC: Idiomless Perl
by TedYoung (Deacon) on Feb 02, 2007 at 16:04 UTC

    Because keeping things like the correct context straight are "idiomatic".

    Contexts, sigils, et al. are not idiomatic, they are fundamental concepts of the language. Just because they are not commonly found in other languages, doesn't mean they are hard, bad, or that they should be avoided. Not every language needs to be C-Style.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: RFC: Idiomless Perl
by Jenda (Abbot) on Feb 02, 2007 at 13:45 UTC

    I don't think I fully understand what do you want, but if you have an itch and want to scratch it, go ahead and write the module. I think you'll find out that you use it less and less as you are getting more comfortable with Perl and eventually you'll stop using it at all. If it makes the transition from a different language soother for you or others, it was not pointless.

Re: RFC: Idiomless Perl
by Trizor (Pilgrim) on Feb 02, 2007 at 21:30 UTC

    Do you have an easy way to chain maps and sorts and return an OO::Array in mind? Would someone using your framework be able to port older code containing such idioms with little effort?

    Would it be worth it to add this wrapping layer? What benefit stands to be gained to using $arrayobj->find($key) as opposed to mapping the list to a hash and testing a key?

    As TedYoung said in his earlier comment: Re: RFC: Idiomless Perl these things are core to perl and are difficult or pointless to wrap.

      Just to play Devil's Advocate:

      What benefit stands to be gained to using $arrayobj->find($key) as opposed to mapping the list to a hash and testing a key?

      It's not hard to imagine an implementation of $array->contains($key) (instead of "find", which I would hate) using grep or List::MoreUtil's indexes (which is XS, and therefore pretty fast). Examples:

      ## discover if an array contains a value, using grep sub contains { ## usage: $bool = $array->contains( $value ) # is $value in $array +obj? ? my ($self, $val) = @_; my $r_array = $self->{data}; # assume this contains the array ## for example, assume values are strings my $cnt = grep { $_ eq $value } @$r_array; return $cnt ? 1 : 0; }
      ## discover if an array contains a value, using List::Util::indexes; sub contains { ## usage: $bool = $array->contains( $value ) # is $value in $array +obj? ? my ($self, $val) = @_; my $r_array = $self->{data}; # assume this contains the array ## for example, assume values are strings my $cnt = List::Util::indexes { $_ eq $value } @$r_array; return $cnt ? 1 : 0; }

      This latter has the advantage that it's easily modifiable to return a list of indexes that match (since that's what indexes does already)

      Using map to cast an array to a hash and testing keys is only useful if you'll be checking for the existence of several values in an array that won't change between your tests. So, I think the real question should be what value does this wrapper provide over using Perl's built-in capabilities?

      I think the OP addressed that just fine -- it's syntactic sugar for those more familiar with OO-based languages who are trying to learn Perl, or who want to consistently use OO for everything for some reason.

      <radiant.matrix>
      Ramblings and references
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: RFC: Idiomless Perl
by muba (Priest) on Feb 06, 2007 at 03:52 UTC
    You speak of idiomatic, yet you go
    $array = [ qw(foo bar) ]; # huh: a reference? $length = scalar @$sarray # huh: @$?
    What's wrong with good old
    @array = qw(foo bar); $length = scalar @array;
    ?

      Useless use of scalar, for one, but that's in both examples.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://597868]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found