CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
how to use Import and Export

 



death_metal
Novice

May 25, 2013, 7:34 AM

Post #1 of 20 (1234 views)
how to use Import and Export Can't Post

Hi,
I am totally confused, how we should use Import and export in Perl.
Also, every now and then I get an error @INC related in perl/lib. Can some one please make me understand why I get this error?


BillKSmith
Veteran

May 25, 2013, 9:35 AM

Post #2 of 20 (1230 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

Import/Export is a mechanism for making the symbols in one package available to another. This is not an issue at all in object oriented perl. Import is never invoked directly.

Perl has many ways to access external code. The use of modules is the only one that belongs in a beginners forum. (The others are rarely used anyhow.) Modules conform to rather strict standards which make them very easy to use. (Most provide detailed usage information in the documentation.) Some modules export all the symbols you might need. All you need is a use statement with the module name. This will load the module and make all those symbols available. Other modules are more selective. They also export all significant symbols, but you do not get them unless you ask for them on your use statement. (Refer to module documentation)

There are several libraries where modules can be stored. The array @INC is a list of these libraries. You can add to this list temporarily with the lib pragma. Usually a message about @INC means that the module that you want is not in any of the libraries. For an existing module, this means either the module is not installed or it has become corrupted. For a module that you developed, you probably need a lib statement.

If you are writing your own modules, you may want to ask specific questions.
Good Luck,
Bill


death_metal
Novice

May 26, 2013, 6:07 AM

Post #3 of 20 (1214 views)
Re: [BillKSmith] how to use Import and Export [In reply to] Can't Post

Hi BillKSmith,

Thank you for your time and lengthy reply.
But again it helps me less to understand Unimpressed Frown

Here are my two file names :-
ol.pl and expe.pl I want to simply export const from expe to ol.pl

The code for expe.pl is:

Code
 
package expe;
use strict;
use warnings;

use Exporter qw(import); #I found out to write this code
use CONSTANT const=>42;
our @Export_ok=('const'); #again found out, to use this line of #code, don't know the logic behind this
print "Mouse"; #Just to test if the script is working or not


The code of ol.pl is:

Code
package ol; 
use 'c://Perl//bin//expe.pl'::'const';
#use expe qw(const);

print const;


I can successfully run expe.pl with output as mouse

And when I run ol.pl, I get below error:
syntax error at ol.pl line 2, near use "'c://Perl//bin//expe.pl'::'const'
Execution of ol.pl aborted due to compilation errors

In Reply To
Import/Export is a mechanism for making the symbols in one package available to another. This is not an issue at all in object oriented perl. Import is never invoked directly.

Perl has many ways to access external code. The use of modules is the only one that belongs in a beginners forum. (The others are rarely used anyhow.) Modules conform to rather strict standards which make them very easy to use. (Most provide detailed usage information in the documentation.) Some modules export all the symbols you might need. All you need is a use statement with the module name. This will load the module and make all those symbols available. Other modules are more selective. They also export all significant symbols, but you do not get them unless you ask for them on your use statement. (Refer to module documentation)

There are several libraries where modules can be stored. The array @INC is a list of these libraries. You can add to this list temporarily with the lib pragma. Usually a message about @INC means that the module that you want is not in any of the libraries. For an existing module, this means either the module is not installed or it has become corrupted. For a module that you developed, you probably need a lib statement.

If you are writing your own modules, you may want to ask specific questions.



Laurent_R
Veteran / Moderator

May 26, 2013, 9:02 AM

Post #4 of 20 (1206 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

You have some problems with upper case / lower case.

The 'constant' module is lc, the @EXPORT_OL array is uc.

Try the following:

The expe.pm module:


Code
package expe;  
use strict;
use warnings;

use Exporter qw(import);
use constant const=>42;
our @EXPORT_OK=qw /const/;
1;


The ol.pl program:


Code
use lib 'c:/Perl/bin'; # tells Perl to look for modules in this directory (in addition to the configured directories) 
use expe qw /const/;

print const;


