您可能想将代码从一种编程语言迁移到另一种语言的原因有很多。每种编程语言都有其自身的优势和劣势,您可能希望利用另一种语言提供的特性。例如,您可能想将代码迁移到性能更好,或使用强类型以帮助防止错误的语言。
为了便于维护代码,您可能希望将代码迁移到在组织内更为广泛使用的语言。例如,如果组织中很少有人熟悉像 Perl 这样的老旧语言,您可能希望将仍在使用的 Perl 代码迁移到更常用的语言,如 Python 或 JavaScript。
Copilot 可以帮助您将代码从一种语言翻译成另一种语言。翻译独立文件(例如脚本)相对简单。此过程已在本文中描述。
翻译包含多个文件的项目是一个更复杂的过程,相关内容请参阅 使用 GitHub Copilot 将项目迁移到另一种编程语言。
示例情景
下面的 Perl 脚本会提示用户输入文本文件的路径。它会检查用户的输入,如果在该路径找到文本文件,则输出文件内容的单词数和字符数。
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;
print "Please enter the path to a local text file: ";
chomp(my $file_path = <STDIN>);
if ($file_path =~ /[`|;&<>(){}\[\]]/ || !File::Spec->file_name_is_absolute($file_path) && dirname($file_path) ne '.') {
die "Invalid file path. Please provide a valid path.\n";
}
if (!-e $file_path) {
die "File not found. Please provide an existing file path.\n";
}
open(my $fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $is_binary = 0;
while (read($fh, my $buffer, 1024)) {
if ($buffer =~ /[^\x09\x0A\x0D\x20-\x7E]/) {
$is_binary = 1;
last;
}
}
close($fh);
if ($is_binary) {
die "The file '$file_path' is a binary file. Please provide a text file.\n";
}
open($fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $content = do { local $/; <$fh> };
close($fh);
my $word_count = scalar(split /\s+/, $content);
my $char_count = length($content);
print "The file '$file_path' contains $word_count words and $char_count characters.\n";
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;
print "Please enter the path to a local text file: ";
chomp(my $file_path = <STDIN>);
if ($file_path =~ /[`|;&<>(){}\[\]]/ || !File::Spec->file_name_is_absolute($file_path) && dirname($file_path) ne '.') {
die "Invalid file path. Please provide a valid path.\n";
}
if (!-e $file_path) {
die "File not found. Please provide an existing file path.\n";
}
open(my $fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $is_binary = 0;
while (read($fh, my $buffer, 1024)) {
if ($buffer =~ /[^\x09\x0A\x0D\x20-\x7E]/) {
$is_binary = 1;
last;
}
}
close($fh);
if ($is_binary) {
die "The file '$file_path' is a binary file. Please provide a text file.\n";
}
open($fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $content = do { local $/; <$fh> };
close($fh);
my $word_count = scalar(split /\s+/, $content);
my $char_count = length($content);
print "The file '$file_path' contains $word_count words and $char_count characters.\n";
示例提示
Tell me how I can convert this Perl script to Typescript. The functionality and output of the new script should be exactly the same as the existing one. Also tell me how to run the new file using Node.
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Basename;
print "Please enter the path to a local text file: ";
chomp(my $file_path = <STDIN>);
if ($file_path =~ /[`|;&<>(){}\[\]]/ || !File::Spec->file_name_is_absolute($file_path) && dirname($file_path) ne '.') {
die "Invalid file path. Please provide a valid path.\n";
}
if (!-e $file_path) {
die "File not found. Please provide an existing file path.\n";
}
open(my $fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $is_binary = 0;
while (read($fh, my $buffer, 1024)) {
if ($buffer =~ /[^\x09\x0A\x0D\x20-\x7E]/) {
$is_binary = 1;
last;
}
}
close($fh);
if ($is_binary) {
die "The file '$file_path' is a binary file. Please provide a text file.\n";
}
open($fh, '<', $file_path) or die "Could not open file '$file_path': $!\n";
my $content = do { local $/; <$fh> };
close($fh);
my $word_count = scalar(split /\s+/, $content);
my $char_count = length($content);
print "The file '$file_path' contains $word_count words and $char_count characters.\n";
Tell me how I can convert this Perl script to Typescript. The functionality and output of the new script should be exactly the same as the existing one. Also tell me how to run the new file using Node.
Tell me how I can convert this Perl script to Typescript. The functionality and output of the new script should be exactly the same as the existing one. Also tell me how to run the new file using Node.
示例回复
Copilot 生成以下 TypeScript 代码。
注意
Copilot Chat 的响应是非确定性的,因此你可能会得到与此处显示的不同的响应。
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Please enter the path to a local text file: ', (filePath) => {
rl.close();
if (/[`|;&<>(){}\[\]]/.test(filePath) || (!path.isAbsolute(filePath) && path.dirname(filePath) !== '.')) {
console.error('Invalid file path. Please provide a valid path.');
process.exit(1);
}
if (!fs.existsSync(filePath)) {
console.error('File not found. Please provide an existing file path.');
process.exit(1);
}
const fileBuffer = fs.readFileSync(filePath);
const isBinary = fileBuffer.some(byte => byte < 9 || (byte > 13 && byte < 32) || byte > 126);
if (isBinary) {
console.error(`The file '${filePath}' is a binary file. Please provide a text file.`);
process.exit(1);
}
const content = fileBuffer.toString('utf-8');
const wordCount = content.split(/\s+/).length;
const charCount = content.length;
console.log(`The file '${filePath}' contains ${wordCount} words and ${charCount} characters.`);
});
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Please enter the path to a local text file: ', (filePath) => {
rl.close();
if (/[`|;&<>(){}\[\]]/.test(filePath) || (!path.isAbsolute(filePath) && path.dirname(filePath) !== '.')) {
console.error('Invalid file path. Please provide a valid path.');
process.exit(1);
}
if (!fs.existsSync(filePath)) {
console.error('File not found. Please provide an existing file path.');
process.exit(1);
}
const fileBuffer = fs.readFileSync(filePath);
const isBinary = fileBuffer.some(byte => byte < 9 || (byte > 13 && byte < 32) || byte > 126);
if (isBinary) {
console.error(`The file '${filePath}' is a binary file. Please provide a text file.`);
process.exit(1);
}
const content = fileBuffer.toString('utf-8');
const wordCount = content.split(/\s+/).length;
const charCount = content.length;
console.log(`The file '${filePath}' contains ${wordCount} words and ${charCount} characters.`);
});
将此文件保存为 wordcount.ts。
Copilot 接下来解释如何使用 Node 运行新文件。
-
安装 Node.js:如果您尚未安装,请从官方网站下载并安装 Node.js:https://node.org.cn/。
-
安装 TypeScript
npm install -g typescript -
编译 TypeScript 代码
tsc wordcount.ts这将在同一目录下创建一个
wordcount.js文件。 -
使用 Node.js 运行 JavaScript 文件
node wordcount.js