{"id":37406,"date":"2018-04-15T03:01:59","date_gmt":"2018-04-14T17:01:59","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=37406"},"modified":"2018-04-15T08:26:48","modified_gmt":"2018-04-14T22:26:48","slug":"tcl-and-javascript-and-php-calculator-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/tcl-and-javascript-and-php-calculator-tutorial\/","title":{"rendered":"Tcl and Javascript and PHP Calculator Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Tcl and Javascript and PHP Calculator Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator_more.jpg\" title=\"Tcl and Javascript and PHP Calculator Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Tcl and Javascript and PHP Calculator Tutorial<\/p><\/div>\n<p>It&#8217;s a &#8220;blown mind&#8221; that has led me to involve Javascript to make the incarnation of our calculator of <a title='Tcl and PHP Calculator Tutorial' href='#tphpct'>Tcl and PHP Calculator Tutorial<\/a> allow for some round bracket functionality around the TCL (expr) &#8220;kernel&#8221; <font size=1>(sorry, Unix &#8230; my apologies <a target=_blank title='?' href='https:\/\/www.youtube.com\/watch?v=Wd-p8RUEo-Y'><strike>Linus<\/strike><\/a>Linux)<\/font>.  The nexus between what a user may enter, versus the hierarchy needs of TCL&#8217;s expr command just blew my mind, <strike>man<\/strike>person!<\/p>\n<p>So what did we turn to?  <a target=_blank title='?' href='https:\/\/www.google.com.au\/search?client=firefox-b-ab&#038;dcr=0&#038;biw=1280&#038;bih=674&#038;ei=QGnRWqafMIiq8QWY5qGgCA&#038;q=ending+English+sentence+with+preposition&#038;oq=ending+English+sentence+with+preposition&#038;gs_l=psy-ab.3...12240.23916.0.24046.40.29.0.0.0.0.548.4556.0j2j9j3j0j2.16.0....0...1c.1.64.psy-ab..24.16.4546...0j35i39k1j0i67k1j0i131k1j0i131i46k1j46i131k1j0i131i67k1j0i20i263k1j0i22i30k1j0i8i13i30k1j33i22i29i30k1.0.eMs3Y1UdjfU'>Modernizing<\/a> one&#8217;s thinking?  We&#8217;re onto it?  But we digress.  <a target=_blank title='?' href='https:\/\/www.google.com.au\/search?client=firefox-b-ab&#038;dcr=0&#038;biw=1280&#038;bih=674&#038;ei=XWnRWtCoI4a98QWBwJiIDg&#038;q=starting+English+sentence+with+but&#038;oq=starting+English+sentence+with+but&#038;gs_l=psy-ab.3..33i22i29i30k1l6.118128.124600.0.124744.11.10.0.0.0.0.350.978.0j1j2j1.4.0....0...1c.1.64.psy-ab..7.1.216....0.fU1yqbht2TI'>Well<\/a>, well.<\/p>\n<p>We turned to Javascript&#8217;s <a target=_blank href='http:\/\/www.w3schools.com\/jsref\/jsref_eval.asp' title='Javascript eval'>eval<\/a> function, not PHP&#8217;s <a target=_blank title='PHP eval method information' href='http:\/\/php.net\/manual\/en\/function.eval.php'>eval<\/a>, nor TCL&#8217;s <a target=_blank title='TCL eval' href='http:\/\/wiki.tcl.tk\/eval'>eval<\/a> version, thanks, but no thanks.  Nevertheless <font size=1>&#8230; and didn&#8217;t you just know that &#8220;but&#8221; would be insulted by the paragraph above?! &#8230;<\/font> use of Javascript eval (for round bracketing scenarios or operator hierarchy scenarios) is not the &#8220;cakewalk&#8221; that you may imagine.  That is because &#8230;<\/p>\n<p><code><br \/>\n6 ** 2 \/\/ in TCL \"to the power of\" operator syntax (which remains our web application display choice for the user) has ...<br \/>\nMath.<a target=_blank title='Math.pow() information from w3schools' href='https:\/\/www.w3schools.com\/jsref\/jsref_pow.asp'>pow<\/a>(6, 2)  \/\/ Javascript equivalent (behind the scenes)<br \/>\n<\/code><\/p>\n<p> &#8230; so we need to cater for that possibility.  The Javascript logic is called at the document.body <a target=_blank title='Javascript event onload information from w3schools' href='http:\/\/www.w3schools.com\/tags\/ev_onload.asp'><i>onload<\/i><\/a> event.<\/p>\n<p>As <a target=_blank title='An Interpretive Style of Web Programming Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/an-interpretive-style-of-web-programming-tutorial\/'>we often do here<\/a>, the &#8220;progression&#8221; of logic was to convert some &#8220;hardcoded&#8221; HTML text (specifically &#8220;First Number:&#8221; and &#8220;:Second Number&#8221;) into HTML select (dropdown) elements to the left and to the right (respectively) of the &#8220;TCL kernel&#8221;, with additional options for the round bracketing the user may want.  These dropdowns move out to the edges of the webpage as they make room for user entered additions to the calculation complexity of what they want to calculate.  The &#8220;Math.pow()&#8221; logic needed benefits from the event timing thoughts on the dropdown&#8217;s <i>onchange<\/i> event logic, otherwise you&#8217;d be doing that dreaded of all dreadeds, in many programmers&#8217; worlds methinks, tracking back through entered &#8220;calculator equation&#8221; texts to discover when a numerical value becomes an operator, a horrible job when a minus sign can be either, for example.  These same &#8220;horrible&#8221; programming tasks would have been at the fore to making a &#8220;TCL only backend solution&#8221; come to pass, we are sure, too.<\/p>\n<p>Can you see from all this how well (clientside) Javascript works with (serverside) PHP?  They are a very potent duo, because it is like two bites at the cherry, every time, with their different timings of the application of their logics.<\/p>\n<p>The downloadable PHP source code <a target=_blank title='calculator.php' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php-GETME\">calculator.php<\/a> changed <a target=_blank title='calculator.php' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php-GETME\">this way<\/a> to achieve this extension of functionality still supervising an unchanged <a target=_blank title='calculator.tcl' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.tcl_GETME\">calculator.tcl<\/a> TCL source code script, and here is a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php\" title=\"Click picture\">live run<\/a> link for you to try this calculator for yourself.<\/p>\n<hr>\n<p id='tphpct'>Previous relevant <a target=_blank title='Tcl and PHP Calculator Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/tcl-and-php-calculator-tutorial\/'>Tcl and PHP Calculator Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Tcl and PHP Calculator Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/TCL\/calculator.jpg\" title=\"Tcl and PHP Calculator Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Tcl and PHP Calculator Tutorial<\/p><\/div>\n<p>The <a title='Tcl and PHP Primer Tutorial' href='#tphppt'>Tcl and PHP Primer Tutorial<\/a> was a foretaste to today&#8217;s calculator web application that &#8230;<\/p>\n<ul>\n<li>uses PHP as a frontend and (HTML) form navigation destination &#8230;<\/li>\n<li>connects to backend via PHP <a target=_blank title='PHP exec() method information' href='http:\/\/php.net\/manual\/en\/function.exec.php'>exec<\/a> &#8230;<\/li>\n<li>uses TCL as backend &#8230; via &#8230;<br \/>\n<code><br \/>\nexec(\"tclsh <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.tcl_GETME' title='calculator.tcl'>calculator.tcl<\/a> &lt; calculator.txt | tail -1 &gt; calculator.out\");<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p>Crucial to the TCL&#8217;s backend role is TCL&#8217;s <a target=_blank title='Tcl expr command information' href='https:\/\/www.tcl.tk\/man\/tcl\/TclCmd\/expr.htm'>expr<\/a> command, as a determinant regarding what today&#8217;s calculator is capable of.<\/p>\n<p>Here is a link to some downloadable PHP source code <a target=_blank title='calculator.php' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php_GETME\">calculator.php<\/a> supervising <a target=_blank title='calculator.tcl' href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.tcl_GETME\">calculator.tcl<\/a> TCL source code, and here is a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/calculator.php\" title=\"Click picture\">live run<\/a> link.<\/p>\n<hr>\n<p id='tphppt'>Previous relevant <a target=_blank title='Tcl and PHP Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/tcl-and-php-primer-tutorial-...-http:\/\/www.tcl.tk\/software\/tcltk\/\/'>Tcl and PHP Primer Tutorial &#8230; http:\/\/www.tcl.tk\/software\/tcltk\/<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/TCL\/domain.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Tcl and PHP Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\" src=\"http:\/\/www.rjmprogramming.com.au\/TCL\/domain.jpg\" title=\"Tcl and PHP Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Tcl and PHP Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/<\/p><\/div>\n<p>Have you heard of <a target=_blank title='Tcl\/Tk landing page' href='http:\/\/www.tcl.tk\/software\/tcltk\/'>Tcl\/Tk?<\/a>   Think maybe you could introduce yourself to it with <a target=_blank href='#tsbpt' title='Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial'>Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial<\/a> as shown below.<\/p>\n<p>The Tcl in Tcl\/Tk refers to the scripting side of things, while the Tk is a GUI framework.   On some (maybe lots) of Linux or unix web servers, such as the one for this domain here at www.rjmprogramming.com.au Tcl scripting is available and that means Tcl could be used by PHP, as a server side language to use the <a target=_blank title='PHP exec information' href='http:\/\/php.net\/manual\/en\/function.exec.php'><i>exec<\/i><\/a> method to ask something of Tcl and get something back, half of which is the arrangement we&#8217;d like to have when feeding our babies.<\/p>\n<p>Today we write a webpage for finding the Country of Origin of a designated URL where PHP supervises Tcl:<\/p>\n<ol>\n<li>ask for a URL in PHP,<\/li>\n<li>send that to the same PHP with a different call &#8230;<\/li>\n<li>and use PHP exec method to send a Tcl commend line command to Tclsh and output this to a file known by the PHP,<\/li>\n<li>get the PHP to read the file of step 3. and send that information in a modified call of this same PHP,<\/li>\n<li>write out the Tcl based findings to the webpage in PHP,<\/li>\n<li>ask for a URL in PHP<\/li>\n<\/ol>\n<p>Here is a link to some downloadable PHP source code <a target=_blank title='domain.php' href=\"http:\/\/www.rjmprogramming.com.au\/TCL\/domain.php_GETME\">domain.php<\/a> supervising <a target=_blank title='domain.tcl' href=\"http:\/\/www.rjmprogramming.com.au\/TCL\/domain.tcl_GETME\">domain.tcl<\/a> TCL source code, and here is a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/TCL\/domain.php\" title=\"Click picture\">live run<\/a> link.<\/p>\n<p>Thanks to:<\/p>\n<ul>\n<li><a target=_blank title='link' href='http:\/\/zetcode.com\/lang\/tcl\/flowcontrol\/'>Flow control in Tcl<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/dbaspot.com\/shell\/362549-tcsh-argv-loses-quotes-messes-argument-grouping-up.html'>[tcsh] $argv loses quotes, messes argument grouping up<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/bin-co.com\/tcl\/articles\/variables_in_arrays.php'>Using Variables in Associative Arrays<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/docstore.mik.ua\/orelly\/unix3\/upt\/ch37_05.htm'>Quoting and Command-Line Parameters<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/books.google.com.au\/books?id=BJpkh3RdG_UC&#038;pg=PA45&#038;lpg=PA45&#038;dq=%22tclsh+split%22&#038;source=bl&#038;ots=PpQlwTydXv&#038;sig=NIqU0g5LTutl9QEmakxbRCxVhpw&#038;hl=en&#038;sa=X&#038;ei=OSk_VJXRLMLCmQWms4DACA&#038;ved=0CB8Q6AEwAA'>Exploring Expect: A Tcl-based Toolkit for Automating Interactive Programs<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/tmml.sourceforge.net\/doc\/tcl\/split.html'>Split a string into a proper Tcl list<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/en.wikipedia.org\/wiki\/Country_code_top-level_domain'>Country code top-level domain<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/www.thrall.org\/domains.htm'>Top Level Domain Names and Country Codes<\/a><\/li>\n<li><a target=_blank title='link' href='http:\/\/www.tutorialspoint.com\/tcl-tk\/pdf\/tcl_strings.pdf'>Tcl &#8211; Strings<\/a><\/li>\n<\/ul>\n<hr \/>\n<p id='tsbpt'>Previous relevant <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=664' title='Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial'>Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/TCL\/MacExample\/\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\" src=\"http:\/\/www.rjmprogramming.com.au\/TCL\/MacExample\/TCL_Example_1of.png\" title=\"Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\"  style=\"float:left;\" \/><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\" src=\"http:\/\/www.rjmprogramming.com.au\/TCL\/MacExample\/TCL_Example_4of.png\" title=\"Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Tcl\/Tk So Brilliant (but where do you start?) Primer Tutorial ... http:\/\/www.tcl.tk\/software\/tcltk\/<\/p><\/div>\n<p>Have you heard of Tcl\/Tk?<\/p>\n<blockquote><p>Tcl\/Tk is open source (based on a BSD-style license), so you can use it and modify it virtually any way you want, including for commercial uses.<\/p><\/blockquote>\n<p>With Tcl\/Tk have used in conjunction with C++ and Python (look out for Tkinter).<\/p>\n<p>Use it for Games Programming (it makes pretty cute GUIs &#8230; you may disagree?!) and it used to work back at Mac OS X 10.5.8\/XCode 3.0 (am going to try Mac OS X 10.7.5 &#8230; any advice anywhere?) and had it going on Windows XP with Visual Studio Express as well.   It has a lot of cross-platform strengths.<\/p>\n<p>Think Tcl\/Tk is really really good.<\/p>\n<p>In this primer  <a target=_blank title='click picture' href='http:\/\/www.rjmprogramming.com.au\/TCL\/MacExample\/'>tutorial<\/a> you can see a Draw Poker game as an XCode project, see it Build and in action, then see how to arrange for *.dmg Package Distribution via Package Manager.<\/p>\n<p>Link to Tcl\/Tk website &#8230; <a target=_blank href='http:\/\/www.tcl.tk\/software\/tcltk\/' title='Home of Tcl\/Tk'>Home of Tcl\/Tk &#8230; where quote up the top resides<\/a><br \/>\nLink to Tcl\/Tk jobs done &#8230; <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/TCL\/' title='Examples of use of Tcl\/Tk'>Tcl\/Tk jobs done<\/a> &#8230; personal experience\n<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d664' onclick='var dv=document.getElementById(\"d664\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?cat=51\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d664' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d10261' onclick='var dv=document.getElementById(\"d10261\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Tcl\/Tk\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d10261 style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d37343' onclick='var dv=document.getElementById(\"d37343\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/exec\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d37343' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d37406' onclick='var dv=document.getElementById(\"d37406\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/javascript\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d37406' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a &#8220;blown mind&#8221; that has led me to involve Javascript to make the incarnation of our calculator of Tcl and PHP Calculator Tutorial allow for some round bracket functionality around the TCL (expr) &#8220;kernel&#8221; (sorry, Unix &#8230; my apologies &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/tcl-and-javascript-and-php-calculator-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,33,37],"tags":[125,2251,212,399,400,405,2524,468,652,752,870,2269,932,997,1105,1122,1245,1247,1319],"class_list":["post-37406","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-software","category-tutorials","tag-backend","tag-calculator","tag-client","tag-eval","tag-event","tag-exec","tag-expr","tag-frontend","tag-javascript","tag-mathematics","tag-onload","tag-operator","tag-php","tag-programming","tag-script","tag-server","tag-tcl","tag-tclsh","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/37406"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=37406"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/37406\/revisions"}],"predecessor-version":[{"id":37436,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/37406\/revisions\/37436"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=37406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=37406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=37406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}