詳細を隠す

関数を作成して、詳細な処理を関数内に隠す。

SourceToHtml4.plx


# ヘッダ部のコピー
fileCopy(".\\Template\\header.txt");

# ソース部の変換
while ($_ = getLine())
{
putLine($_);
}

# フッタ部のコピー
fileCopy(".\\Template\\footer.txt");

# ファイルのコピー
sub fileCopy
{
my ($fileName) = @_;

open(F, $fileName) || die "open: $!";
while (<F>)
{
print;
}
close(F);
}

# 1行ずつ読み込む
sub getLine
{
# 1行読み込む
$_ = <>;

# TABを空白に変換
while(($pos = index($_, "\t")) >= 0) #TABがあるか
{
$num = 4 - ($pos % 4); #空白何文字分に置き換えればよいか
$_ = substr($_,0,$pos).(' ' x $num).substr($_,$pos+1); #空白に置き換え
}

# 行末の空白を削除
s/ +$//;

# <, >, &, |, (, ) を置換
s/&/&#x26;/g; # &
s/</&#x3C;/g; # <
s/>/&#x3E;/g; # >
s/\(/&#x28;/g; # ( はてな
s/\)/&#x29;/g; # ) はてな
s/\|/&#x7C;/g; # | はてな

# 変換結果を返す
return $_;
}

# 1行ずつ書き込む
sub putLine
{
print @_;
}

実行形式


C:\Perl5>SourceToHtml4.plx input.txt > output.txt