Hi,
This has very little practical value but I wonder if it is possible to hack Perl into using a pseudo object-oriented style for the built-in functions that deal with arrays. That is, in stead of saying

reverse @array;

you could say

$array->reverse();

rather than doing

sort &\func, @myArray;

you would have:

$foo->sort(\&func);

And so on. More formally, for all sub-routines in the package %CORE:: that pass one array by reference and take zero or more other arguments, make it an equivalent method call where the array is the object on which to operate but otherwise the parameter order is intact.

I'd been wishing for this kind of "silly" feature when experimenting in C but didn't come up with anything useful based on macroes at least. THen I saw an app-specific, obscure scripting language called FraggleScript which actually had the basic idea as a generic language feature, though it didn't support OOP at all. That is:
param1.function(...)
being exactly equivalent to
function(param1, ...)


My first thought in Perl, though, was to attempt source filtering. But it is all too easy to filter half-heartedly and end-up destroying some language features in the process i.e. not allowing chained method calls, if Perl supports them. I must confess I only toyed with the idea without even trying the implementation just yet.

My second port of call was to make a minimal object, a blessed array, and use its autoload function, err method, to forward the method calls to the built-ins. that is, determine the function name, get its prototype, re-arrange the parameter list and call. I've succesfully used autoloading before, to create a logarithm function which takes the base in the function name e.g. log2(argument).

I went and read about symbol tables and such and again was awe struck by how simple Perl makes things. Coming from a Java background, reflection still feels sort of magical and I was happy to discover that Perl treats even symbol tables as hashes. Again, lack of abstraction feels wonderful when programming in the small.

Anyway, I found the package CORE:: in all caps and it had a member GLOBAL::. Then I tried listing the keys of %CORE::GLOBAL:: but got neither data nor functions out. I'm certain that there's a perfectly sensible explanation for this but I haven't figured out what that might be. Maybe they want to protect you from overriding the built-ins, which would be a very unperlish thing to do, in my view at least. Compare to the ease with which you can change almost everything in Ruby, unless I'm much mistaken.

Well, not being able to iterate the built-ins hasn't been my only problem. I tested the prototype function and it seems many of the built-ins are highly magical, too. While most of the string functions appear to have prototypes, many that handle array references and blocks, such as grep, map and sort, cannot be represented as a prototype at all, I think.

If all else fails, I reckon I could mmirror the built-ins in my class and arrange all the parameters for the functions manually. But creating one method per built-in is certainly not the lazy way and it wont' automatically scale should new Perl versions come out. I doubt they'll be changing the built-ins any time soon but the manual method still seems the least interesting to me.

Mind you this Perl hack is not because I'm an OOP advocate or want to achieve something useful. Rather, I'd like to know Perl better and have some fun in the process.

So, are there any strategies I might have overlooked or is this pseudo-OOP syntax simply something unreachable (without doing func specific work)? Also, any tips or general help would be appreciated as usual.

PS: I reckon it is ok to ask this kind of question, even though this is a Win32 list.

--
With kind regards Veli-Pekka Tätilä ([EMAIL PROTECTED])
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to