2
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 1

TclでHello, world!

Posted at

前置き

ActiveTcl 8.6 + Windows10 Japanese 64bitで動作確認しました。

スクリプト冒頭3行の

#!/bin/sh
#\
	exec tclsh "$0" ${1+"$@"}

#!/bin/sh
#\
	exec wish "$0" ${1+"$@"}

はActiveTclで実行する場合は不要なので以下では省略します。UNIX OSやCygwinでファイル名だけで実行する場合は必要です。

Tcl編

実行方法
> c:\activetcl\bin\tclsh.exe hello.tcl

シンプルにHello, world! ~その1~

hello.tcl
puts {Hello, world!}

シンプルにHello, world! ~その2~

hello.tcl
puts "Hello, world!"

標準エラー出力にHello, world!

hello.tcl
puts stderr "Hello, world!"

細切れにHello, world!

hello.tcl
puts -nonewline "Hello"
puts -nonewline ", world!"
puts -nonewline "\n"

printf風にHello, world!

hello.tcl
puts -nonewline [format "%s, %s!\n" Hello world]

変数に入れてからHello, world! ~その1~

hello.tcl
set foo "Hello, world!"
puts $foo

変数に入れてからHello, world! ~その2~

hello.tcl
set foo Hello
set bar world
puts "$foo, $bar!"

変数に入れてからHello, world! ~その3~

hello.tcl
set foo Hello
set bar world
puts "[set foo], [set bar]!"

リストをつなげてHello, world!

hello.tcl
set foo [list Hello world!]
puts [join $foo ", "]

関数の処理でHello, world!

hello.tcl
proc helloworld {} {
    puts "Hello, world!"
}
helloworld

関数の戻り値でHello, world!

hello.tcl
proc helloworld {} {
    return "Hello, world!"
}
puts [helloworld]

シリアルポートにHello, world!

COM1とCOM2がクロスケーブルで接続されているとして。

hello.tcl
set ch1 [open "\\\\.\\COM1" r+]
fconfigure $ch1 -mode 9600,n,8,1
set ch2 [open "\\\\.\\COM2" r+]
fconfigure $ch2 -mode 9600,n,8,1 -buffering line
puts $ch2 "Hello, world!"
close $ch2
puts [gets $ch1]
close $ch1
2
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
2
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?