style of functions with complex arguments

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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 ...
    2. 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...
    3. 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...
    4. 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...
    5. #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...
  3. #2

    Default Re: 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>
    >
    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,

    [url]http://danconia.org[/url]
    Wiggins D Anconia Guest

  4. #3

    Default Re: style of functions with complex arguments

    On Jan 30, Robin Sheat said:
    >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));
    [snip]
    >sub potentiateWeights {
    > my $self = shift; # for the OO-ness
    > my $net = shift;
    > my $delta = shift;
    > my (@src, @dest, @layer);
    Here, you could make sure that @_ % 3 == 0, which insures that triplets
    were indeed sent. But, based on your code below:
    > while (@_) {
    > push @src, shift;
    > push @dest, shift;
    > push @layer, shift;
    > }
    since @src = (0,1,3), @dest = (0,2,2), and @layer = (3,2,2), wouldn't it
    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

  5. #4

    Default Re: style of functions with complex arguments

    On Fri, Jan 30, 2004 at 07:40:06AM -0700, Wiggins d Anconia wrote:
    > 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,
    I looked at this a bit more after writing the email, and ended up
    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.
    > 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.
    I am already using XML::Simple :) the function I described is basically
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139