With the appropriate change in the path for the file, I get the following output:


Code
$ perl ol.pl 
42



BillKSmith
Veteran

May 26, 2013, 10:56 AM

Post #5 of 20 (1200 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

Laurent has certainly solved your immediate problem. Let me make one suggestion for the future. Read the documentation in perldoc perlmod. In the section 'Perl Modules' you will find a template. If you start a new module with the template and cut-and-paste as necessary, you will have all of the necessary pieces, and avoid many of the common mistakes.
Good Luck,
Bill


death_metal
Novice

May 27, 2013, 9:30 AM

Post #6 of 20 (1189 views)
Re: [Laurent_R] how to use Import and Export [In reply to] Can't Post

Hi Laurent,
Thanks for such a comprehensive explanation, but when I try to run ol.pl, it doesn't work in my system.

When I run, ol.pl, I get the below error:
can't locate expe.pl in @INC( @INC contains: c:/Perl/bin c:/Perl/site/lib c:Perl/lib) at ol.pl line 5
BEGIN failed--compilation aborted at ol.pl line 5

Below is the code of ol.pl

Code
package ol; 
#use 'c://Perl//bin//expe.pl'::'const';
use lib 'c:/Perl/bin';

use expe qw (const);

print const;
[\code]
I have few doubts:-

1) our @EXPORT_OK=qw /const/; here we have used =
however,
use expe qw /const/; , here we don't use =, I am unable to understand why?

In Reply To
You have some problems with upper case / lower case.

The 'constant' module is lc, the @EXPORT_OL array is uc.

Try the following:

The expe.pm module:


Code
package expe;  
use strict;
use warnings;

use Exporter qw(import);
use constant const=>42;
our @EXPORT_OK=qw /const/;
1;


The ol.pl program:


Code
use lib 'c:/Perl/bin'; # tells Perl to look for modules in this directory (in addition to the configured directories) 
use expe qw /const/;

print const;


With the appropriate change in the path for the file, I get the following output:


Code
$ perl ol.pl 
42




Laurent_R
Veteran / Moderator

May 27, 2013, 10:55 AM

Post #7 of 20 (1185 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

Where did you put your expe.pm module? It has to be in c:/Perl/bin/ if you want this to work, and the error message shows that this is not the case.

For your other questions:


Code
@EXPORT_OK=qw /const/;


This is defining the @EXPORT_OK array as an array contain ing just one element, const.


Code
use expe qw /const/


This is saying that you want to use the expe module and that, in this module, you want to use "const".

Two differents things.


death_metal
Novice

May 28, 2013, 10:46 AM

Post #8 of 20 (1167 views)
Re: [Laurent_R] how to use Import and Export [In reply to] Can't Post

Hi Laurent,
Apologies for the inconvenience caused so far. Frown

expe.pl is in C:/Perl/bin folder. I wonder why it is not working then. Unsure

Thanks for inputs on other queries. I will read more.


In Reply To
Where did you put your expe.pm module? It has to be in c:/Perl/bin/ if you want this to work, and the error message shows that this is not the case.

For your other questions:


Code
@EXPORT_OK=qw /const/;


This is defining the @EXPORT_OK array as an array contain ing just one element, const.


Code
use expe qw /const/


This is saying that you want to use the expe module and that, in this module, you want to use "const".

Two differents things.



Laurent_R
Veteran / Moderator

May 28, 2013, 2:03 PM

Post #9 of 20 (1160 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

I can't test right now, but try to rename expe.pl as expe.pm. I think that the use function expects a file with .pm as extension, not .pl.


death_metal
Novice

May 29, 2013, 7:24 AM

Post #10 of 20 (1144 views)
Re: [Laurent_R] how to use Import and Export [In reply to] Can't Post

Hi Laurent,
Thanks for your precious time.
I have saved the file with .pm extension and it worked.
Also, there was an error:
I wasn't using upper case for

Exporter_OK
It is to be used as EXPORTER_OK

Once again, I would like to thank you. Smile


