Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Urgent: Same code for different OS?

Status
Not open for further replies.

tmpalaniselvam

Programmer
May 14, 2000
68
IN
Hi!
We Know that tcl is an multi-platform language.
Is it possible, to utilize same code written in Windows OS to other OS like linux, Unix without any modification?
Always welcome your comments.. Thanks and Regards,
Palani.
mail to: tmpalaniselvam@yahoo.com
 
With the exception of certain hard coded portions, which
include file pathnames, etc.., yes.
Of course this only applies to "base" tcl and wish. There
are platform specific extension commands for unix that are not available for windows, though I'm not aware of any the other way around...hmmm
 
Quite possible, lots of people are doing it. As a small example, virtually all of the programs used in the classes I teach are platform-independent, and I run them without change on both our Windows and Linux configurations. They exercise such areas as basic Tcl functionality, file I/O, GUI management, and TCP/IP socket-based communication.

As jerkie mentioned, some Tcl extensions aren't platform-independent, so you need to check the documentation for whichever extensions you plan to use. For example, Expect runs on Unix platforms only. But in general, an extension written in pure Tcl code only is platform independent. And there are some Windows-only extensions, jerkie, mainly providing access to Windows-specific functionality, like registry (for obvious reasons); dde, tcom, and optcl for interprocess communication; tkprint for printing under Windows; winico for twiddling with the icon in the Windows titlebar and installing applications in the taskbar; and winutils for access to other Windows functionality. On the Tcl'ers Wiki ( see the page "Windows specific Tcl commands," for a more complete listing.

As for making sure that your application is platform independent, it's fairly easy to do in Tcl. Just be careful whenever you access the environment in any way.
[ul][li]File I/O is usually safe, as Tcl automatically translates different end-of-line conventions when it reads from a channel, and uses the platform-native convention whenever it writes to a channel.[/li]
[li]File paths are usually the biggest problem. Get in the habit of using the file join command to construct platform-safe paths all the time, use relative pathnames when accessing other modules of your application, and read about Tcl introspection commands like info script which returns the path of the script currently executing.[/li]
[li]Use glob for getting directory listings instead of trying to execute the system's ls or dir command. Yes, glob is rather brain-dead, IMHO, but it is Tcl-native.[/li]
[li]Similarly, use other Tcl built-ins whenever possible, like file copy, file delete, file rename, file mkdir, cd, and pwd.[/li]
[li]Use the Tcl clock command for time and date manipulation.[/li]
[li]Avoid executing other programs unless absolutely necessary. This applies to both execing another program and opening a pipe to another program. If you do, you'll likely need to provide customized code for different platforms you support. Which brings up...[/li]
[li]To test what platform your script is running on, check the value of the predefined global tcl_platform(platform) array element. Its value will be "macintosh", "unix", or "windows". You can then switch based on this value to execute any platform-specific code you need to include.[/li][/ul]

Those are the basics that I can think of for writing platform-independent code. The Tcl'ers Wiki has the page "Tcl built-ins for cross platform functionality," providing links to more pages giving more details about these techniques.

Also, it would be a good idea to pick up TclPro and use TclPro Checker, a Tcl syntax checker. Besides being a good tool for ensuring that your script doesn't have obvious syntax errors, TclPro Checker will also raise warnings if it finds any blatant platform-dependent code in your script. You can find out more information about TclPro and download the free version from Also, ActiveState is now offering a "quality-assured" version of TclPro, which includes updates for the latest version of Tcl, as part of its for-sale ASPN Tcl release. More information on that is available at - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hi!
Thanks a lot!.
Jones, I appreciate your helping tendency. I got the concept...
Thanks and Regards,
Palani.
mail to: tmpalaniselvam@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top