[[Perl]]

* ツール

**文字コード変換

 #!/usr/bin/perl
 
 =head1 DESCRIPTION
 
 	文字コードをSJISに
 	改行コードはCRLFに
 
 =cut
 
 use strict;
 use Jcode;
 
 undef $/;
 
 foreach my $path (@ARGV) {
 	open(IN,"$path")
 		or warn("can't open $path");
 	binmode(IN);
 	my $src = <IN>;
 	if ($src =~ /\x0D\x0A/) {
 		# CRLFが存在する
 	} else {
 		# CRLFが存在しない
 		$src =~ s/\x0A/\x0D\x0A/g;
 	}
 	$src = &Jcode::convert($src,'sjis');
 	close(IN);
 	open(OUT,">$path")
 		or warn("can't open $path");
 	binmode(OUT);
 	print OUT $src;
 	close(OUT);
 }

 #!/usr/bin/perl
 
 =head1 DESCRIPTION
 
 	文字コードをEUCに
 	改行コードはLFに
 
 =cut
 
 use strict;
 use Jcode;
 
 undef $/;
 
 foreach my $path (@ARGV) {
 	open(IN,"$path")
 		or warn("can't open $path");
 	binmode(IN);
 	my $src = <IN>;
 	$src =~ s/\x0D\x0D/\x0A/g;
 	$src =~ s/\x0D\x0A/\x0A/g;
 	$src = &Jcode::convert($src,'euc');
 	close(IN);
 	open(OUT,">$path")
 		or warn("can't open $path");
 	binmode(OUT);
 	print OUT $src;
 	close(OUT);
 }

	#!/usr/bin/perl
	
	=head1 DESCRIPTION
	
		expand - | unexpand -t 4 -
		を同じファイルで書き込み
		
		あるいはその逆操作
	
		ファイル以外は無視する
	
	=head1 SYNOPSIS
	
		tab8to4 [-r] files
	
	=cut
	
	use strict;
	use Text::Tabs;
	
	my $BEFORE	= 8;
	my $AFTER	= 4;
	if ($ARGV[0] eq '-r') {
		shift;
		$BEFORE	= 4;
		$AFTER	= 8;
	}
	
	foreach my $path (@ARGV) {
		if (not -f $path) {
			warn("$path is not regular file.\n");
			next;
		}
		open(IN,"$path")
			or warn("can't open $path");
	
		my $src = '';
		while (my $line = <IN>) {
			# 変換
			$tabstop = $BEFORE;
			$line = expand($line);
			$tabstop = $AFTER;	
			$line = unexpand($line);
			$src .= $line;
		}
		close(IN);
		
		open(OUT,">$path")
			or warn("can't write $path");
		print OUT $src;
		close(OUT);
		
		print "done $path.\n";
	}

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS