Nigel Peck wrote: > > Hi all, > > I'm hoping to get some ideas on how to write my subs, specifically on > passing variables into them and how best to do it. > > Currently I do this: > > > example_sub ( $some_scalar, $some_hash_ref, 'some string' ); > > sub example_sub { > my ( $passed_scalar, $passed_hash_ref, $passed_string ) = @_; > > > } > > > This is fine, except when I want to pass in something else at a later > date, when I find I need the sub to do something extra, I have to go > back and update all of the calls to the sub that I wrote before. > > So if I wanted to add $some_array_ref... > > > # A call to the sub that doesn't need to use the array_ref > example_sub ( $some_scalar, $some_hash_ref, [], 'some string' ); > > sub example_sub { > my ( $passed_scalar, $passed_hash_ref, $some_array_ref, $passed_string > ) = @_; > > > } > > > I have to go back to all of the other calls to the sub and add an > empty array ref in the call. > > I realise I could stick them on the end of the list to avoid doing > this, but I like them to be in a logical order. Any better suggestions? > > I thought about using a hash instead of a list of variables. > > > example_sub ( > { > some_scalar => $some_scalar, > some_hash_ref => $some_hash_ref, > some_string => 'some string', > } > ); > > So I could add to it any way I wanted and write them in any order. > > Any better suggestions? > > Thanks in advance, > Nigel > >
Maybe in OO Style ? I guess that would be clear enough. Just more sub-class to do more things.... likely no need to touch the old codes... package whatever ; sub doingA { deal with array } sub doingH { deal with hash } sub example_sub { bless {} } 1; __END__ # main use whatever; my $func = whatever::example_sub ; my $step1 = $func -> doingA; my $step2 = $func -> doingB; my $end_result = $step1 . $step2 ; hth, Mug -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>