[perl-python] 20050119 writing modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xah Lee

    #1

    [perl-python] 20050119 writing modules

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

Working...