How do I pass values from a .pl to a .pm.

Discussion in 'Perl Misc' started by Ironhide, Apr 1, 2011.

  1. Ironhide

    Ironhide Guest

    With the code below how do I send the value of $value_to_pass to A.pm,
    so that sub-routines in A.pm can use them?

    A.pm
    ====

    package A;

    .....
    .....
    Do something with the value that I got from the perl script

    test.pl
    =====
    use A;

    my $value_to_pass= get_value(...);

    sub get_value{
    .....
    .....
    return $value
    }

    ~hanks for your help in advance
    _gourab
     
    Ironhide, Apr 1, 2011
    #1
    1. Advertising

  2. On 01/04/2011 07:50, Ironhide wrote:
    > With the code below how do I send the value of $value_to_pass to A.pm,
    > so that sub-routines in A.pm can use them?
    >
    > A.pm
    > ====
    >
    > package A;
    >
    > ....
    > ....
    > Do something with the value that I got from the perl script
    >
    > test.pl
    > =====
    > use A;
    >
    > my $value_to_pass= get_value(...);
    >
    > sub get_value{
    > ....
    > ....
    > return $value
    > }
    >


    See answers from Henry Law & Tad McClellan

    In addition, what you might want to do is make A.pm be object-oriented.
    Read `perldoc perltoot`

    Then test.pl can do

    #!/usr/bin/perl
    use strict;
    use warnings;
    use A;

    #
    # "send the value of $value_to_pass to A.pm"
    #
    my $value_to_pass = 42;
    my $a = A->new($value_to_pass);

    #
    # "sub-routines in A.pm can use [it]"
    #
    $a->foo();
    $a->bar();

    See the tutorial I referred to for details.

    --
    RGB
     
    RedGrittyBrick, Apr 1, 2011
    #2
    1. Advertising

  3. Ironhide <> wrote:
    >With the code below how do I send the value of $value_to_pass to A.pm,
    >so that sub-routines in A.pm can use them?
    >
    >A.pm
    >====
    >
    >package A;
    >
    >....
    >....
    >Do something with the value that I got from the perl script


    sub Do_something_with_the_value_that_I_got_from_the_perl_script {
    my $value = shift;
    ......
    }

    >
    >test.pl
    >=====
    >use A;
    >
    >my $value_to_pass= get_value(...);


    Do_something_with_the_value_that_I_got_from_the_perl_script($value_to_pass);

    >sub get_value{
    >....
    >....
    >return $value
    >}
    >
    >~hanks for your help in advance
    >_gourab
     
    Jürgen Exner, Apr 2, 2011
    #3
  4. RedGrittyBrick <> wrote:
    >On 01/04/2011 07:50, Ironhide wrote:
    >> With the code below how do I send the value of $value_to_pass to A.pm,
    >> so that sub-routines in A.pm can use them?

    >
    >In addition, what you might want to do is make A.pm be object-oriented.


    Or at the very least if you prefer a procedural interface have a
    function "set()" to not break the data abstraction that the module
    provides:

    A::set($value_to_pass)

    jue
     
    Jürgen Exner, Apr 2, 2011
    #4
  5. Ironhide

    ccc31807 Guest

    On Apr 1, 1:50 am, Ironhide <> wrote:
    > With the code below how do I send the value of $value_to_pass to A.pm,
    > so that sub-routines in A.pm can use them?


    Why would you want to do that? A module essentially contains common
    code that other code can call. For example, if you had a need to
    configure the environment for several scripts, say to set $username,
    $password, and $ipaddress, so that the script could communicate with
    some server, you might want to put the configuration functionality in
    a module, like this:

    package commoncode;
    sub configure
    {
    my $script = shift;
    my %CONFIG;
    # open configuration file and set variables
    return \%CONFIG;
    }

    while in your script that needs to be configured, you might do this:

    use commoncode;
    my $CONFIG = commoncode::configure('FTPscript');
    my $username = $CONFIG->{username};
    .... and so on.

    The point is that your module contains code that you use in your
    scripts, and you pass and return values to that code in the same way
    you would as if the code were in the script itself.

    I may be dumb, dumber, and dumbest, but I can't see how your question
    makes any sense.

    CC.
     
    ccc31807, Apr 3, 2011
    #5
    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. Jerry
    Replies:
    20
    Views:
    8,006
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    582
  3. Mr A
    Replies:
    111
    Views:
    2,168
  4. Radde
    Replies:
    38
    Views:
    1,020
  5. Robert
    Replies:
    10
    Views:
    1,384
    E. Robert Tisdale
    Aug 24, 2005
Loading...

Share This Page