[perl-python] 20050119 writing modules

Xah Lee xah at xahlee.org
Wed Jan 19 00:12:26 EST 2005


© # -*- 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
©   xah at xahlee.org
©   http://xahlee.org/PageTwo_dir/more.html




More information about the Python-list mailing list