Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I pass objects to subroutines?

Is one of these the best or the worst approach?

utilize the scope:

my $cache = CHI->new( driver => 'File', expires_in => 3600 );

sub one {
    if ( my $data = $cache->get( 'key_one' ) ) {
    # ...
}

sub two {
    if ( my $data = $cache->get( 'key_two' ) ) {
    # ...
}

passing the object as argument:

my $cache = CHI->new( driver => 'File', expires_in => 3600 );

sub one {
    my ( $cache ) = @_;
    if ( my $data = $cache->get( 'key_one' ) ) {
    # ...
}

sub two {
    my ( $argument1, $cache ) = @_;
    if ( my $data = $cache->get( 'key_two' ) ) {
    # ...
}

or creating in the subroutine a new instance:

sub one {
    my $cache = CHI->new( driver => 'File', expires_in => 3600 );
    if ( my $data = $cache->get( 'key_one' ) ) {
    # ...
}

sub two {
    my $cache = CHI->new( driver => 'File', expires_in => 3600 );
    if ( my $data = $cache->get( 'key_two' ) ) {
    # ...
}
like image 606
sid_com Avatar asked Mar 13 '12 11:03

sid_com


People also ask

How do you pass data into a subroutine?

To pass parameters to a subroutine, the calling program pushes them on the stack in the reverse order so that the last parameter to pass is the first one pushed, and the first parameter to pass is the last one pushed. This way the first parameter is on top of the stack and the last one is at the bottom of the stack.

How do you pass an object to a function?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

How can we do pass by reference in subroutine?

With internal subroutines, you can use TYPE or LIKE to refer to the internal table you want to pass directly. You can pass all internal tables as parameters in the list after TABLES in the FORM and PERFORM statements. Internal tables passed with TABLES are always called by reference.

How do you pass an object as an argument in C method?

C# provides the ref parameter modifier for passing value objects into a method by reference and the out modifier for those cases in which you want to pass in a ref variable without first initializing it. C# also supports the params modifier which allows a method to accept a variable number of parameters.


2 Answers

First choice uses a global variable, not so hot. Third choice is a lot of extra overhead. Not so great either, so I guess the middle choice is preferable within the context of your question. A broader issue is why do the subroutines need to know about the cache at all? It appears they are only worried about the data. I would consider fetching the data and pass that to the subroutines, where they don't have to worry if it was cached or just created.

like image 173
Bill Ruppert Avatar answered Oct 01 '22 13:10

Bill Ruppert


Unless you want to change the original data, it's safer to pass the arguments by reference, using method 2:

my $cache = CHI->new( driver => 'File', expires_in => 3600 );

one (\$cache);

sub one {
    my ( $cache ) = @_;
    if (any {!defined @_} $cache { //can expand on this
       croak "missing parameters";
    if ( my $data = $cache->get( 'key_one' ) ) {
    # ...
}
like image 41
Matt King Avatar answered Oct 01 '22 13:10

Matt King