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

S

sm

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
 
K

kens

sm said:
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
 
G

Gunnar Hjalmarsson

sm said:
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 );
 
D

Dave Weaver

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;
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top