1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TclAdvent Calendar 2018

Day 2

TclでHello, world! ~その2~

Posted at

Tcl編 ~その2~

多言語対応でHello, world!

実行環境のデフォルトの言語で挨拶するときは

> c:\activetcl\bin\tclsh.exe hello.tcl

明示的に言語を指定するときは

> c:\activetcl\bin\tclsh.exe hello.tcl ja

以下のように訳文を用意していない言語を指定するとROOT.msgが使われるみたい。

> c:\activetcl\bin\tclsh.exe hello.tcl en
hello.tcl
#-*- mode:tcl; coding:cp932 -*-
package require msgcat
namespace import ::msgcat::mc
::msgcat::mcload [file join [file dirname [info script]] msgs]
lassign $argv locale
if {$locale ne ""} {
	::msgcat::mclocale $locale
}
puts [mc MSG1]
msgs/ROOT.msg
#-*- mode:tcl; coding:utf-8 -*-
::msgcat::mcflmset {
	MSG1 "Hello, world!"
}
msgs/ja.msg
#-*- mode:tcl; coding:utf-8 -*-
::msgcat::mcflmset {
	MSG1 "こんにちは、世界!"
}

プロセス間通信でHello, world!

コマンドプロンプトを2つ開いて、一方で

> c:\activetcl\bin\tclsh.exe hello1.tcl

のようにサーバーを実行し、他方で

> c:\activetcl\bin\tclsh.exe hello2.tcl

のようにクライアントを実行すると、クライアント側にHello, world!が表示される。

TCP

hello1.tcl
proc echo {sock} {
	set data [gets $sock]
	if {[eof $sock]} {
		close $sock
	} else {
		puts $sock $data
	}
}
proc listen {sock host port} {
	fconfigure $sock -buffering line -encoding utf-8 -translation crlf
	fileevent $sock readable [list echo $sock]
}
socket -server listen 9999
vwait forever
hello2.tcl
set sock [socket localhost 9999]
fconfigure $sock -buffering line -encoding utf-8 -translation crlf
puts $sock "Hello, world!"
puts [gets $sock]

DDE

hello1.tcl
package require dde
set response ""
proc echo {data} {
	global response
	set response $data
}
dde servername -handle echo -- Echo
vwait forever
hello2.tcl
package require dde
dde execute TclEval Echo "Hello, world!"
puts [dde request TclEval Echo response]
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?