in reply to Avoiding global object handle

You dismissed method 2 as possibly being too much of a performance hit, since it involves passing a scalar variable containing a reference into each subroutine.

But your preferred method involves calling an extra subroutine every time you want to access your object. I haven't done any tests, but I'd be surprised if sticking an extra param in an existing sub call is more costly than making a whole new call. Especially if in a particular sub you do ten method calls on your object — you've now replaced one param with ten function calls.

Did you actually have a problem with your code running too slow, or did you just get the urge to do this because you suspected there might be one?

Replies are listed 'Best First'.
Re: Re: Avoiding global object handle
by Biker (Priest) on Mar 26, 2002 at 10:43 UTC

    If I have to call the get_handle() when I need it, I can appreciate that there's a performance cost. But pulling this handle through numerous calls to subs that don't need the handle themself, with the only reason to provide the handle to a 'sub-sub' feels as paying for something without getting anything in return.

    Furthermore, imagine three subs named x(), y() and z(). sub x() calls sub y() that calls sub z().

    One day sub z() is changed and needs access to the handle. As a result, the programmer must change sub x() and sub y() to take the handle as a parameter even though they couldn't care less about the handle themself.

    This would lead to discussions like: "If we want to change sub z() to use the functionality of Module.pm, then we must first do an impact analyze to determine how many other subs must be enhanced to provide the handle, directly or indirectly, to our sub z()".

    If sub z() can get a direct access to the handle, the change can be implemented in sub z() with no impact on other subs.

    Yes, I'm talking about rather big and complex Perl applications here.


    Everything went worng, just as foreseen.

Re: Re: Avoiding global object handle
by Biker (Priest) on Mar 26, 2002 at 10:49 UTC

    "Especially if in a particular sub you do ten method calls on your object — you've now replaced one param with ten function calls."

    Not necessarily. If I know that I'm going to use the object 10 times in a specific sub, I can call get_handle() once and save the handle in my own variable throughout the sub.

    sub z { my $handle=get_handle(); $handle->method_a('parameter'); $handle->method_b('parameter'); $handle->method_c('parameter'); # Etc. }



    Everything went worng, just as foreseen.