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

Re: Overload abuse or is it just me?

by sfink (Deacon)
on Mar 18, 2006 at 17:32 UTC ( [id://537685]=note: print w/replies, xml ) Need Help??


in reply to Overload abuse or is it just me?

I agree with what everyone else has said about this particular topic, but I also think that if you look at it from a wider perspective, then the conclusion isn't quite so straightforward.

By overloading operators to be so far from their regular meanings, FEAR::API has created a "walled garden" in which you have to think and do things differently. It is essentially a "little language" -- inheriting from but different than Perl -- that may be used for performing a certain type of task. In this case, the type of task is site scraping.

Would you want this blended in with your regular Perl code that isn't primarily concerned with site scraping? Certainly not. It is changing the definition of fundamental things, and anyone writing or maintaining the code has to be aware of all the differences in order to understand what's going on. But if you want to write a seperable piece of an application that only deals with site scraping, then I think FEAR::API would be perfectly reasonable if you feel that the gain from using a slightly different language is enough to justify requiring you and your maintainers to learn that language. For the rest of the program, stick to Perl.

The particular examples shown are so easily rewritten in standard Perl that I would not feel that the invention of a new language was worthwhile. But that conclusion might be premature; I'd really have to read the documentation and try it out for nontrivial amounts of real code.

Moreover, I would assert that this situation can and does arise in many other areas, even when overloading is not involved. Templating systems are an even more extreme example of creating a new language tuned to a specific type of tasks. They overload far more than just operators, yet most people regard them as valuable additions to their toolboxes. I think people accept the burden of learning a new language because it's so much better suited to its task than straightforward Perl is. Similarly for IO::All, Inline, Parse::RecDescent, and many other modules (okay, a disproportionate share of these were written by Damian or ingy.)

Less extreme examples are even more common. What about CGI.pm? Consider the code from the SYNOPSIS:

use CGI qw/:standard/; print header, start_html('A Simple Example'), h1('A Simple Example'), start_form, "What's your name? ",textfield('name'),p, "What's the combination?", p, checkbox_group(-name=>'words', -values=>['eenie','meenie','minie','moe' +], -defaults=>['eenie','minie']), p, "What's your favorite color? ", popup_menu(-name=>'color', -values=>['red','green','blue','chartreuse'] +),p, submit, end_form, hr;

There is no overloading going on there, but simply having bareword "hr"s and "p"s scattered around code results in a very different look and feel from most Perl scripts. How is a beginner Perl maintainer to know that hr and p aren't built in to the Perl language? The language used in the above example is different than Perl, even though it's just Perl using a bunch of exported functions. And none of those functions would be any harder to rewrite in straight Perl than the overloaded operators in the OP were. But (some) people feel that switching to a different little language for generating HTML code is worth learning and dealing with the difference.

If you take it a little further, you'll see that this differs only in degree from using any CPAN module. Why force people to learn the LWP API when they could write HTTP code directly in native Perl? Answer: because learning to use LWP is a lot easier than understanding the masses of code required to implement the same tasks, and importing LWP changes very little about the language with which you would write the rest of the code.

I don't think anyone would disagree that if you're dealing with complex numbers, it's a lot nicer to have the basic arithmetic operators overloaded ($a = $b + $c rather than $a = [ $b->[0] + $c->[0], $b->[1] + $c->[1] ] everywhere). It's less clear whether using CGI.pm's HTML generation code is worth it, or whether the FEAR::API environment makes sense. My point is only that it's not just a spectrum from "a little weird" to "a lot weird"; you have to consider how isolated the language mutation is, as well as the task that you are using it for -- if it gives a lot of bang for that particular task, then it might be reasonable to use even if you would never get near it for any of your other code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found