raptor has asked for the wisdom of the Perl Monks concerning the following question:
Instead of something like this :sub ($self,$var,@arr) { }
this will save alot keystorokes ... yes I know this place is for prototipes.. but the vars are easly recognaized from prototypes ( /\$\w+/ ). what U think ?sub () { my $self = shift; my ($var,@arr) = @_; ... }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sub ($self,$var,@arr) {}
by MeowChow (Vicar) on Feb 09, 2001 at 15:57 UTC | |
Total Savings = 6 keystrokes (not including spaces) This wouldn't add new functionality, and saving a few keystrokes is less important, IMHO, than maintaining consistency. Understanding what @_ is, how it works, and how to use it is a Good Thing; it takes some of the mystery out of parameter passing, and is conceptually related to many other important Perlisms that beginners often struggle with (eg. $_, local, aliases). Offering Perl beginners an easy way out by not learning about @_ would be doing them a disservice. @_ allows for many interesting possibilities that fixed parameter lists do not (for instance, "overloaded" subs). It would also be unfortunate if Perl's worth were diluted by encouraging people to code like it's just another scripting language. My gut feeling is that if you were to add such a syntax, newcomers to the language would use it almost exclusively, and @_ would evolve into an unusual, depricated construct of interest primarily to obfuscators. And I don't want Perl to look any more like C, C++, or Java than it has to ;) Bad memories, you know? That said, your suggestion is in keeping with the idea that TIMTOWTDI, and I don't see how it could break backwards-compatibility or be difficult to implement. It would also add some sanity, from an aesthetic perspective, to Perl's prototyping system. So I don't quite know on which end of this issue I sit. Perhaps, I'll just offer the annoying cliche "if it ain't broke..." | [reply] [d/l] [select] |
by tadman (Prior) on Feb 09, 2001 at 22:54 UTC | |
Still, I find it nice that Perl allows you to use the parameters any way you wish, which is especially useful when you're passing parameters using the HASH-style methods, such that you really don't know how many parameters you are going to receive, nor what type they are: It is interesting that CGI.pm uses parameters in a way that is not unlike UNIX-style command-line arguments in terms of the way they are parsed and processed. The prototypes introduced in 5.6(?) go a long way towards ensuring that your module is used properly, and detecting errors in parameters vs. errors in the module: Although MeowChow doesn't "want Perl to look any more like ... C,C++ or Java", that is no reason to not borrow methods and practices from these languages that were implemented with solid reasoning. The cliché "if it ain't broke" certainly hasn't been heeded by the Perl developers, Larry Wall included, because Perl 4.0 was hardly broken, and just when you think it couldn't get any better, it did! | [reply] [d/l] [select] |
by raptor (Sexton) on Feb 13, 2001 at 16:38 UTC | |
prototypes are GOOD thing, but current implementation doesn't help US alot :") | [reply] |
|
Re: sub ($self,$var,@arr) {}
by mirod (Canon) on Feb 09, 2001 at 17:02 UTC | |
To expand on what MeowChow wrote, I have no objection if the current @_ mechanism is also kept. I like Perl's flexibility and here are some of the things you can do with the current parameter passing mechanism: | [reply] [d/l] [select] |
|
C-like Function Prototypes
by japhy (Canon) on Feb 09, 2001 at 20:12 UTC | |
Now, in Perl, we can do this ourselves in the function, by checking the contents of @_, since Perl doesn't differentiate between strings and numbers (or types of numbers) like C does. (But the practice of having a function do totally unrelated things is A Bad Thing, and lends itself to confusion.) How would you C-ize this Perl function? Maybe something like I find that hideous. I really think I would find any type of variable-based prototyping in Perl pretty ugly. japhy -- Perl and Regex Hacker | [reply] [d/l] [select] |
by tilly (Archbishop) on Feb 09, 2001 at 22:01 UTC | |
which does not seem to me to be significantly uglier than Perl. But mixing Perl and typing is IMNSHO utterly broken. It is often said that Perl does not differentiate between strings, numbers, etc. This is only somewhat true. Perl allows a variable to contain any of them, and then your code differentiates between them. Mixing this existing type system with anything else doesn't work. On a similar note, it is very hard in Perl to write generic code which will continue to work if people pass in strings, references to arrays, etc. It can be done, but you are fighting the language. In Ruby this is trivial. In fact the above code is perfectly generic as long as x is some type that supports a * method. (Overloading in Perl is possible, but not so transparent.) Now I am not saying that the way that Ruby does it is right or better. I am just saying that it is possible to support prototyping very similar to what raptor asked for without it being ugly and broken. I am also saying that Perl's claim of not forcing types on variables isn't completely true. But what Perl loses in the ability to write generic code, it gains in the fact that you can transparently extract numbers from strings, work with them as numbers, and then use them as strings again without needing to do explicit conversions. And 3/2 in Perl is not 1. PS: Ruby does not have a concept like @_. The closest it has to that is the ability to declare a parameter with a * which will gather remaining arguments into an array. If Ruby did have @_ then the conflict between the two modes of passing arguments would become much worse. Not coincidentally, Ruby is also not list oriented... PPS: I am not trying to advocate Ruby, merely show by comparison with another language that the suggestion can work. However I don't think it would work well in Perl for a number of reasons. First is the confusion with the existing idea of prototypes. Second we have the conflict with the existing notion of @_. Thirdly there would be questions about pass by value versus pass by reference. | [reply] [d/l] |
by raptor (Sexton) on Feb 13, 2001 at 16:29 UTC | |
Later : instead of : my 5c | [reply] [d/l] [select] |
by McD (Chaplain) on Feb 09, 2001 at 22:05 UTC | |
$y ||= 10; FWIW, be careful with that. Using ||= is one of my favorite ways to write fluent Perl, but it just got me burned a few days ago. The problem was that the $y I was initializing was a numeric value - but zero was an acceptable value. In that case, my ||= was overriding the supplied value of zero, which tested false against the operator. In the example here, providing an argument of 0 for $y will result in $y getting 10, not zero. Oops. :-)
Peace, | [reply] [d/l] [select] |
by arturo (Vicar) on Feb 09, 2001 at 22:11 UTC | |
#534987 in arturo's minor amplifications for the benefit of those who read the thread days later: For just those sorts of cases where false values are valid, $y =defined($y) ? $y : 10; (or whatever test makes sense). Philosophy can be made out of anything. Or less -- Jerry A. Fodor | [reply] [d/l] |
|
(jeffa) Re: sub ($self,$var,@arr) {}
by jeffa (Bishop) on Feb 09, 2001 at 19:58 UTC | |
Get in the habit of passing references around, much nicer to your RAM. Jeff
| [reply] [d/l] |
by raptor (Sexton) on Feb 13, 2001 at 15:57 UTC | |
just exapmle I'm not passing in this way.. And the people can use both ways, why not ..i.e. instead of : for me the first is much more clear... and can be used to document what the creator of this sub mostly expected like parameters.. with one RegEx U can extract a desc of methods and so on... this is JMO | [reply] [d/l] [select] |
|
Re: sub ($self,$var,@arr) {}
by extremely (Priest) on Feb 10, 2001 at 04:22 UTC | |
If the scalar causes a cast, you get 4, (6,7,8,9) otherwise you get 1, (2,3,4,6,7,8,9). And if it doesn't cast, what did it save you? -- | [reply] [d/l] |