Posted to tcl by aspect at Wed Apr 15 00:16:42 GMT 2015view raw

  1. # returns a script which composes the given list of commands:
  2. # this has elsewhere been called [pipe], [cmdpipe], [~>]
  3. proc cmdpipe args {
  4. set anonvar ~
  5. set args [lassign $args body]
  6. foreach cmd $args {
  7. if {[string first $anonvar $cmd] >= 0} {
  8. set body [string map [list $anonvar "\[$body\]"] $cmd]
  9. } else {
  10. set body "$cmd \[$body\]"
  11. }
  12. }
  13. set body
  14. }
  15.  
  16. # example:
  17. -- {
  18. set s [cmdpipe {*}{
  19. {open /etc/passwd r}
  20. {read}
  21. {string trim}
  22. {split ~ \n}
  23. {lindex ~ end}
  24. {split ~ :}
  25. {lindex ~ 4}
  26. {puts}
  27. }]
  28. }
  29.  
  30. # another example:
  31. proc lremove {_ls args} {
  32. set script [cmdpipe [list set $_ls] {*}[map {list lsearch -exact -not -all -inline ~} $args] [list set $_ls]]
  33. tailcall try $script
  34. }
  35.