in reply to Re: Subroutine Bewilderment
in thread Subroutine Bewilderment
AFAIK there is now no type of subroutine that MUST be called with a leading ampersand or without comma's seperating the argumentshmmm ... I could have emphasized more the 'definition' I was considering for MUST and MUST NOT. In other words, think of it like a kind of puzzle, can I write code in such a way that violating the rule will cause an error or a warning?. Then, if you find an answer to the puzzle, consider whether the code is likely to happen in the 'real world' ... if 'yes', then you have a candidate for MUST and MUST NOT. I have found this approach to be useful because it essentially asks "how many different ways can I (legally) say the same thing?"
For example, here is code that illustrates the ampersand 'rules'.
### init use strict; use warnings; ### ampersand variants fooFunc (1,2); ### ok, ampersand optional &fooFunc(1,2); ### ok, apersand optional #foneFunc(1,2); ### NOT ok, called too early &foneFunc(1,2); ### ok, ampersand required #fwoFunc(1,2); ### NOT ok, too early &fwoFunc(1,2); ### ok, ampersand required print "\n--------------------------\n"; sub fooFunc { if($_[0]){print "fooFunc arg0: ". $_[0] ."\n"} if($_[1]){print "fooFunc arg1: ". $_[1] ."\n"} print "-------------\n"; } sub foneFunc() { if($_[0]){print "foneFunc arg0: ". $_[0] ."\n"} if($_[1]){print "foneFunc arg1: ". $_[1] ."\n"} print "-------------\n"; } sub fwoFunc($$){ if($_[0]){print "fwoFunc arg0: ". $_[0] ."\n"} if($_[1]){print "fwoFunc arg1: ". $_[1] ."\n"} print "-------------\n"; }
As far as a subroutine call where separating commas MUST NOT be included, consider a user-defined subroutine that works like map CODEBLOCK ARRAY putting a comma between CODEBLOCK and ARRAY causes error.
Note that we don't run into problems until we start introducing prototypes, which generally people say to "stay away from", which is fine, but with caveats...
- For someone used to programming in JavaScript (for example) it's not entirely obvious that fooFunction() is anything more than just a bare declaration of a subroutine name (as opposed to a 'prototype' embodied in the empty parenth).
- You can only 'stay away' from something for so long, and then finally you want to become one of those mythical people who 'really really really know what they are doing'
- It's interesting to know whether the 'stay away' items are either 1) legacy stuff that is going to be deprecated, or 2) advanced stuff that will remain for those who wish to delve deeper.
- It seems like some 'stay away' items that are advanced, overlap with 'stay away' items that are to be deprecated.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Subroutine Bewilderment
by Joost (Canon) on May 29, 2004 at 23:40 UTC | |
by doom (Deacon) on May 31, 2004 at 07:00 UTC |
In Section
Seekers of Perl Wisdom