In Reply To
I can't test right now, but try to rename expe.pl as expe.pm. I think that the use function expects a file with .pm as extension, not .pl.



(This post was edited by death_metal on May 29, 2013, 7:25 AM)


Laurent_R
Veteran / Moderator

May 29, 2013, 10:55 AM

Post #11 of 20 (1138 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

You obviously have failed to read what I wrote previously with enough attention. Frown

I told you before about @EXPOTR_OK un upper case, as I also told you to name the module expe.pm. Tongue

Well, happy that it works now for you.


death_metal
Novice

May 29, 2013, 11:06 AM

Post #12 of 20 (1137 views)
Re: [Laurent_R] how to use Import and Export [In reply to] Can't Post

Thanks a lot for your patience.

I have one more query, sorry to dump with so many things. Frown
if I comment line 1 and line 2 in exp2.pm, then again ol.pl works.

Code for ol.pl


Code
package ol; 
#use 'c://Perl//bin//expe.pl'::'const';
use lib 'c:/Perl/bin';

use exp2 qw /const/;

print "\nChataayi\n";
print const;


Code for exp2.pm is


Code
package exp2; 
use strict;
use warnings;

# line 1# require Exporter;
# line 2# use Exporter qw(import);
use constant const=>42;
our @EXPORT_OK=('const');
print "Mouse";
print const;


But what I have understood so far is without @ISA qw(Exporter) or importing exporter's import method, module doesn't get import and export functionality.

I wonder after commenting ol.pl gives proper input. Shocked


FishMonger
Veteran / Moderator

May 29, 2013, 12:17 PM

Post #13 of 20 (1130 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

Putting your scripts and modules under c:/Perl/bin is a very poor habit. Don't do that!!

Why did you have the package statement in ol.pl? Remove it.

Add the strict and warnings pragmas to ol.pl and you'll discover several problems.

Now, uncomment the 'require Exporter;' line in exp2.pm and add our @ISA = qw(Exporter); then rerun your ol.pl script


death_metal
Novice

May 29, 2013, 1:18 PM

Post #14 of 20 (1125 views)
Re: [FishMonger] how to use Import and Export [In reply to] Can't Post

Hi FishMonger,

Where should I put my scripts then?
Why is it no advised to put in bin folder?

I couldn't understand what you meant by:-
Why did you have the package statement in ol.pl? Remove it.
Which package statement should be removed in ol.pl?

Kindly help me.


In Reply To
Putting your scripts and modules under c:/Perl/bin is a very poor habit. Don't do that!!

Why did you have the package statement in ol.pl? Remove it.

Add the strict and warnings pragmas to ol.pl and you'll discover several problems.

Now, uncomment the 'require Exporter;' line in exp2.pm and add our @ISA = qw(Exporter); then rerun your ol.pl script



FishMonger
Veteran / Moderator

May 29, 2013, 2:25 PM

Post #15 of 20 (1117 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post


Quote
Where should I put my scripts then?

Create a directory specifically for code development. I typically use C:\testing or C:\dev or something similar.

I do the same for the modules. Sometimes I create a sub dir off of the main development dir, such as C:\dev\modules and then add a use lib 'C:\dev\modules'; statement. You could put the modules under C:\perl\site\lib. But even that is not recommend until you are experienced in writing modules and packaging them up so you can install them via cpan.


Quote
Why is it no advised to put in bin folder?

Because that's where the perl application files are placed and simply not the appropriate place for your scripts. You don't put your excel spreadsheets under the "C:\Program Files" dir, do you?


Quote
Which package statement should be removed in ol.pl?

Your post only shows 1 package statement in that file. Do you have more than that? Only modules should have the package statement, not plain scripts.

You should not use all lowercase names for modules; that is by convention reserved for pragmas.

Here are the cleaned up versions, with a couple comments added to the module.

Exp2.pm

Code
package Exp2; 

use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);

# In most cases, using constants is poor design choice
# It would be better to use the Readonly module
# http://search.cpan.org/~roode/Readonly-1.03/Readonly.pm
use constant const => 42;

