Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How we can link/access the variables of different module in main script without defining variable as global.. ??

by pinkeshprajapati (Initiate)
on Feb 27, 2014 at 05:29 UTC ( #1076356=perlquestion: print w/replies, xml ) Need Help??
pinkeshprajapati has asked for the wisdom of the Perl Monks concerning the following question:

hi
is their any way by which we can link/access the variable value of other modules without defining it as global variables in it...??

Thanks in advance... :-)

use below code for reference... Note i want to access this variable without defining this variable as global in xyz.pm module.... Can anyone Please suggest way to do this.. ??

abc.pl use strict; use warning; use xyz; print @array;
xyz.pm package xyz; use strict; use warnings; require 5.008; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( @array ); my $file = "qwerty.txt"; open FILE, "< $file"; my @array = <FILE>;
  • Comment on How we can link/access the variables of different module in main script without defining variable as global.. ??
  • Select or Download Code

Replies are listed 'Best First'.
Re: How we can link/access the variables of different module in main script without defining variable as global.. ??
by davido (Archbishop) on Feb 27, 2014 at 06:38 UTC

    This line:

    my @array = <FILE>;

    ...creates a lexically scoped variable named @array, which is actually a completely different variable from the package global promised to Exporter, named @array. The package global's full name is @xyz::array... and it doesn't exist in your program. Aside from telling Exporter that you intend to export it, you never declare it nor populate it with any value.

    You would be able to export @array if it were declared with our instead of my; lexical variables cannot be exported, while package globals may be. Exporting variables is usually the wrong approach though, but it is possible.


    Dave

Re: How we can link/access the variables of different module in main script without defining variable as global.. ??
by NetWallah (Canon) on Feb 27, 2014 at 06:30 UTC
    There are several mechanisms available to access the contents of variables declared inside a package.

    If you follow the link and learn about Symbol tables, package scope (our) then some of these mechanisms may be useful.

    However , the most robust and common method of extracting data from packages is to use accessors, which is essentially a sub inside a package that returns the value of the variable you seek.

    If you need to MODIFY the value, you typically use a mutator - google these terms to get a better idea of how they are used.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

Re: How we can link/access the variables of different module in main script without defining variable as global.. ??
by AnomalousMonk (Chancellor) on Feb 27, 2014 at 12:07 UTC

    Defining a module with accessor and mutator (get_* and set_*) functions has already been discussed. Another approach is to define iterators to access data. Happily, a file handle is already an iterator (or more accurately, is structured to be used by an iterator like readline). You can define functions in your module to interact with, in this case, a file handle and yet keep it absolutely private. A very simple example:

    File use_Getfile_1.pl:

    use warnings; use strict; use Getfile; print qq{printing from file }, Getfile->FILENAME, qq{\n}; print Getfile->content; # in list context Getfile->reset; # start again print scalar Getfile->content; print scalar Getfile->content;

    File Getfile.pm:

    # Getfile.pm package Getfile; use 5.008; use warnings; use strict; use constant FILENAME => 'qwerty.txt'; open my $fh, '<', FILENAME or die qq{opening }, FILENAME, qq{: $!}; sub content { return <$fh>; } sub reset { seek $fh, 0, 0 or die qq{seeking }, FILENAME, qq{: $!}; } 1;

    File qwerty.txt:

    line 1: The rain in Spain line 2: falls mainly on the plain. line 3: The quick brown fox jumps over line 4: the lazy dog. line 5: How now, brown cow?

    Output:

    c:\@Work\Perl\monks>perl use_Getfile_1.pl printing from file qwerty.txt line 1: The rain in Spain line 2: falls mainly on the plain. line 3: The quick brown fox jumps over line 4: the lazy dog. line 5: How now, brown cow? line 1: The rain in Spain line 2: falls mainly on the plain.
Re: How we can link/access the variables of different module in main script without defining variable as global.. ??
by Arunbear (Prior) on Feb 27, 2014 at 11:38 UTC
    An example of the NetWallah's suggestion:
    package Foo; use strict; use Exporter 'import'; # Export on demand, so the caller knows # where these came from. our @EXPORT_OK = qw(get_array populate_array); { my @array; # can only be accessed in this block sub get_array { \@array } sub populate_array { @array = 'a' .. 'h' } } 1;
    Then to try it in the Perl debgugger:
    % perl -de0 Loading DB routines from perl5db.pl version 1.37 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(-e:1): 0 DB<1> use Foo qw(get_array populate_array) DB<2> x get_array() 0 ARRAY(0x7fa9ac81a298) empty array DB<3> populate_array() DB<4> x get_array() 0 ARRAY(0x7fa9ac81a298) 0 'a' 1 'b' 2 'c' 3 'd' 4 'e' 5 'f' 6 'g' 7 'h' DB<5>
Re: How we can link/access the variables of different module in main script without defining variable as global.. ??
by vinoth.ree (Monsignor) on Feb 27, 2014 at 06:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1076356]
Approved by hdb
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-07-21 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?