文書更新:2018年07月02日(月) 午後6時41分01秒
Home > 備忘録 > 言語関連 > 言語共通 > 外部ファイルのサブルーティン化( 43 )
共通に利用できるサブルーティンを1つの外部ファイルにまとめ利用します。
外部ファイルのサブルーティン化
- ruby 版
- 外部ファイル
subroutine.rb
def test
....
end
def test2
....
end
- ruby 側
demo.rb
require "subroutine.rb" #subroutine.rbを読込む
def test3
....
end
test2() #subroutine.rbのtest2をコールする
test() #subroutine.rbのtestをコールする
test3()
php 版perl 版- 外部ファイル
subroutine.pl
sub test{
}
sub test2{
}
1;←この行がないと動かない(ファイルの最後に 1; をつける)
- perl 側
demo.pl
require("subroutine.pl"); #subroutine.plを読込む
sub test3(){
}
&test2(); #subroutine.plのtest2をコールする
&test(); #subroutine.plのtestをコールする
&test3();
python 版- ディレクトリー構造
demo.py
module/ ←ディレクトリー
__init__.py ←空の__init__.pyを作る。これがないと動きません。
subroutine.py ←サブルーチンをまとめたファイル
- 外部ファイル
subroutine.py
def test():
.......
.......
def test2():
.......
.......
- python 側
「 module.subroutine 」の module は subroutine.py が格納されているディレクトリー
demo.py
from module.subroutine import *
def test4():
.......
#---------------
test() ←subroutine.pyの関数をコール
test2() ←subroutine.pyの関数をコール
test4()
遊び
| 郵便番号 | 住所 |
|---|
| 135-0064 | 東京都江東区青海 | | 162-0812 | 東京都新宿区西五軒町 | | 675-0024 | 兵庫県加古川市尾上町長田 |
| | 郵便番号 | 住所 |
|---|
| 380-0941 | 長野県長野市安茂里 | | 270-0151 | 千葉県流山市後平井 | | 928-0032 | 石川県輪島市小伊勢町 |
|