# exporting vars/constants is also a sign of a poor design choice.
our @EXPORT_OK = ('const');

# I assume these are simply debugging print statements
# otherwise, having them wouldn't make any sense.
print "Mouse";
print const;


ol.pl

Code
#!c:/perl/bin/perl 

use strict;
use warnings;
use lib 'C:\dev\modules';
use Exp2 qw/const/;

print "\nChataayi\n";
print const;



Laurent_R
Veteran / Moderator

May 29, 2013, 2:31 PM

Post #16 of 20 (1116 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

Well, I just tried to fix your code so that you could get it working, Fishmonger is rightly pointing out to more general poor practices in your code. Don't take it as an offense, he is just trying to help you writing better code, you just need to learn better ones.

Simple guilelines:
- Put your own modules in a private directory where you don't risk to interfere with standard modules (and "use lib..." to tel Perl to look for them there).
- ol.pl is just a program, not a module, you don't need to have a package statement.


death_metal
Novice

May 30, 2013, 11:14 AM

Post #17 of 20 (1106 views)
Re: [FishMonger] how to use Import and Export [In reply to] Can't Post

Hi FishMonger,

Thanks a lot for your patience and taking time out from your busy schedule in explaining me in such a comprehensive way.
I was completely unaware package and module knowledge. And creating new directory for code. I now have created a new folder for codes.


I have small query, sorry for these many questions. I am completely new to this.


Quote
require Exporter;
our @ISA = qw(Exporter);


If we use use Exporter qw( import ); then again code runs successfully.
I know the difference the to, but how would I know which to use when?


death_metal
Novice

May 30, 2013, 11:26 AM

Post #18 of 20 (1104 views)
Re: [Laurent_R] how to use Import and Export [In reply to] Can't Post

Hi Laurent_R,

I completely understand it is my fault, not knowing the basics in Perl and asking questions. I would like to apologize to you and FishMonger for the inconvenience caused.

I have done the changes what FishMonger has told.

Thank you for your time.


In Reply To
Well, I just tried to fix your code so that you could get it working, Fishmonger is rightly pointing out to more general poor practices in your code. Don't take it as an offense, he is just trying to help you writing better code, you just need to learn better ones.

Simple guilelines:
- Put your own modules in a private directory where you don't risk to interfere with standard modules (and "use lib..." to tel Perl to look for them there).
- ol.pl is just a program, not a module, you don't need to have a package statement.



FishMonger
Veteran / Moderator

May 30, 2013, 12:19 PM

Post #19 of 20 (1104 views)
Re: [death_metal] how to use Import and Export [In reply to] Can't Post

A careful reading the following related perldoc's would be very benificial.

Code
        perlmod             Perl modules: how they work 
perlmodlib Perl modules: how to write and use
perlmodstyle Perl modules: how to write modules with style
perlmodinstall Perl modules: how to install from CPAN
perlnewmod Perl modules: preparing a new module for distribution
perlpragma Perl modules: writing a user pragma


as well as:
perldoc parent
http://perldoc.perl.org/parent.html

perldoc -f use
http://perldoc.perl.org/functions/use.html

perldoc -f require
http://perldoc.perl.org/functions/require.html

You're dealing with 2 different aspects of loading modules.
1) The importing of the default exported vars and functions vs specifying which ones to be imported.

2) Setting up inheritance between modules.

The method I showed which manually modifies the @ISA array is one way to setup inheritance. Another older method would be:
use Exporter;
use base qw(Exporter);


The newer and better method would be:
use parent qw(Exporter);

In which case, your module would be:

Code
package Exp2; 

use strict;
use warnings;
use parent qw(Exporter);
use constant const => 42;

our @EXPORT_OK = ('const');

print "Mouse";
print const;



death_metal
Novice

Jun 5, 2013, 10:52 AM

Post #20 of 20 (1060 views)
Re: [FishMonger] how to use Import and Export [In reply to] Can't Post

Hi FishMoneger,
Thank you very much for sharing these links. I will go through these. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives