(... And You Should Show Several In The Fine Documentation.)
I find this code seriously confusing:
my $proxy = HTTP::Proxy->new();
# create a new HTTP::Recorder object
my $agent = new HTTP::Recorder;
Besides the fact that indirect object syntax Just Doesn't Work according to a complex series of rules that almost no one can explain properly, I don't understand the purpose of mixing the direct and indirect constructor calls within two lines of code.
I'm all for TMTOWTDI if and when it allows people to choose the most effective approach for solving the particular problems they encounter, but mix and match coding styles within the synopsis of documentation baffles me. HTTP::Recorder is not the only offender; it's just the first one to catch my attention today.
Meh. (Score:1)
$bob = new Servant;$bob->fetch_me_a_drink('please');
I think "Blah->new" always reads badly, but that's a matter of personal taste.
Re: (Score:1)
I'm sure Perl would read as better English without the sigils or the punctuation characters too, but if the code doesn't work, it's difficult to justify using it for practical purposes.
Re: (Score:1)
Unless one's doing something pathological, the indirect syntax works. If someone really doesn't care about (relative) readability, they just use Lisp or Postscript or Smalltalk, where everything really is an X, for their favorite value of X.
Re: (Score:1)
How is one more readable than the other? Perl is an artificial language with artificial rules.
Re: (Score:1)
Re: (Score:1)
The indirect object syntax can break depending on which methods someone has declared in other namespaces.
It's hardly "pathological" to use namespaces.
In my mind, it's silly to hope that those collisions never occur. They're not easy to debug.
Re: (Score:1)
It's hardly "pathological" to use namespaces.
Save the hyperbole for UNIVERSAL::isa ;). Namespaces, like functions and variables, are programming language features that can be used in both beneficial and harmful ways.
The indirect object syntax can break depending on which methods someone has declared in other namespaces.
The case I think you have in mind is this:
which prints "ab". So the problem occurs only when (1) you use a function from B before it's declared, (2) you use indirect syntax, and (3) you declare a function of the same name in A. I think doing all
Re: (Score:1)
Have you used a large system written in Perl, such as Catalyst or Plagger, which supports plugins and loading schemes far different than bare
use? It's not always easy to know who loads what when, especially when you use generated and non-generated classes with similar naming styles.Point taken (Score:1)
Re: (Score:1)
You're right about points 1 and 2 as well. Still, I see the indirect object syntax as susceptible to breaking due to action at a distance. When you load a module that otherwise behaves as a properly-encapsulated black box, it may do things that cause the indirect object constructor calls to fail.
I can imagine otherwise-innocent changes to Test::MockObject that would do such a thing. (I'll never do them, unless there's no other way to accomplish something else, but I could justify that code except for t
WTF? (Score:1)
So, $obj = Classy -> new() is always correct.
If you don't get it,
Re: (Score:1)
Exactly, and sometimes for legitimate reasons. Plenty of normal things in Perl can fail if someone's being very tricky, but this is one of those corners where the parser needs to know some very precise information at the point of compilation. If that information isn't available, it has to guess, and there are several inferences that it can make incorrectly.
Inconsistent Documentation (Score:2)
I often see this when people are copying code directly from the documentation for a module. Beginners don't understand that
and
are just two ways of writing the same thing. They can be forgiven for assuming that classes that use the indirect notation for the constructor must be used that way as, almost certainly, they've never read anything that tells them otherwise. It's really the module authors who need to be educated into cleaning up their documentation.
Re: (Score:1)
I suspect that a lot of Perl programmers think that
newis an operator in Perl, as it is in other languages (such as C++ and Java).It doesn't confuse me that novice programmers copy and paste from synopses into code (that's why we have synopses in the CPAN POD form!). What confuses me is the juxtaposition of both forms of constructor call in the same section of code.
My other favorite (Score:1)