Uri Guttman wrote: > >>>>> "NC" == Nicholas Clark <[EMAIL PROTECTED]> writes: > > NC> How come there seems to be no way to specify mandatory named > NC> parameters? I'm not sure that *I*'d ever want to write > > apoc6: > A hash declaration like *%named indicates that the %named hash > should slurp up all the remaining named arguments (that is, > those that aren't bound explicitly to a specific formal > parameter). > > that tells me that you can use named arguments to fill in the required > positional (formal) params.
Yeah, I read that as saying that *%named will slurp up any named ('looks like a pair') arguments that have names not explicitly specified by the signature parameters, but I don't *think* that implies anything about whether or not the names you _do_ specify are actually required to exist. So I have to agree, my reading of A6 is that named parameters are _always_ treated as optional... the only way to specify _required_ parameters are as positionals. Design team, help? > and in the calling section it says: > > After the positional argument part, you may pass as many named > pairs as you like. These may bind to any formal parameter named > in the declaration, whether declared as positional or > named. However, it is erroneous to simultaneously bind a > parameter both by position and by name. Perl may (but is not > required to) give you a warning or error about this. > > so it seems you can pass only named args and if you don't pass in enough > to fill all the required positional args, it can be an error. it could > even be a compile time one since the named args canbe checked against > the names of the positional params. So you can only specify a named argument by name, but you can specify a positional argument either by position or name. So yes, technically you _can_ pass a required (positional) parameter by name, but you wouldn't be _required_ to pass it by name. sub foo($pos, +$zap) {...} foo('a', zap => 'b'); # OK foo( pos => 'a', zap => 'b'); # OK too foo( zap => 'b', pos => 'a'); # OK foo('a', 'b'); # ERROR; $zap must be named, not positional foo( zap => 'b' ); # ERROR; $pos is not bound (?) foo( zap => 'b', 'a' ); # ERROR; wrong way round foo( 'a', pos => 'a'); # ERROR; simultaniously bound by position and name foo( 'a', zap => 'b', zap => 'c' ); # ? (Not sure about the exact error messages on those ones... I'm hoping they can be descriptive, and that the second error there doesn't just try and assign the string 'zap' to $pos.) So my reading is that it's not possible to specify 'required' named parameters, unless we're completely missing something. My first impulse is that it's not a big deal, because who's gonna do that? My second impulse is... well... if I were writing a big library with big, hairy methods, and wanted to *require* the use of names for all params because the signatures are just too baffling otherwise, I can sortof see wanting that... If, for example, you are a purist who believes _all_ sub parameters should be named, we might want Perl to nonjudgementally support that particular neurosis. Bleah, dunno. MikeL