How can I access variables in my perl script from a sub in a module

Discussion in 'Perl Misc' started by sm, Jan 3, 2007.

  1. sm

    sm Guest

    Hi Folks,

    How can I access some of the variables in my perl script from a perl module.
    example

    #!/usr/bin/perl

    use this_pakage;

    my $VARA = 72;
    my $VARB = 44;

    this::pakage::get_vara_value();
    #end

    ===================
    this_package.pm
    ....
    ............
    sub get_vara_value {
    my $AAA = $main::$VARA; ## how do I access $VARA in my script
    print $AAA \n";
    }

    Regards,

    -sm
     
    sm, Jan 3, 2007
    #1
    1. Advertising

  2. sm

    kens Guest

    sm wrote:
    > Hi Folks,
    >
    > How can I access some of the variables in my perl script from a perl module.
    > example
    >
    > #!/usr/bin/perl
    >
    > use this_pakage;
    >
    > my $VARA = 72;
    > my $VARB = 44;
    >
    > this::pakage::get_vara_value();
    > #end
    >
    > ===================
    > this_package.pm
    > ...
    > ...........
    > sub get_vara_value {
    > my $AAA = $main::$VARA; ## how do I access $VARA in my script
    > print $AAA \n";
    > }
    >
    > Regards,
    >
    > -sm


    Based on the information given, I would think passing the variable as
    an argument
    would work in this case.

    get_vara_value($VARA);


    sub get_vara_value
    {
    my $AAA = shift;
    if ( defined( $AAA ) ) {
    print "$AAA\n";
    }
    }

    Of course if you need to modify the variable, it a reference would need
    to be passed
    to the get_vara_value subroutine.

    HTH, Ken
     
    kens, Jan 4, 2007
    #2
    1. Advertising

  3. Re: How can I access variables in my perl script from a sub in amodule

    sm wrote:
    > How can I access some of the variables in my perl script from a perl module.


    By declaring it as a package global.

    our $VARA;

    But to me, doing so sounds like a bad idea. You'd better pass the value
    to the sub.

    my $VARA = 72;
    this_package::get_vara_value( $VARA );

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 4, 2007
    #3
  4. sm

    Dave Weaver Guest

    On Wed, 3 Jan 2007 15:50:11 -0800, sm <> wrote:
    > Hi Folks,
    >
    > How can I access some of the variables in my perl script from a perl module.
    > example
    >
    > #!/usr/bin/perl


    use strict;
    use warnings;

    > use this_pakage;
    >
    > my $VARA = 72;
    > my $VARB = 44;

    ^^

    our $VARA;
    our $VARB;

    Lexical variables (declared using 'my') are not accessible outside
    their lexical scope. Using 'our' will declare a package variable
    instead, which *can* be accessed from elsewhere.

    However, using 'global' variables is a poor approach to sharing data
    betwwen modules.

    > this::pakage::get_vara_value();

    ^^ ^
    this_package::get_vara_value();

    A pooly named sub, since it is not actually "get"ting anything, merely
    printing something.


    > sub get_vara_value {
    > my $AAA = $main::$VARA; ## how do I access $VARA in my script

    ^
    my $AAA = $main::VARA

    Using caps is not the best convention for sub-scoped lexical
    variables.

    > print $AAA \n";
    > }


    1;

    Your module should end with a true value.

    Also, on the whole, modules shouldn't really be accessing things from
    main - it's usually the other way around.
    Here's an example of a better way (UNTESTED):

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

    my $wotsit = Foo::get_wotsit();
    print "Wotsit = $wotsit\n";

    ----- Foo.pm:

    package Foo;
    use strict;
    use warnings;

    { # this block restricts the scope of $Wotsit,
    # so other subs in package Foo can't access it.
    # You should always restrict the scope of a viariable
    # to the smallest it needs to be.

    my $Wotsit = 123;

    sub get_wotsit {
    return $Wotsit;
    }
    }

    # other subs here

    1;
     
    Dave Weaver, Jan 4, 2007
    #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. dpackwood
    Replies:
    3
    Views:
    1,815
  2. loga123
    Replies:
    2
    Views:
    333
    loga123
    May 20, 2006
  3. Ben
    Replies:
    2
    Views:
    903
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,047
    Lawrence D'Oliveiro
    May 20, 2011
  5. Replies:
    4
    Views:
    143
    Ted Zlatanov
    Feb 10, 2009
Loading...

Share This Page