Es gibt viele Gründe, warum du Code von einer Programmiersprache in eine andere umwandeln möchtest. Jede Programmiersprache hat ihre eigenen Stärken und Schwächen. Du kannst die Features nutzen, die in einer anderen Sprache verfügbar sind. Du kannst zum Beispiel Code in eine andere Programmiersprache umwandeln, die eine bessere Leistung bietet oder mit starker Typisierung dabei hilft, Bugs vorzubeugen.
Um den Code einfacher zu verwalten, solltest du ihn in eine Programmiersprache umwandeln, die in deiner Organisation häufiger verwendet wird. Wenn beispielsweise nur wenige Personen in deiner Organisation mit einer älteren Programmiersprache wie Perl vertraut sind, kannst du noch genutzten Perl-Code in eine häufiger verwendete Programmiersprache wie Python oder JavaScript umwandeln.
Copilot kann dir dabei helfen, Code von einer Programmiersprache in eine andere umzuwandeln. Das Übersetzen einer eigenständigen Datei wie z. B. eines Skripts ist leicht. Dieser Vorgang wird in diesem Artikel beschrieben.
Das Übersetzen eines Projekts mit mehreren Dateien ist ein komplexerer Prozess und wird in AUTOTITLE- beschrieben.
Beispielszenario
Mit dem folgenden Perl-Skript wird die benutzende Person aufgefordert, den Pfad zu einer Textdatei einzugeben. Es wird überprüft, was die Person eingibt und ob eine Textdatei in diesem Pfad gefunden wird. Eine Wortanzahl und Zeichenanzahl werden für den Inhalt der Datei ausgegeben.
#!/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";
Beispiel für Prompt
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.
Beispielantwort
Copilot generiert den folgenden TypeScript-Code.
Note
Da die Copilot Chat-Antworten nicht deterministisch sind, erhältst du möglicherweise eine andere Antwort als die hier beschriebene.
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.`);
});
Speichern Sie diese Datei als wordcount.ts
.
Copilot erklärt dann, wie die neue Datei mit Node ausgeführt wird.
-
Installieren von Node.js: Lade Node.js von der offiziellen Website herunter, und installiere sie, wenn dies noch nicht geschehen ist: https://nodejs.org/.
-
Installieren von TypeScript:
npm install -g typescript
-
Kompilieren des TypeScript-Codes:
tsc wordcount.ts
Dadurch wird eine
wordcount.js
-Datei im selben Verzeichnis erstellt. -
Ausführen der JavaScript-Datei mit Node.js:
node wordcount.js