It's kind of surprising to me that this code exists in the "real world". Here's why. Any/All parameters passed to new() will find their way into the object's hash as key/value pairs. In my mind, that gives too much power to the caller, particularly for a public method (more on this later). It could even create potential issues down the road, similar to the kind of issues created by using the -s switch on the perl command line, wherein the script's invoker is able to create package globals in the script's main namespace simply by what is typed on the command line. ...ok, with the object oriented example you showed, the potential only exists to stamp on an individual object's internal namespace, but it still feels a little sleezy to me.
The other problem is that any arguments passed to new() must come in pairs (key/value pairs), or you'll get a warning (assuming warnings are in effect). ...This is probably a good thing if you're intentionally creating key/value pairs in the object's internal namespace. But it does, once again, feel a little sleazy. In Perl Best Practices, if I recall, the conventional wisdom about passing key/value pairs to a subroutine is to pass them as an anonymous hash, rather than as a bare list.
The last problem is that of legibility. You had to ask what it's doing. I had to think about it for a moment. It's not exactly clean, maintainable code. Clever, yes. But a little too clever.
Now for some redemption: As I read the documentation for Module::Starter::Simple, I see that the new() method isn't intended for public use; it's intended to be invoked as an internal method, called by create_distro(). So the fact is, you probably shouldn't ever really need to be concerned with new(), and unless you're simply trying to learn a little from the module's code, you don't really need to worry about the implementation. That being the case however, new() probably ought to be called _new()... another recommendation supported by Perl Best Practices for marking internal (non-public) methods clearly. Once again, at first look I wouldn't have guessed that new() was intended to be an internal-use-only method. The POD draws attention to the fact, but it would make sense for it to be named with the leading underscore, the traditional way of calling out internal methods.
Oh, you also asked about the => operator. Just think of it as a "fat comma". All it's doing in this case is acting as a comma. While it might be visually confusing in this context, I would guess that the module's author saw it as a clever way to visually demonstrate that the anonymous hash being blessed "belongs to" the class on the righthand side of the "comma arrow".
Dave
In reply to Re: What means: "bless { @_ } => $class;"?
by davido
in thread What means: "bless { @_ } => $class;"?
by hesco
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |