Home: Perl Programming Help: Advanced:
Module programming



oldwarrior_32
Novice

Mar 8, 2012, 11:00 AM


Views: 14999
Module programming

 
Hello.

I am curious about Perl Modules. I have done some scripts based in modules. The modules always make the hard part of the job. I try to read a module source code and is very painful.

For example the telnet module, I don't get how the module tells the computer "send a SYN segment to start a TCP session". How do you tell the computer that?

Could you recommend a book or webpage that describes this deep Perl programming? It has some special technical name?

Thanks.

PD: the Telnet Module begins with this,

use vars qw(@EXPORT_OK);
@EXPORT_OK = qw(TELNET_IAC TELNET_DONT TELNET_DO TELNET_WONT TELNET_WILL
TELNET_SB TELNET_GA TELNET_EL TELNET_EC TELNET_AYT TELNET_AO
TELNET_IP TELNET_BREAK TELNET_DM TELNET_NOP TELNET_SE
TELNET_EOR TELNET_ABORT TELNET_SUSP TELNET_EOF TELNET_SYNCH
TELOPT_BINARY TELOPT_ECHO TELOPT_RCP TELOPT_SGA TELOPT_NAMS
TELOPT_STATUS TELOPT_TM TELOPT_RCTE TELOPT_NAOL TELOPT_NAOP
TELOPT_NAOCRD TELOPT_NAOHTS TELOPT_NAOHTD TELOPT_NAOFFD
TELOPT_NAOVTS TELOPT_NAOVTD TELOPT_NAOLFD TELOPT_XASCII
TELOPT_LOGOUT TELOPT_BM TELOPT_DET TELOPT_SUPDUP
TELOPT_SUPDUPOUTPUT TELOPT_SNDLOC TELOPT_TTYPE TELOPT_EOR
TELOPT_TUID TELOPT_OUTMRK TELOPT_TTYLOC TELOPT_3270REGIME
TELOPT_X3PAD TELOPT_NAWS TELOPT_TSPEED TELOPT_LFLOW
TELOPT_LINEMODE TELOPT_XDISPLOC TELOPT_OLD_ENVIRON
TELOPT_AUTHENTICATION TELOPT_ENCRYPT TELOPT_NEW_ENVIRON
TELOPT_EXOPL);

What does this mean "use vars qw(@EXPORT_OK);"?

And all that name in the list? It's like are predefined to do something in the system, is not like a common list.


(This post was edited by oldwarrior_32 on Mar 8, 2012, 11:03 AM)


wickedxter
User

Mar 8, 2012, 12:31 PM


Views: 14990
Re: [oldwarrior_32] Module programming

the @EXPORT and @EXPORT_OK are part of the Exporter module that export the subrutines or variables to be used..

http://search.cpan.org/~toddr/Exporter-5.66/lib/Exporter.pm


Code
 
package YourModule;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(sub1 sub1 $VER); #exports at request

my $VER = '1.0';

sub1 {...}
sub2 {...}


1;



the use vars module is (obsolete) as the cpan.org page says and superseded by our declarations.
the modules is makeing the @EXPORT_OK array a global variable.

http://search.cpan.org/~flora/perl-5.14.2/lib/vars.pm


rovf
Veteran

Mar 9, 2012, 1:27 AM


Views: 14979
Re: [oldwarrior_32] Module programming

EXPORT_OK says that these identifiers *can* be imported only by explicit request by the user of the module.

EXPORT would say that these identifiers *will* be automatically imported, *except* if the user explicitly writes a import list.

If you keep to the habit to always explicitly say which names you want to import (which, IMO, is a good practice anyway), it doesn't make any difference whether the names are in EXPORT or in EXPORT_OK.

Ronald


FishMonger
Veteran / Moderator

Mar 9, 2012, 7:44 AM


Views: 14969
Re: [oldwarrior_32] Module programming

You may want to read a few of the perldocs related to modules


Code
perldoc perlmod             Perl modules: how they work 
perldoc perlmodlib Perl modules: how to write and use
perldoc perlmodstyle Perl modules: how to write modules with style



oldwarrior_32
Novice

Mar 10, 2012, 9:41 AM


Views: 14924
Re: [oldwarrior_32] Module programming

 
Thanks for your answers. Ok, with Export you can export subroutines from another modules. In the code exposed, the subroutines are:


######################## Exported Constants ##########################


sub TELNET_IAC () {255}; # interpret as command:
sub TELNET_DONT () {254}; # you are not to use option
sub TELNET_DO () {253}; # please, you use option
sub TELNET_WONT () {252}; # I won't use option
sub TELNET_WILL () {251}; # I will use option
sub TELNET_SB () {250}; # interpret as subnegotiation
sub TELNET_GA () {249}; # you may reverse the line
sub TELNET_EL () {248}; # erase the current line
sub TELNET_EC () {247}; # erase the current character
sub TELNET_AYT () {246}; # are you there
sub TELNET_AO () {245}; # abort output--but let prog finish
sub TELNET_IP () {244}; # interrupt process--permanently
sub TELNET_BREAK () {243}; # break
sub TELNET_DM () {242}; # data mark--for connect. cleaning
sub TELNET_NOP () {241}; # nop
sub TELNET_SE () {240}; # end sub negotiation
sub TELNET_EOR () {239}; # end of record (transparent mode)
sub TELNET_ABORT () {238}; # Abort process
sub TELNET_SUSP () {237}; # Suspend process
sub TELNET_EOF () {236}; # End of file
sub TELNET_SYNCH () {242}; # for telfunc calls

What does the above code means? They are constans like "DEFINE" in "C"? If so, you can call a subroutine that is a constant or a mere numerical value?


rovf
Veteran

Mar 12, 2012, 6:48 AM


Views: 14860
Re: [oldwarrior_32] Module programming

Yes, they are constants. A prototyped sub without parameters acts as a constant, if certain conditions are met. Have a look at

perldoc perlsub

In practice, however, the code would be more clearly written as

use constant TELNET_SYNCH => 242;

etc.

See

perldoc constant

for details.


oldwarrior_32
Novice

Mar 14, 2012, 8:49 PM


Views: 14774
Re: [rovf] Module programming

 
Thanks all for your help.

By the way, the telnet library for python is a lot more clear than Perl.


FishMonger
Veteran / Moderator

Mar 15, 2012, 6:13 AM


Views: 14758
Re: [oldwarrior_32] Module programming

Part of the problem is that Net::Telnet is old, outdated and hasn't been updated since 2002. Nearly all experienced Perl programmers recommend using one of the newer SSH modules over telnet.