| View previous topic :: View next topic |
| Author |
Message |
dj-zath Op
Joined: 15 Nov 2008 Posts: 134
|
Posted: Fri Apr 03, 2009 11:49 am Post subject: [SOLVED] "passthru" function in TCL - how to do it |
|
|
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: |
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: |
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Apr 03, 2009 12:23 pm Post subject: |
|
|
Taken from php.net:
| Quote: | | 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: | | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Apr 03, 2009 12:48 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
dj-zath Op
Joined: 15 Nov 2008 Posts: 134
|
Posted: Sat Apr 04, 2009 1:36 am Post subject: |
|
|
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: |
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: |
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-
 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Apr 05, 2009 9:18 am Post subject: |
|
|
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: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|