This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] "passthru" function in TCL - how to do it

Help for those learning Tcl or writing their own scripts.
Post Reply
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

[SOLVED] "passthru" function in TCL - how to do it

Post by dj-zath »

this ones probably simple, unfortunately, I can't find anything on it mainly cause I can't explain what I need to do in a few simple words...

so, here goes with an example:


$artist = "wang chung"
$song = "Dance Hall days"

a template file named "playlist.tpl" contains:

"$artist - $song"

now we set it up...

Code: Select all

if {([file exists "/path/to/playlist.tpl"])} {
  set TmpPL "[read [open "/path/to/playlist.tpl" r]]"
} else {
  putlog {WARNING - Playlist Template File Not Found!}
}
thats the EASY part...

Now, I have a proc that generates $artist and $song and I want them to be "inserted" into $TmpPL where $artist = "wang chung" and $song is "dance Hall days" this changes dynamically.. and will be called to do so every 2 seconds...

if I did it "manually" from in the proc; say:

Code: Select all

set TmpPL "$artist - $song"
it works.. but since its not pratical to actually generate code for the entire webpage in which it is, I need to do it "externally"

the only part I'm stuck on is how to "replace" or "insert" the varables from a proc internally into an external file and load it as a new varable...

PHP calls this a passthru TCL has NO such function that I can find or figure out..

-DjZ-
:) :)
Last edited by dj-zath on Sat Apr 04, 2009 4:18 am, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Taken from php.net:
The passthru() function is similar to the exec() function in that it executes a command . This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser. A common use for this is to execute something like the pbmplus utilities that can output an image stream directly. By setting the Content-type to image/gif and then calling a pbmplus program to output a gif, you can create PHP scripts that output images directly.
The equivialent of this in tcl would look something like this:

Code: Select all

puts stdout [exec "command"]
However, I doubt that's what you are asking for. Rather I believe you're looking for templates. Best approach here might vary from case to case, but using regsub with suitable regular expressions is usually a good start.
NML_375
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Another way you could do templates, although great care would be required, would be to read the template into a variable, and use the subst command to do variable substitution.
NML_375
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

Hi nml_375

yeah, I have "slept on it". and reached the same conclusion...

the answer was VERY simple: after reading some explanations in the "TCL FAQ" section on this forum, I came up with the following code snippet:

Code: Select all

regsub -all -- "\\\$SNG-0" $TmpPLY "$VarC" MetaPL;
regsub -all -- "\\\$SNG-1" $MetaPL "$VarD" MetaPL;
regsub -all -- "\\\$SNG-2" $MetaPL "$VarE" MetaPL;
regsub -all -- "\\\$SNG-3" $MetaPL "$VarF" MetaPL;
regsub -all -- "\\\$SNG-4" $MetaPL "$VarG" MetaPL;
regsub -all -- "\\\$SNG-5" $MetaPL "$VarH" MetaPL;
regsub -all -- "\\\$SNG-6" $MetaPL "$VarI" MetaPL;
regsub -all -- "\\\$SNG-7" $MetaPL "$VarJ" MetaPL;
regsub -all -- "\\\$SNG-8" $MetaPL "$VarK" MetaPL;
regsub -all -- "\\\$SNG-9" $MetaPL "$VarL" MetaPL;
Note here that "string map" did not work; and I believe it is because string map couldn't "import" the local varable within the expression..

Code: Select all

set MetaPL [string map {"\\\$SNG-1" "$VarC"} $TmpPLY]
simply returns "$VarC" instead of "Take The Skinheads Bowling - Camper Van Beethoven"

again, thanks nma_375 for your help... (I tell you, I'm getting stupider by the day!)

-DjZ-
:) :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

String map should work just fine, as long as you allow the tcl interpreter to do the appropriate substitutions..

Hint: Enclosing a parameter with {} tells the interpreter to do no further substitutions in the current evaluation.

Code: Select all

set MetaPL [string map \
 [list {\$SNG-0} $VarC \
  {\$SNG-1} $VarD \
  {\$SNG-2} $VarE \
  {\$SNG-3} $VarF \
  {\$SNG-4} $VarG \
  {\$SNG-5} $VarH \
  {\$SNG-6} $VarI \
  {\$SNG-7} $VarJ \
  {\$SNG-8} $VarK \
  {\$SNG-9} $VarL] \
 $TmpPLY]
NML_375
Post Reply