Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

usage of EXPORT function

by himesh (Initiate)
on Sep 29, 2012 at 13:31 UTC ( #996376=perlquestion: print w/ replies, xml ) Need Help??
himesh has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have been using perl for some days and had a question in mind.How the EXPORT is useful in OO programming of perl,when I create a .pm file. For Example :Is it require to export "new" function from student.pm to be included into sample.pl file. sample.pl student->new('swarup','26');

Comment on usage of EXPORT function
Re: usage of EXPORT function
by 2teez (Priest) on Sep 29, 2012 at 13:50 UTC

    How the EXPORT is useful in OO programming of perl
    Please note as a general rule, if the module is trying to be object oriented (OO) as stated in the OP, then export nothing.
    Check the example below:

    package Student; use warnings; use strict; sub new { my $type = shift; my $class = ref($type) || $type; my $self = { name => shift // "No name", score => shift // 0, }; return bless( $self, $class ); } sub name { my $self = shift; $self->{'name'} = shift // $self->{'name'}; return $self->{'name'}; } sub score { my $self = shift; $self->{'score'} = shift // $self->{'score'}; return $self->{'score'}; } 1;
    Then that is called in the .pl script like so:, without the Exporting any subroutine
    #!/usr/bin/perl -l use warnings; use strict; use Student; # the module used my $std = Student->new(); print join " - " => ( $std->name(), $std->score() ); # prints No name +- 0 my $std2 = Student->new( "melchi", 78 ); print join " - " => ( $std2->name(), $std2->score() ); # prints melchi + - 78 print join " - " => ( $std->name("kunle"), $std2->score(90) ); # print +s kunle - 90
    You might what to check, perlobj, perlmod, perlmodlib
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re^2: usage of EXPORT function
by tobyink (Abbot) on Sep 29, 2012 at 14:53 UTC

    Modules written to be used for OO should generally not be exporting anything. Especially they should never export a sub which is expected to be called as a method.

    (This was intended as a reply to the OP, not to 2teez. Anyone who has suitable privileges, feel free to re-parent it.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996376]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-08-23 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (172 votes), past polls