Merge branch 'LCTT:master' into master

This commit is contained in:
宋锦丰 2023-02-17 11:39:47 +08:00 committed by GitHub
commit 972b15b15b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 243 additions and 238 deletions

View File

@ -3,23 +3,26 @@
[#]: author: "Gopala Krishna Behara https://www.opensourceforu.com/author/gopalakrishna-behara/"
[#]: collector: "lkxed"
[#]: translator: "onionstalgia"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-15548-1.html"
为什么企业应该选择平台即服务PaaS
======
*<ruby>平台即服务<rt>PaaS</rt></ruby> "能够快速、轻松地创建网络应用,而无需购买和维护其下的软件和基础设施。本文解释了它为什么有用。*
平台即服务(以下简称 PaaS是一种“免去了建立和维护基础设施的复杂工作为客户提供开发、运行和管理应用程序的平台”的云计算服务。这是云原生应用和支持系统所依托的核心平台。
![][0]
> 平台即服务能够快速、轻松地创建网络应用,而无需购买和维护其下的软件和基础设施。本文解释了它为什么有用。
<ruby>平台即服务<rt>PaaS</rt></ruby>(以下简称 PaaS指的是云计算服务它为客户提供了开发、运行和管理应用程序的平台而免去了建立和维护与开发和启动应用程序相关的基础设施的复杂工作。这是云原生应用和支持系统所依托的核心平台。
PaaS 通常包括不同的应用基础功能,包括应用平台、集成平台、业务分析平台、事件流服务和移动后端服务。此外,它还包括一套与监控、管理、部署相关的功能。
开发人员希望他们的开发环境不需要等待而运营团队则更关心性能和稳定性。这经常引起两方间的冲突。PaaS 为这两方创造了和平的环境。作为服务交付的应用平台,即 PaaS被用于部署用户代码。Cloud Foundry、Cloudify 和 OpenShift 这些开源环境都可用作 PaaS。
开发人员希望他们的开发环境不需要等待而运营团队则更关心性能和稳定性。这经常引起两方间的冲突。PaaS 为这两方创造了和平的环境。一个作为服务交付的应用平台被称作 PaaS被用于部署用户代码。Cloud Foundry、Cloudify 和 OpenShift 这些开源环境都可用作 PaaS。
### PaaS 的采用模式
云计算必须满足五个基本特征——按需服务、接入网络、资源池化、弹性和可度量的服务。为此云计算提供了三种服务模式软件即服务SaaS、平台即服务PaaS、基础设施即服务IaaS
云计算必须满足五个基本特征按需服务、接入网络、资源池化、弹性和可度量的服务。为此,云计算提供了三种服务模式:<ruby>软件即服务<rt>Software as a Service</rt></ruby>SaaS<ruby>平台即服务<rt>Platform as a Service</rt></ruby>PaaS<ruby>基础设施即服务<rt>Infrastructure as a Service</rt></ruby>IaaS
业务选用 PaaS 的关键驱动力:
@ -27,25 +30,25 @@ PaaS 通常包括不同的应用基础功能,包括应用平台、集成平台
* 通过减少应用程序的交付时间和提高开发和交付质量,最大限度地降低 IT 成本
* 增加中间件之间的灵活性和集成度
**简单 PaaS** 是踏入 PaaS 领域的入门。它可以提供应用程序服务,并将它们暴露在自助服务的目录中,自动部署和计量服务使用的资源。
*简单 PaaS*:踏入 PaaS 领域的入口。它可以提供应用程序服务,并将它们暴露在自助服务的目录中;自动部署和计量服务使用的资源。
*管理 PaaS* 管理已配置应用程序的<ruby>服务级别协议<rt>SLA</rt></ruby><ruby>服务质量<rt>QoS</rt></ruby>,例如弹性、应用程序性能、安全性等。
*管理 PaaS*管理已配置应用程序的<ruby>服务级别协议<rt>SLA</rt></ruby><ruby>服务质量<rt>QoS</rt></ruby>,例如弹性、应用程序性能、安全性等。
*编程 PaaS* 允许应用程序与外部应用程序或公共云集成,并实现自动扩展和云爆发场景。
*编程 PaaS*允许应用程序与外部应用程序或公共云集成,并实现自动扩展和云爆发场景。
*<ruby>面向流程<rt>Process-oriented</rt></ruby> PaaS* 允许通过创建持续交付流程来实现<ruby>开发运维<rt>DevOps</rt></ruby>流程,该流程可以自动构建、测试应用程序并将其交付到云环境中。
*面向流程 PaaS*允许通过创建持续交付流程来实现<ruby>开发运维<rt>DevOps</rt></ruby>流程,该流程可以自动构建、测试应用程序并将其交付到云环境中。
除了这些采用模式之外,还有其他的 PaaS 变体如下,这些变化可能与上文的模式有一定重合
除了这些采用模式之外,还有其他的 PaaS 变体如下,这些变化可能与上文的模式有一定重合
**<ruby>集成平台即服务<rt>iPaaS</rt></ruby>**是一套能够开发、执行和管理集成流的云服务。集成流可以是个人内部或跨多个组织连接的,可以包含任何企业内部或基于云的流程、服务、应用和数据。这些组合变化可能也符合上述的模式之一,例如 MuleSoft CloudHub 和 BizTalk。
*集成平台即服务iPaaS*一套能够开发、执行和管理集成流的云服务。集成流可以是个人内部或跨多个组织连接的,可以包含任何企业内部或基于云的流程、服务、应用和数据。这些组合变化可能也符合上述的模式之一,例如 MuleSoft CloudHub 和 BizTalk。
**<ruby>移动平台即服务<rt>mPaaS</rt></ruby>**是为开发移动应用提供的<ruby>集成开发环境<rt>IDE</rt></ruby>,并且支持多种移动平台。
*移动平台即服务mPaaS*为开发移动应用提供的集成开发环境IDE,并且支持多种移动平台。
**<ruby>数据库平台即服务<rt>dbPaas</rt></ruby>**是一种按需的、安全且可扩展的自助式数据库平台可自动配置和管理数据库。dbPaaS 使扩展数据库变得更加容易,并使它们更加可靠。
*数据库平台即服务dbPaas*一种按需的、安全且可扩展的自助式数据库平台可自动配置和管理数据库。dbPaaS 使扩展数据库变得更加容易,并使它们更加可靠。
**<ruby>物联网平台即服务<rt>IoTPaaS</rt></ruby>**提供了实现异构物联网拓扑所需的通信、安全、分析和管理的通用基础架构。它为构建物联网解决方案提供了更简单、更敏捷的模型。
*物联网平台即服务IoTPaaS*提供了实现异构物联网拓扑所需的通信、安全、分析和管理的通用基础架构。它为构建物联网解决方案提供了更简单、更敏捷的模型。
**<ruby>业务流程管理平台即服务<rt>bpmPaaS</rt></ruby>**是一个完整的预集成业务流程管理平台,托管在云端并作为服务交付。它被用于开发和执行整个企业的业务流程和以工作流程为中心的应用程序。例如 Pega cloud 和 OpenText Cordys cloud。
*业务流程管理平台即服务bpmPaaS*一个完整的预集成业务流程管理平台,托管在云端并作为服务交付。它被用于开发和执行整个企业的业务流程和以工作流程为中心的应用程序。例如 Pega cloud 和 OpenText Cordys cloud。
PaaS 的一些基本特征:
@ -65,19 +68,19 @@ PaaS 的一些基本特征:
* 应用堆栈的选择
* 语言、数据库和框架支持
* 规模的可扩展性
* <ruby>服务质量<rt>QoS</rt></ruby>
* 服务质量QoS
* 开发和运营的工具
* 它有多适合你的业务
现在让我们快速浏览下流行的开源 PaaS。
**Cloud Foundry**提供了多种云的选择、开发者框架和应用服务。Cloud Foundry 使构建、测试、部署和扩展应用程序变得更快、更容易。
*Cloud Foundry*提供了多种云的选择、开发者框架和应用服务。Cloud Foundry 使构建、测试、部署和扩展应用程序变得更快、更容易。
它有不同的发行版本,其中比较流行的是 Pivotal 和 IBM。它包含应用<ruby>运行时<rt>runtime</rt></ruby>和容器运行时。在 Pivotal 上包含有应用服务和容器服务。
**OpenShift**红帽的云计算 PaaS 产品。这是一个云端的应用平台,应用开发者和团队可以在这里构建、测试、部署和运行他们的应用程序。
*OpenShift*红帽的云计算 PaaS 产品。这是一个云端的应用平台,应用开发者和团队可以在这里构建、测试、部署和运行他们的应用程序。
**Cloudify** 在开放的原则下开发和设计,用以推动 IT 转型革命。它使组织能够在其上设计、建立和提供各种商业应用和网络服务。Cloudify 的最新版本为 4.3,它包含了先进的安全、控制和<ruby>真自服务<rt>true self-service</rt></ruby>等增强功能。Cloudify 4.3 还为 Kubernetes 容器编排引入了全新的概念。
*Cloudify*在开放的原则下开发和设计,用以推动 IT 转型革命。它使组织能够在其上设计、建立和提供各种商业应用和网络服务。Cloudify 的最新版本为 4.3,它包含了先进的安全、控制和<ruby>真自服务<rt>true self-service</rt></ruby>等增强功能。Cloudify 4.3 还为 Kubernetes 容器编排引入了全新的概念。
| 功能 | Cloud Foundry | Cloudify | OpenShift |
| :- | :- | :- | :- |
@ -117,9 +120,11 @@ via: https://www.opensourceforu.com/2022/09/why-enterprises-should-opt-for-platf
作者:[Gopala Krishna Behara][a]
选题:[lkxed][b]
译者:[onionstalgia](https://github.com/onionstalgia)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.opensourceforu.com/author/gopalakrishna-behara/
[b]: https://github.com/lkxed
[0]: https://img.linux.net.cn/data/attachment/album/202302/17/094352atasztuxqg4t1ctt.jpg

View File

@ -0,0 +1,61 @@
[#]: subject: "Opera Browser Plans to Integrate ChatGPT"
[#]: via: "https://debugpointnews.com/opera-chatgpt-integration/"
[#]: author: "arindam https://debugpointnews.com/author/dpicubegmail-com/"
[#]: collector: "lkxed"
[#]: translator: "gpchn"
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-15547-1.html"
Opera 浏览器计划集成 ChatGPT
======
![][1]
> 据报道Opera 浏览器正在寻求将语言模型 ChatGPT 集成到其桌面和移动产品中。
### 摘要
- 据报道,流行的桌面和移动网络浏览器 Opera 浏览器正在探索将语言模型 ChatGPT 集成到其产品中。
- 微软最近宣布将 ChatGPT 的制造商 OpenAI 技术集成到其 Edge 网络浏览器中,为用户添加人工智能功能。
### Opera 与 ChatGPT
[Opera][2],一个被广泛使用的桌面和移动网络浏览器,将把语言模型 ChatGPT 集成到它的软件产品中。Opera 的母公司昆仑科技是这一新闻的幕后推手尽管这次集成的细节仍然有限。Opera 提供了一系列软件产品,包括桌面网络浏览器、移动网络浏览器以及其为 Chromebook 设计的浏览器版本。
最有可能整合 ChatGPT 的是 Opera 的主要桌面浏览器,包括 Opera 浏览器和 Opera GX这是一款为游戏玩家设计的浏览器。然而也有可能集成到 Opera 的移动浏览器中,包括常规的 Opera 移动浏览器、Opera Mini 和 Opera Crypto 浏览器。
### 来自微软和谷歌的最新更新
微软上周宣布将 OpenAI 集成到其 Edge 网络浏览器中,成为头条新闻。这种集成将在未来为 Edge 用户带来两种人工智能功能,即 “<ruby>Chat<rt>聊天</rt></ruby>” 和 “<ruby>撰写<rt>Compose</rt></ruby>”。“聊天”功能可以让用户在浏览器中直接与人工智能交流。同时,“撰写”功能帮助用户进行文本撰写,包括使用不同的语气和长度撰写电子邮件和社交媒体帖子。
在最近的一次演示中,谷歌没有宣布将人工智能聊天机器人直接集成到其 Chrome 浏览器中,这一举措得到的回应并不热烈。
### 下一步是什么
据 Statcounter 统计Opera 浏览器被数亿用户使用,在全球浏览器市场占有 2.4% 的份额。虽然它可能落后于谷歌 Chrome 的 65% 和 Safari 的 18%,但与 Mozilla Firefox 的 3% 相比,它仍然保持着自己的优势。
昆仑科技是一家在深圳证券交易所上市的北京公司。本周,中国搜索巨头百度宣布计划在其搜索产品中集成 ChatGPT 风格的机器人。
总之ChatGPT 与 Opera 浏览器的集成代表着人工智能网络浏览向前迈出的重要一步。随着微软和百度等公司将人工智能技术融入其产品Opera 将自己定位为该领域的领导者。
随着数亿用户和浏览器市场的日益增长ChatGPT 与 Opera 的集成是一项备受期待的发展。
参考:[CNBC][3]
--------------------------------------------------------------------------------
via: https://debugpointnews.com/opera-chatgpt-integration/
作者:[arindam][a]
选题:[lkxed][b]
译者:[gpchn](https://github.com/gpchn)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://debugpointnews.com/author/dpicubegmail-com/
[b]: https://github.com/lkxed
[1]: https://debugpointnews.com/wp-content/uploads/2023/02/opera.jpg
[2]: https://www.opera.com/
[3]: https://www.cnbc.com/2023/02/09/web-browser-opera-is-planning-to-incorporate-chatgpt.html

View File

@ -1,61 +0,0 @@
[#]: subject: "Opera Browser Plans to Integrate ChatGPT"
[#]: via: "https://debugpointnews.com/opera-chatgpt-integration/"
[#]: author: "arindam https://debugpointnews.com/author/dpicubegmail-com/"
[#]: collector: "lkxed"
[#]: translator: "gpchn"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Opera Browser Plans to Integrate ChatGPT
======
![][1]
**Opera browser is reportedly looking to integrate the language model ChatGPT into its desktop and mobile products.**
### In brief
- Opera browser, a popular desktop and mobile web browser is reportedly exploring integrating the language model ChatGPT into its products.
- Microsoft recently announced the integration of OpenAI technology, the maker of ChatGPT, into its Microsoft Edge web browser, adding AI-powered features for users.
### Opera and ChatGPT
[Opera][2], a widely-used desktop and mobile web browser is set to integrate the language model ChatGPT into its software products. Operas parent company Kunlun Tech is behind the news, although the details of the integration remain limited. Opera offers a range of software products, including desktop web browsers, mobile web browsers, and a version of its browser designed for Chromebooks.
The most likely candidates for the ChatGPT integration are Operas main desktop browsers, including Opera browser and Opera GX, a browser designed for gamers. However, there is also potential for integration into Operas mobile browsers, including the regular Opera mobile browser, Opera Mini, and Opera Crypto Browser.
### Recent updates from Microsoft and Google
Microsoft made headlines this week by announcing OpenAI integration into its Microsoft Edge web browser. The integration will bring two AI-powered features, Chat and Compose, to Edge users in the future. Chat allows users to communicate directly with the AI in the browser. At the same time, Compose helps users with text composition, including writing emails and social media posts with different tones and lengths.
During a recent presentation, Google did not announce direct AI chatbot integration into its Chrome browser, which received a less-than-enthusiastic response.
### Whats next
Opera browser is used by hundreds of millions of users, with a 2.4% share of the global browser market, according to Statcounter. While it may trail behind Google Chromes 65% and Safaris 18%, it still holds its own compared to Mozilla Firefoxs 3%.
Kunlun Tech is a Beijing-based company listed on the Shenzhen stock exchange. This week, Chinese search giant Baidu announced plans to integrate a ChatGPT-style bot into its search product.
In conclusion, the integration of ChatGPT into the Opera browser represents a significant step forward in AI-powered web browsing. As companies like Microsoft and Baidu integrate AI technology into their products, Opera is positioning itself as a leader in the field.
With hundreds of millions of users and a growing presence in the browser market, the integration of ChatGPT into Opera is a highly-anticipated development.
Via [CNBC][3]
--------------------------------------------------------------------------------
via: https://debugpointnews.com/opera-chatgpt-integration/
作者:[arindam][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://debugpointnews.com/author/dpicubegmail-com/
[b]: https://github.com/lkxed
[1]: https://debugpointnews.com/wp-content/uploads/2023/02/opera.jpg
[2]: https://www.opera.com/
[3]: https://www.cnbc.com/2023/02/09/web-browser-opera-is-planning-to-incorporate-chatgpt.html

View File

@ -2,7 +2,7 @@
[#]: via: "https://opensource.com/article/22/1/troubleshoot-hardware-sysadmin"
[#]: author: "David Both https://opensource.com/users/dboth"
[#]: collector: "lujun9972"
[#]: translator: " "
[#]: translator: "toknow-gh"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "

View File

@ -1,153 +0,0 @@
[#]: subject: "Learn Tcl by writing a simple game"
[#]: via: "https://opensource.com/article/23/2/learn-tcl-writing-simple-game"
[#]: author: "James Farrell https://opensource.com/users/jamesf"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Learn Tcl by writing a simple game
======
My path to Tcl started with a recent need to automate a difficult Java-based command-line configuration utility. I do a bit of automation programming using Ansible, and I occasionally use the expect module. Frankly, I find this module has limited utility for a number of reasons including: difficulty with sequencing identical prompts, capturing values for use in additional steps, limited flexibility with control logic, and so on. Sometimes you can get away with using the shell module instead. But sometimes you hit that ill-behaving and overly complicated command-line interface that seems impossible to automate.
In my case, I was automating the installation of one of my company's programs. The last configuration step could only be done through the command-line, through several ill-formed, repeating prompts and data output that needed capturing. The good old traditional Expect was the only answer. A deep understanding of Tcl is not necessary to use the basics of Expect, but the more you know, the more power you can get from it. This is a topic for a follow-up article. For now, I explore the basic language constructs of Tcl, which include user input, output, variables, conditional evaluation, looping, and simple functions.
### Install Tcl
On a Linux system, I use this:
```
# dnf install tcl
# which tclsh
/bin/tclsh
```
On macOS, you can use [Homebrew][1]to install the latest Tcl:
```
$ brew install tcl-tk
$ which tclsh
/usr/local/bin/tclsh
```
### Guess the number in Tcl
Start by creating the basic executable script `numgame.tcl`:
```
$ touch numgame.tcl
$ chmod 755 numgame.tcl
```
And then start coding in your file headed up by the usual shebang script header:
```
#!/usr/bin/tclsh
```
Here are a few quick words about artifacts of Tcl to track along with this article.
The first point is that all of Tcl is considered a series of strings. Variables are generally treated as strings but can switch types and internal representations automatically (something you generally have no visibility into). Functions may interpret their string arguments as numbers ( `expr`) and are only passed in by value. Strings are usually delineated using double quotes or curly braces. Double quotes allow for variable expansion and escape sequences, and curly braces impose no expansion at all.
The next point is that Tcl statements can be separated by semicolons but usually are not. Statement lines can be split using the backslash character. However, it's typical to enclose multiline statements within curly braces to avoid needing this. Curly braces are just simpler, and the code formatting below reflects this. Curly braces allow for deferred evaluation of strings. A value is passed to a function before Tcl does variable substitution.
Finally, Tcl uses square brackets for command substitution. Anything between the square brackets is sent to a new recursive invocation of the Tcl interpreter for evaluation. This is handy for calling functions in the middle of expressions or for generating parameters for functions.
### Procedures
Although not necessary for this game, I start with an example of defining a function in Tcl that you can use later:
```
proc used_time {start} {
return [expr [clock seconds] - $start]
}
```
Using `proc` sets this up to be a function (or procedure) definition. Next comes the name of the function. This is then followed by a list containing the parameters; in this case 1 parameter `{start}` and then followed by the function body. Note that the body curly brace starts on this line, it cannot be on the following line. The function returns a value. The returned value is a compound evaluation (square braces) that starts by reading the system clock `[clock seconds]` and does the math to subtract out the `$start` parameter.
### Setup, logic, and finish
You can add more details to the rest of this game with some initial setup, iterating over the player's guesses, and then printing results when completed:
```
set num [expr round(rand()*100)]
set starttime [clock seconds]
set guess -1
set count 0
puts "Guess a number between 1 and 100"
while { $guess != $num } {
incr count
puts -nonewline "==> "
flush stdout
gets stdin guess
if { $guess < $num } {
puts "Too small, try again"
} elseif { $guess > $num } {
puts "Too large, try again"
} else {
puts "That's right!"
}
}
set used [used_time $starttime]
puts "You guessed value $num after $count tries and $used elapsed seconds"
```
The first `set` statements establish variables. The first two evaluate expressions to discern a random number between 1 and 100, and the next one saves the system clock start time.
The `puts` and `gets` command are used for output to and input from the player. The `puts` I've used imply standard out for output. The `gets` needs the input channel to be defined, so this code specifies `stdin` as the source for terminal input from the user.
The `flush stdout` command is needed when `puts` omits the end-of-line termination because Tcl buffers output and it might not get displayed before the next I/O is needed.
From there the `while` statement illustrates the looping control structure and conditional logic needed to give the player feedback and eventually end the loop.
The final `set` command calls our function to calculate elapsed seconds for gameplay, followed by the collected stats to end the game.
### Play it!
```
$ ./numgame.tcl
Guess a number between 1 and 100
==> 100
Too large, try again
==> 50
Too large, try again
==> 25
Too large, try again
==> 12
Too large, try again
==> 6
Too large, try again
==> 3
That's right!
You guessed value 3 after 6 tries and 20 elapsed seconds
```
### Continue learning
When I started this exercise, I doubted just how useful going back to a late 1990s fad language would be to me. Along the way, I found a few things about Tcl that I really enjoyed — my favorite being the square bracket command evaluation. It just seems so much easier to read and use than many other languages that overuse complicated closure structures. What I thought was a [dead language][2] was actually still thriving and supported on several platforms. I learned a few new skills and grew an appreciation for this venerable language.
Check out the official site over at [https://www.tcl-lang.org][3]. You can find references to the latest source, binary distributions, forums, docs, and information on conferences that are still ongoing.
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/2/learn-tcl-writing-simple-game
作者:[James Farrell][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/jamesf
[b]: https://github.com/lkxed/
[1]: https://opensource.com/article/20/6/homebrew-mac
[2]: https://opensource.com/article/19/6/favorite-dead-language
[3]: https://www.tcl-lang.org

View File

@ -2,7 +2,7 @@
[#]: via: "https://opensource.com/article/23/2/lua-loops-while-repeat-until"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "

View File

@ -0,0 +1,153 @@
[#]: subject: "Learn Tcl by writing a simple game"
[#]: via: "https://opensource.com/article/23/2/learn-tcl-writing-simple-game"
[#]: author: "James Farrell https://opensource.com/users/jamesf"
[#]: collector: "lkxed"
[#]: translator: "geekpi"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
通过编写一个简单的游戏来学习 Tcl
======
我的 Tcl 之路始于最近需要将一个困难的基于 Java 的命令行配置工具自动化。我使用 Ansible 做了一些自动化编程,偶尔也会使用 expect 模块。坦率地说,我发现这个模块的作用有限,原因包括:难以对相同的提示进行排序,难以捕捉到额外步骤的值,控制逻辑的灵活性有限,等等。有时你可以用 shell 模块来代替。但有时你会遇到那种行为异常、过于复杂的命令行界面,似乎无法实现自动化。
就我而言,我正在自动安装我公司的一个程序。最后的配置步骤只能通过命令行来完成,通过几个不规范的、重复的提示和需要捕捉的数据输出。好在传统的 Expect 是唯一的答案。要使用 Expect 的基本功能,并不需要对 Tcl 有很深的了解,但你了解的越多,你就能从它那里得到更多的力量。这是后续文章的话题。现在,我探讨一下 Tcl 的基本语言结构,包括用户输入、输出、变量、条件判断、循环和简单函数。
### 安装 Tcl
在 Linux 系统上,我使用这个:
```
# dnf install tcl
# which tclsh
/bin/tclsh
```
在 macOS 上,你可以使用 [Homebrew][1] 来安装最新的 Tcl
```
$ brew install tcl-tk
$ which tclsh
/usr/local/bin/tclsh
```
### 在 Tcl 中猜数字
从创建基本的可执行脚本 `numgame.tcl` 开始:
```
$ touch numgame.tcl
$ chmod 755 numgame.tcl
```
接着在你的文件中开始编码,标题是通常的 #!
```
#!/usr/bin/tclsh
```
这里有一些关于 Tcl 的简单介绍,以便与本文一起追踪。
第一点是,所有的 Tcl 都被认为是一系列的字符串。变量通常被当作字符串处理,但可以自动切换类型和内部表示(这一点你通常无法看到)。函数可以把它们的字符串参数解释为数字(`expr`),并且只通过值传递。字符串通常使用双引号或大括号来划分。双引号允许变量扩展和转义序列,而大括号则完全没有扩展。
第二点是 Tcl 语句可以用分号隔开,但通常不是这样。语句行可以用反斜杠字符来分割。然而,典型的做法是将多行语句放在大括号内,以避免需要这样做。大括号只是更简单,下面的代码格式也反映了这一点。大括号允许对字符串进行延迟求值。在 Tcl 进行变量替换之前,一个值被传递给一个函数。
最后Tcl 使用方括号进行命令替换。方括号之间的任何东西都会被送到 Tcl 解释器的一个新的递归调用中进行求值。这对于在表达式中间调用函数或为函数生成参数是很方便的。
### 过程
虽然在这个游戏中没有必要,但我先举一个在 Tcl 中定义函数的例子,你可以在以后使用:
```
proc used_time {start} {
return [expr [clock seconds] - $start]
}
```
使用 `proc` 将其设定为一个函数(或过程)定义。接下来是函数的名称。然后是一个包含参数的列表;在本例中是一个参数 `{start}` ,然后是函数主体。注意,主体的大括号在这一行开始,它不能在下面一行。该函数返回一个值。返回值是一个复合求值(方括号),它从读取系统时钟 `[clock seconds]` 开始,并进行数学运算以减去 `$start` 参数。
### 设置、逻辑和完成
你可以在这个游戏的其余部分增加更多的细节,进行一些初始设置,对玩家的猜测进行迭代,然后在完成后打印结果:
```
set num [expr round(rand()*100)]
set starttime [clock seconds]
set guess -1
set count 0
puts "Guess a number between 1 and 100"
while { $guess != $num } {
incr count
puts -nonewline "==> "
flush stdout
gets stdin guess
if { $guess < $num } {
puts "Too small, try again"
} elseif { $guess > $num } {
puts "Too large, try again"
} else {
puts "That's right!"
}
}
set used [used_time $starttime]
puts "You guessed value $num after $count tries and $used elapsed seconds"
```
前面的 `set` 语句建立变量。前两个求值表达式用于识别 1 到 100 之间的随机数,下一个保存系统时钟启动时间。
`puts``gets` 命令用于来自玩家的输出和输入。我使用的 `puts` 暗示输出是标准输出。`gets` 需要定义输入通道,所以这段代码指定 `stdin` 作为用户的终端输入源。
`puts` 省略行末终止符时,需要 `flush stdout` 命令,因为 Tcl 缓冲了输出,在需要下一个 I/O 之前可能不会被显示。
从这里开始,`while` 语句说明了循环控制结构和条件逻辑,需要给玩家反馈并最终结束循环。
最后的 `set` 命令调用我们的函数来计算游戏的耗时秒数,接着是收集到的统计数字来结束游戏。
### 玩吧!
```
$ ./numgame.tcl
Guess a number between 1 and 100
==> 100
Too large, try again
==> 50
Too large, try again
==> 25
Too large, try again
==> 12
Too large, try again
==> 6
Too large, try again
==> 3
That's right!
You guessed value 3 after 6 tries and 20 elapsed seconds
```
### 继续学习
当我开始这个练习时,我怀疑回到 90 年代末的流行语言对我有多大的帮助。一路走来,我发现 Tcl 有几处让我非常喜欢的地方,我最喜欢的是方括号内的命令求值。与其他许多过度使用复杂闭包结构的语言相比,它似乎更容易阅读和使用。我以为它是一种[死语言][2],但实际上它仍在蓬勃发展,并在多个平台上得到支持。我学到了一些新的技能,并对这种古老的语言有了新的认识。
在 [https://www.tcl-lang.org][3] 上查看官方网站。你可以找到最新的源代码、二进制发行版、论坛、文档,以及仍在进行的会议信息的参考。
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/2/learn-tcl-writing-simple-game
作者:[James Farrell][a]
选题:[lkxed][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/jamesf
[b]: https://github.com/lkxed/
[1]: https://opensource.com/article/20/6/homebrew-mac
[2]: https://opensource.com/article/19/6/favorite-dead-language
[3]: https://www.tcl-lang.org