in reply to passing arguments

I think that many monks replied here pay too much attention to realization of concrete fake example and hmm, yeah, too categorical tell about "more than one argument".
As far as I understand, all of this is not very important.

The only important thing is idea. Why not meditate on idea, not on realization?

And idea, as I understand it, is making program based on data structures used in this program instead of actions required from this program. Yes, I agree, this is much more like OOP. And in fact we have three (and may be more) different designes of how to write program:

  1. logic oriented
  2. data oriented
  3. object oriented
And I sure, all of them is more usable than other in some tasks.

So, the question to mstone: describe please reallife task where your style give advantages.

P.S. I have a plan to make post in Meditations how style like this one used by me can help in writing CGIs.

Replies are listed 'Best First'.
Re: Idea vs realization
by demerphq (Chancellor) on Apr 25, 2002 at 17:02 UTC
    I can give you an example where you are going to have a hard time with only using one parameter.

    I want you to write a rotuine that only takes one parameter that will allow me to determine is several scalars are the same entity or not.

    So I have

    my ($x,$y,$z); $x='a'; $y='b'; $z='c';
    I want to be able to take any selection (including duplicates) and determine the number of distinct values. Thus
    Input Output. $x,$y,$x,$y => 2 $x,$y,$z => 0 $x,$x,$x => 1
    Oh, dont bother trying to hard to do this with just one parameter. It cant be done. :-)

    And if that isnt a red rag to a bull then I dont know what is.

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      Aggregation is a wonderful thing: ;-)
      sub find_unique_items_in { my $set = shift; my (%uniq, @out) = (); for $i (@$s) { $uniq{ $i }++; } push @out, sprintf ( "%d unique entities (%d duplicates):\n", scalar keys %uniq, scalar ( grep { $uniq{$_} > 1 } keys %uniq ), ); for $i (sort keys %uniq) { push @out, sprintf ("%-16s -- %d", $i, $uniq{$i}); } return (join ("\n", @out)); } @list = (0) x 10; for (1..10) { $set = []; push @set_list, $set; for $i (0.. rand(5)+10) { push @$set, \$list[ rand @list ]; } } for $s (@set_list) { print "\n", '-'x72 ,"\n\n"; print join ("\n", @$s), "\n\n"; print &find_unique_items_in ($s), "\n"; }
      Not only does it keep your parameter lists trim, it lets you store your data sets so you can use them again.

      Now, I don't pretend that the above is an exhaustive search for matching entities, but as far as I know, it's fundamentally impossible to create a parameter list that can't be stored as a data structure. And thinking about those data structures -- especially the possibility of using them again -- helps you build well-organized code.

        Hmm. Interesting solution.

        Turns out though that you should have developed this under strict. There is a minor bug that prevents your routine from running if you dont use your exact perparation code. The problem is that what you pass to the routine never gets used (you do $set=shift; but then iterate over @$s, which just happens to be defined as a global so it works out fine in your sample...) Anyway I fixed that and removed some of the extraneous code. See below.

        My problem with this is that you have to preconstruct an array containing references to the scalars in question, which I dont really think meets the letter of my specification. Also the proconstruction is pretty cumbersome. Anyay heres your revised code, mine and some output.

        use Data::Dumper; use strict; sub count_unique { print Dumper \@_; my %uniq; $uniq{0+\$_}++ foreach @_; return sprintf ( "CTU: %d unique entities (%d duplicates):\n", scalar keys %uniq, scalar ( grep { $uniq{$_} > 1 } keys %uniq ), ); } sub find_unique_items_in { my $set = shift; print Dumper $set; my (%uniq, @out) = (); for my $i (@$set) { $uniq{ $i }++; } return sprintf ( "FUI: %d unique entities (%d duplicates):\n", scalar keys %uniq, scalar ( grep { $uniq{$_} > 1 } keys %uniq ), ); } my ($x,$y,$z)=qw(a b c); print count_unique($x,$y,$z),$/; print find_unique_items_in([\$x,\$y,\$z]),$/; print count_unique($x,$y,$y),$/; print find_unique_items_in([\$x,\$y,\$y]),$/; print count_unique($x,$x,$y,$y),$/; print find_unique_items_in([\$x,\$x,\$y,\$y]),$/; print count_unique($z,$z,$z,$z),$/; print find_unique_items_in([\$z,\$z,\$z,\$z]),$/; print count_unique($x,$x,$y,$y,$z,$z),$/; print find_unique_items_in([\$x,\$x,\$y,\$y,\$z,\$z]),$/; __END__ $VAR1 = [ 'a', 'b', 'c' ]; CTU: 3 unique entities (0 duplicates): $VAR1 = [ \'a', \'b', \'c' ]; FUI: 3 unique entities (0 duplicates): $VAR1 = [ 'a', 'b', ${\$VAR1->[1]} ]; CTU: 2 unique entities (1 duplicates): $VAR1 = [ \'a', \'b', $VAR1->[1] ]; FUI: 2 unique entities (1 duplicates): $VAR1 = [ 'a', ${\$VAR1->[0]}, 'b', ${\$VAR1->[2]} ]; CTU: 2 unique entities (2 duplicates): $VAR1 = [ \'a', $VAR1->[0], \'b', $VAR1->[2] ]; FUI: 2 unique entities (2 duplicates): $VAR1 = [ 'c', ${\$VAR1->[0]}, ${\$VAR1->[0]}, ${\$VAR1->[0]} ]; CTU: 1 unique entities (1 duplicates): $VAR1 = [ \'c', $VAR1->[0], $VAR1->[0], $VAR1->[0] ]; FUI: 1 unique entities (1 duplicates): $VAR1 = [ 'a', ${\$VAR1->[0]}, 'b', ${\$VAR1->[2]}, 'c', ${\$VAR1->[4]} ]; CTU: 3 unique entities (3 duplicates): $VAR1 = [ \'a', $VAR1->[0], \'b', $VAR1->[2], \'c', $VAR1->[4] ]; FUI: 3 unique entities (3 duplicates):
        but as far as I know, it's fundamentally impossible to create a parameter list that can't be stored as a data structure.

        And my point is that this is wrong. The parameter list @_ is magic. It contains aliases to the items passed. A normal array contains _copies_ of the items passed. The only way that you can simulate this behaviour is to do the work around you did, which is to pass references to the items in question. But then that completely changes the data being passed and the code that must handle it. And all of this is clear from the dumper results in the code above.

        Anyway, good try. :-)

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

Re: Idea vs realization
by mstone (Deacon) on Apr 25, 2002 at 22:01 UTC
    GUI programming is the first example. Web applications also benefit, because they're a subset of the Model-View-Control architecture. Signal-based programs and multi-threaded programs also benefit from keeping the data isolated from the logic. So does any code that needs to be reentrant -- scripts written for mod_perl, for instance.

    Think of it this way: your function signatures are your program's internal communication protocol. So how good is your program's protocol? Is it structured and well-organized, or is it more like the transcript of half a dozen random bar conversations?