Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ways to reach private variables in another module? 1

Status
Not open for further replies.

sunw

Technical User
Sep 24, 2007
32
US
At first, please take a look at the following codes:

File 'test1.pl':
Code:
#! /usr/bin/perl

use strict;
use test2;

my $thisStr = &getStr;
print "\$thisStr = $thisStr\n";
print "##$test2::str##\n";

File 'test2.pm':
Code:
use strict;
use vars qw(@ISA);
use vars qw(@EXPORT);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
  test
);

my $str  = "Can you see this?";

sub getStr {
  return $str;
}

return 1;

The outputs:
Code:
% ./test1.pl
$thisStr = Can you see this?
####

My question: are there any other ways to get $str in test2.pm w/o calling test2::getStr? Maybe I should use 'import'? But I don't know how.

Thanks for your help.
 
Sorry, I had a typo.

Wrong:
Code:
@EXPORT = qw(
  test
);

Should be:
Code:
@EXPORT = qw(
  getStr
);
 
Code:
#! /usr/bin/perl
use warnings;
use strict;
use test2 qw(getStr);

my $thisStr = &getStr;
print "\$thisStr = $thisStr\n";
print "##$test2::str##\n";

Code:
use strict;
require Exporter;
our(@ISA, @EXPORT, @EXPORT_OK);
@ISA = qw(Exporter);
@EXPORT = (); 
@EXPORT_OK = qw(getStr);

my $str  = "Can you see this?";

sub getStr {
  return $str;
}
1;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you, Kevin.

If I understood you right, are you trying to tell me no other way to reach test2::$str w/o calling test2::getStr?

Another question, what is the benefit to use @EXPORT_OK?

Thanks again.
 
Hi,

Would that all work if Test2.pm was Test2.pl ?? im having a similar problem
 
Thank you, Kevin.

If I understood you right, are you trying to tell me no other way to reach test2::$str w/o calling test2::getStr?

Another question, what is the benefit to use @EXPORT_OK?

Thanks again.

You could add $str to the our() list and not declare it with "my". Then in your main script you could access it:

print $test2::str;

@EXPORT_OK is a list of things in the module that the main program can import when loading the module:

use MODULE qw(foo bar biz)

if foo bar and biz are in MODULE and in the @EXPORT_OK list the module will export them.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, I must have missed something. I modified test2.pm (test1.pl is identical to your code):

Code:
use strict;
require Exporter;
our(@ISA, @EXPORT, @EXPORT_OK, [COLOR=red]$str[/color]);
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(getStr);

$str  = "Can you see this?";

sub getStr {
  return $str;
}
1;

And I got this error:
Code:
% ./test1.pl
Name "test2::str" used only once: possible typo at ./test1.pl line 8.
$thisStr = Can you see this?
Use of uninitialized value in concatenation (.) or string at ./test1.pl line 8.
####

Please help.
 
we both missed something. Need to "package" the module:

Test1.pm

Code:
[b]package Test1;[/b]
require Exporter;
our(@ISA, @EXPORT, @EXPORT_OK, $foo);
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw(getStr);

my $str  = "Can you see this?";
$foo = 'not a lexical';

sub getStr {
  return $str;
}
1;

test1.pl

Code:
use strict;
use warnings;
use Test1 qw(getStr);
print getStr;
print $Test1::foo;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top