> > I have a function that takes a fairly complex set of arguments, and am > wondering if there is a better way of doing this than the way I have > done. A call to the function, as I have it, looks like: > > $network->potentiateWeights('network',1,(0,0,0, > 1,2,3, > 3,2,2)); > > where the list at the end is actually a set of 3 triples. To make it > clearer, the function turns those parameters into the following XML > (which is pretty much its job): > <PotentiateWeights change="1" net="network"> > <Weight src="0" dest="0" layer="0" /> > <Weight src="1" dest="2" layer="3" /> > <Weight src="3" dest="2" layer="2" /> > </PotentiateWeights> >
Two thoughts for me come immediately to mind. First, use named parameters rather than ordered arguments. That will allow you to check immediately if all of the needed options have been passed in. Second, for your triples I would use array references, and for the set of 3 triples I would use an array reference as well. So my call looks like, $network->potentiateWeights('net' => 'network', 'delta' => 1, 'weights' => [ [0,0,0], [1,2,3], [3,2,2], ], ); > The arguments are processed using: > sub potentiateWeights { > my $self = shift; # for the OO-ness > my $net = shift; > my $delta = shift; > my (@src, @dest, @layer); > while (@_) { > push @src, shift; > push @dest, shift; > push @layer, shift; > } > > Now, this works quite well, but doesn't seem all that right. I'd like to > know how someone who had been using the language more than I would > handle this situation. In particular, there is no way (excluding runtime > checking) to ensure that there are the appropriate number of triples in > the list. > > Thoughts? > Working is always a good thing, don't lose sight of that. Given my changes above you need to check that your required parameters exist, then you need to check that the 'weights' is an array ref (perldoc -f ref) and that each of the elements of that array are array refs with 3 values. Which goes a little something like --UNTESTED!-- sub potW { my $self = shift; my %args = @_; foreach my $key ('net','delta','weights') { unless (defined $args{$key}) { #return missing arg error; } } unless (ref($args{'weights'}) eq 'ARRAY') { # return weights not array ref error } foreach my $array (@{$args{'weights'}}) { unless ((ref($array) eq 'ARRAY') && (@$array == 3)) { # next or return invalid weight list } } # process which could also be done in the above foreach } In this manner you always know how to manipulate the weights (once the parameter checking is done) and adding an additional parameter will not mess anything up in the ordering, which is always a nightmare, especially since as it stands you are passing *a lot* of arguments in a single flat list. For some light reading, perldoc perlreftut perldoc perlref perldoc perllol perldoc perldsc Now, having said all of that, and knowing your end goal I wouldn't bother at all, but would instead look at XML::Simple to handle all of my XML processing (until getting really advanced). Then all you need to do is build a data structure in normal Perl and pass it to XMLout and voila beautiful XML. HTH, http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>