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

Discussion in 'Perl Misc' started by dn.perl@gmail.com, Mar 21, 2009.

  1. Guest

    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?

    -------
     
    , Mar 21, 2009
    #1
    1. Advertising

  2. writes:

    > 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
     
    Peter Makholm, Mar 21, 2009
    #2
    1. Advertising

  3. Peter Makholm <> wrote:
    > writes:



    >> $rh_result_set = &main::get_data( {

    >
    > 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


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 21, 2009
    #3
  4. <> wrote:
    >
    >
    > 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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 21, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Drummond

    Passing a list as an arg to a subroutine

    Mark Drummond, Mar 7, 2006, in forum: Perl
    Replies:
    2
    Views:
    2,319
    Thomas Wasell
    Mar 9, 2006
  2. n00m
    Replies:
    5
    Views:
    403
  3. king
    Replies:
    5
    Views:
    205
  4. Replies:
    7
    Views:
    189
  5. Replies:
    21
    Views:
    359
    Barry Schwarz
    Mar 5, 2014
Loading...

Share This Page