in reply to Re: Avoiding Globals with OO Perl
in thread Avoiding Globals with OO Perl

I am learning alot thank you all.

Want I want to avoid is this. Passing variables to subroutines that don't use them because subs inside need them

#pass in vars some of which won't be used till deep #nested sub work($a,$b,$c,$d,$e,$f); sub work{ my ($a,$b,$c,$d,$e,$f) = @_; more_work($b,$c); print "$a\n"; } more_work{ my ($b,$c) = @_; even_more_work($c); print "$b\n"; } even_more_work{ $c = shift; print "$c\n"; }

Replies are listed 'Best First'.
Re^3: Avoiding Globals with OO Perl
by BrowserUk (Patriarch) on Oct 20, 2011 at 20:15 UTC
    work($a,$b,$c,$d,$e,$f); sub work{ my ($a,$b,$c,$d,$e,$f) = @_; more_work($b,$c); print "$a\n"; } more_work{ my ($b,$c) = @_; even_more_work($c); print "$b\n"; }

    $d, $e, $f are never used, and $b & $c are only used by more_work().

    Then perhaps what you should be doing is:

    more_work( $b, $c ); work( $a )

    Or if work() requires the results of more_work(), then maybe:

    work( $a, more_work( $b, $c ) );

    I realise that your example was just that, but it demonstrates the problem with using made-up examples rather than real ones. It becomes all too easy to exaggerate the effect of the perceived problem -- that of passing lots of data around -- whilst obscuring what is often the real problem -- poorly structured code.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Avoiding Globals with OO Perl
by GrandFather (Saint) on Oct 21, 2011 at 11:14 UTC

    That looks like a candidate for some light weight OO and named parameters. Consider:

    use strict; use warnings; my $obj = bless {flibble => 1, floop => 2, flop => 3}; $obj->work(nuts => 7, bolts => 5); sub work{ my ($self, %params) = @_; $self->more_work(%params); print "$params{nuts}\n" if $self->{flibble}; } sub more_work{ my ($self, %params) = @_; $self->even_more_work(%params); print "$params{bolts}\n" if $self->{floop} > 1; } sub even_more_work{ my ($self, %params) = @_; my $sum = $params{nuts} + $params{bolts}; print "$sum\n" if $self->{flop} == 3; }

    Although in real code a constructor would check important parameters and subs would check that any required named parameters are as expected.

    True laziness is hard work
Re^3: Avoiding Globals with OO Perl
by fisher (Priest) on Oct 24, 2011 at 06:02 UTC
    You know, you've faced the problem of my everyday erlang reality. There is no such thing as a global variables, but (sic!) there is a process-dictionary, the State and things alike. So, anyway, you need to have some dynamically modifiable in runtime params which you will need only on N-th in depth sub. Just create a hash-like structure and give a link to it in a bunch of your nested subs =)