Ask a Question related to PERL Beginners, Design and Development.
-
Robin Sheat #1
style of functions with complex arguments
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>
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?
cheers,
--
Robin <robin@kallisti.net.nz> JabberID: <eythian@jabber.org>
Hostes alienigeni me abduxerunt. Qui annus est?
PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFAGdFYzTjgendttmMRAmlcAJ9Sy2mi+71fd4ual9kL2D YJOCD0LwCdEfFm
A5A1agdrWP8boglGavpY7qQ=
=Z6o5
-----END PGP SIGNATURE-----
Robin Sheat Guest
-
ASP Functions & Style Sheets
Can anyone tell me if it's possible to attach style sheets to functions. <% Function answerTypeA(A) If A = 1 then A = 'Never' elseIf A = 2 then ... -
Need help with Style conversion from Style object to Style key/value collection.
I am writing a custom control that derives from the DataGrid control. I would like to apply SelectedItemStyles and ItemStyles for use in my derived... -
passing arguments to functions
Hi all Problem: I want to send 2 arguments to a subroutine in the form of arrays and want to use their result which is also in the form of an... -
Number format German style => English style when importing CSV files into MySQL-DB
Hi there, I'm trying to import a csv file into my MySQL database. Unfortunately the number format for the price column is formatted in German... -
#10743 [Com]: class functions & PHP core functions inconsistently clash ;)
ID: 10743 Comment by: destes at ix dot netcom dot com Reported By: jjones at net-conex dot com Status: Open... -
Wiggins D Anconia #2
Re: style of functions with complex arguments
>
Two thoughts for me come immediately to mind. First, use named> 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>
>
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],
],
);
Working is always a good thing, don't lose sight of that. Given my> 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?
>
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,
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Jeff 'Japhy' Pinyan #3
Re: style of functions with complex arguments
On Jan 30, Robin Sheat said:
[snip]>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));Here, you could make sure that @_ % 3 == 0, which insures that triplets>sub potentiateWeights {
> my $self = shift; # for the OO-ness
> my $net = shift;
> my $delta = shift;
> my (@src, @dest, @layer);
were indeed sent. But, based on your code below:
since @src = (0,1,3), @dest = (0,2,2), and @layer = (3,2,2), wouldn't it> while (@_) {
> push @src, shift;
> push @dest, shift;
> push @layer, shift;
> }
make more sense to pass the triplets as array references like so:
$network->pW(network => 1, [0,1,3], [0,2,2], [3,2,2]);
sub pW {
my $self = shift;
my ($net, $delta, $src, $dest, $layer);
# and then deal with @$src, @$dest, @$layer
}
That'd be my strategy.
--
Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
Jeff 'Japhy' Pinyan Guest
-
Robin Sheat #4
Re: style of functions with complex arguments
On Fri, Jan 30, 2004 at 07:40:06AM -0700, Wiggins d Anconia wrote:
I looked at this a bit more after writing the email, and ended up> 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,
changing my code to use array references, as that made something else
much easier (the subtraction thing that I mentioned (but messed up a
little) in another email).
I think I'll look into the named params, also. However, they do
introduce extra validity checking.
I am already using XML::Simple :) the function I described is basically> 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.
part of an API, it uses XML::Simple to create the XML based on the
parameters given. I want the function there as that is something that I
will be doing a lot, and don't want to create the XML directly in the
main program. (I'm using Perl as a fast-writing scriptable interace to
my neural network, and have a module that is effectivly an API to the
Java code, via XML).
cheers,
--
Robin <robin@kallisti.net.nz> JabberID: <eythian@jabber.org>
Hostes alienigeni me abduxerunt. Qui annus est?
PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFAGnNCzTjgendttmMRAtc6AJ90xMzPNsb24fVoIeLI4q 7OdIYCIgCgiBa8
35qmmMwUW7+CVblx5zmDol4=
=diFJ
-----END PGP SIGNATURE-----
Robin Sheat Guest



Reply With Quote

