in reply to A question of style

This just appears to be poor code. While there are many reasons to pass references to subs, this should be written:
my $response=doSomething(); sub doSomething { ... # fail return 0; }
This is assuming that the ... doesn't hide code that would actually cause me to consider passing a reference.


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re: Re: A question of style
by Scarborough (Hermit) on Mar 30, 2004 at 16:44 UTC
    In the real inherited programs as many as ten refs are passed to the subs. I could understand if they where all being changed, but often it is only the response which gets changed.
    If I where writing from fresh I'd be with you on the return way of doing things. Thanks for confirming my feelings that this is just poor practice.

      Such code smells like a C programmer who couldn't stand to program without memory pointers and grabed the first thing in Perl that looked anything like it. Screwing with pointers is perfectly acceptable in C, as most C programs are very concerned with efficiency. If you're using Perl, you're already giving up so much efficiency over pure C that you can usually ignore the passing style.

      If more than one refernce is being changed, then Perl is perfectly capable of returning multiple values as a list instead. For instance:

      my ($foo, $bar, $baz) = do_something(); sub do_something { return 1, 2, 3; }

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

      Long term, functions shouldn't take 10 parameters (that's ugly even in C...structures are a good way to fix this), and in Perl named variables (through use of hashes) are a good way to fix this.

      do_wacky_stuff( -alpha => 1, -delta => 'sputnik', -beta => 2, -gamma => 3, );

      Note order doesn't matter, and that's why it's cool. Use Params::Validate for added fun-ness.

      Also you mentioned inherited code being the root of all your problems. Such is life! My handwritten-from-scratch code doesn't have any bugs! SCENE 42: flyingmoose is striken dead by a lightingbolt from the heavens, onlookers seem puzzled as he was indoors!

      oming from a more formal (ADA, Java) background I would favour a return

      Perl is as formal as you make it. I prefer to call those languages 'whiney' not 'formal'. Why? Well you can just as easily screw up a Java app, and it takes discipline no matter what you do. Other languages just want to hamper your productivity more than others :)