something like perl's select

11 views
Skip to first unread message

Wenchi

unread,
Sep 13, 2002, 11:02:42 AM9/13/02
to
Within perl is the ability to set a default destination for print
commands.

For example, doing

open F, "> /some/file" or die;
select(F);
print "Hello there\n";

will print text into /some/file without the handle to be passed to
the print command.

Is there something like this for tcl?

WL

Glenn Jackman

unread,
Sep 13, 2002, 11:49:50 AM9/13/02
to

Not built-in, I don't think, but you can create it:

proc select args {
global _selected_channel
if {[llength $args] == 0} {
if {![info exists _selected_channel]} {
set _selected_channel stdout
}
return $_selected_channel
} elseif {[llength $args] == 1} {
set _selected_channel $args
} else {
error "wrong # args: [lindex [info level 0] 0] ?channelId?"
}
}

proc print string {
puts [select] $string
}

--
Glenn Jackman
gle...@ncf.ca

Glenn Jackman

unread,
Sep 13, 2002, 11:55:36 AM9/13/02
to
Wenchi <wliao@freeN0:5P@Mshell:com.freeshell.org> wrote:

Not built-in, I don't think, but you can create it:

proc select args {
global _selected_channel
if {[llength $args] == 0} {
if {![info exists _selected_channel]} {
set _selected_channel stdout
}
return $_selected_channel
} elseif {[llength $args] == 1} {
set _selected_channel $args
} else {
error "wrong # args: [lindex [info level 0] 0] ?channelId?"
}
}

proc print string {
puts [select] $string
}

set f1 [open /tmp/f1 w]
set f2 [open /tmp/f2 w]

print "line1"
select $f1
print "line2"
select $f2
print "line3"
select stdout
print "line4"


--
Glenn Jackman
gle...@ncf.ca

Glenn Jackman

unread,
Sep 13, 2002, 12:05:10 PM9/13/02
to
Wenchi <wliao@freeN0:5P@Mshell:com.freeshell.org> wrote:

Not built-in, I don't think, but you can create it:

proc select {{channelId ""}} {
global _selected_channel
if {[string length $channelId] == 0} {


if {![info exists _selected_channel]} {
set _selected_channel stdout
}

} else {
set _selected_channel $channelId
}
return $_selected_channel

Reply all
Reply to author
Forward
0 new messages