By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

[perl-python] 20050119 writing modules

P: n/a
# -*- 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
xa*@xahlee.org
http://xahlee.org/PageTwo_dir/more.html

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.