[perl-python] 20050119 writing modules

X

Xah Lee

© # -*- coding: utf-8 -*-
© # Python
©
© # one can write functions,
© # save it in a file
© # and later on load the file
© # and use these functions.
©
© # For example, save the following line in
© # a file and name it module1.py
© # def f1(n): returns range(n)
©
© # to load the file, use import
© import mymodule
©
© # to use the function, use
© # fileName.functionName.
© print mymodule.f1(5)
©
© # the file name without the .py suffix
© # is the module's name, and is available
© # as the variable fileName.__name__
© print mymodule.__name__
©
© # for more on modules, see
© # http://python.org/doc/2.3.4/tut/node8.html
©
© --------------------
© # the perl analogue is like this:
© # save the following 3 lines in a file
© # and name it mymodule.pm
©
© # package mymodule;
© # sub f1($){(1..$_[0])}
© # 1
©
© # then, call it like the following way
© use mymodule;
© use Data::Dumper;
© print Dumper [&mymodule::f1(7)];
©
© # this is about the simplest way
© # to write a modlue in perl and
© # calling its function.
© # for an inscrutable treatment,
© # see "perldoc perlmod"
©
© Xah
© (e-mail address removed)
© http://xahlee.org/PageTwo_dir/more.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top