Passing ARG[0] parameters to a subroutine in a module

D

dn.perl

use English ;

Within a single-file script, I tend to use the following method to
pass arguments :

$rh_result_set = &main::get_data( {
rh_query => $rh_query,
rh_db_details => $rh_db_details,
db_name => $db_name } );

sub get_data
{
my $rh_query = $ARG[0]{rh_query};
my $rh_db_details = $ARG[0]{rh_db_details};
}


Is it possible, and if possible is it advisable, to pass parameters to
a function in a module in this manner?

-------
 
P

Peter Makholm

use English ;

$rh_result_set = &main::get_data( {

You shouldn't use '&' as it is considered bad style. Using the fully
quallyfied name ''main::get_data' instead of just 'get_data' is
unneeded too.
rh_query => $rh_query,
rh_db_details => $rh_db_details,
db_name => $db_name } );

sub get_data
{
my $rh_query = $ARG[0]{rh_query};
my $rh_db_details = $ARG[0]{rh_db_details};
}


Is it possible, and if possible is it advisable, to pass parameters to
a function in a module in this manner?

Yes it is possible if the module's API is defined that way. If a
module provides both a positional and a named way of passing
parameters I prefere to use the latter.

But I really don't like to have to add an extra pair of
parentheses. So I usually define my functions with named parameteres
in this way:

sub get_data {
my %args = @_;
my $query = $args{query};
my $details = $args{details};

...
}

get_data( query => 'foo', details => 1 );

//Makholm
 
T

Tad J McClellan

Peter Makholm said:
(e-mail address removed) writes:


You shouldn't use '&' as it is considered bad style.


It is not merely a style choice.

It is a *semantic* choice.

That is, &get_data() means something different than get_data() does,
and you almost never want the meaning that is associated with &get_data().

The different meanings are pointed out near the top of:

perldoc perlsub
 
T

Tad J McClellan

use English ;

Within a single-file script, I tend to use the following method to
pass arguments :

$rh_result_set = &main::get_data( {
rh_query => $rh_query,
rh_db_details => $rh_db_details,
db_name => $db_name } );

sub get_data
{
my $rh_query = $ARG[0]{rh_query};
my $rh_db_details = $ARG[0]{rh_db_details};
}


Is it possible,


What happened when you tried it?

and if possible is it advisable, to pass parameters to
a function in a module in this manner?


Where "this manner" means "by name" rather than "by position"?

Sure.

Where "this manner" means with

use English;

No, as it ignores this, taken straight from perldoc English:

... It is especially important to do this in modules ...


I personally would never use the English module in any production code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top