Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Style & subroutine organization

by reyjrar (Hermit)
on Aug 29, 2001 at 19:04 UTC ( #108775=note: print w/replies, xml ) Need Help??

in reply to Style & subroutine organization

I'm not clear on what you want to do, but if you want to pass the return value of func1() to func2() and the return value of func2() to func3() then you just have to:
func3( func2( func1() ) );

if you're looking to abuse some perl internals, then you can using the special variable @_ and function passing using &subroutine.
When you call a function like this: &foo; foo is passed the current value of @_. Also, did you know that manipulating @_ inside a function actually manipulates the passed variables? Kinda like pass by reference in c. check this out:
use subs qw/foo bar baz/; @_ = ( 1, 2, 3, 4 ); &foo; # sends current contents of @_ print join(' ', @_), "\n"; &bar; # sends current contents of @_ print join(' ', @_), "\n"; &baz; # sends current contents of @_ print join(' ', @_), "\n"; &baz(); # sends an empty list print join(' ', @_), "\n"; sub foo { map { $_++; } @_; # map actually changes the values in @_ } sub bar { map { $_+=2 } @_; # again, map changes @_ } sub baz { map { $_--; } @_; # also chainging @_; }

I don't know which you're trying to accomplish, but becareful using these tricks as it could have unwanted side effects. Also note that calling a sub routine like this: &foo() over rides the default behaviour of passing @_ to the function and sends it an empty list ().

hope that helps,

Replies are listed 'Best First'.
Re: Re: Style & subroutine organization
by demerphq (Chancellor) on Aug 29, 2001 at 21:41 UTC
    I usually prefer to keep the scope levels in mind. Because perl doesnt have a defined main block and some of the subtler uses of scope (anonymous blocks for instance) and BEGIN{} END{} blocks I try to keep things organised so its intuitive. For instance
    Because this kind of thing can get confusing:
    my $var; sub foo {$var++; #ie code} my @list; use Some::Class; sub bar{} my %hash; sub sna{} sub fu{} my $scalar; # code # .... __DATA__
    Now you might say "what if a lexical variable is used only by one particular funct ie static lexical" well then I usually do something like this
    { #Anonymous block for the following static lexicals: my $static_scalar; my @static_array; sub push_stack { push @static_array,@_} sub pop_stack { pop @stack} } #End anonymous block
    So the first would probably end up like this:
    use Some::Class; sub sna{} sub fu{} { my $var; sub foo {$var++; #ie code} } { my @list; sub bar{} } my $scalar; my %hash; # code # .... __DATA__
    Much easier to figure out whats gone 12 months later i assure you. Yves

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://108775]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2019-11-21 19:40 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (105 votes). Check out past polls.