Skip to main content

์ฝ”๋“œ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ๋ฒˆ์—ญ

Copilot Chat๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•˜์—ฌ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ์ฝ”๋“œ๋ฅผ ์ด๋™ํ•˜๋ ค๋Š” ๋ฐ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋Š” ๊ณ ์œ ํ•œ ๊ฐ•์ ๊ณผ ์•ฝ์ ์ด ์žˆ์œผ๋ฉฐ ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ ํ™œ์šฉ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋” ํ–ฅ์ƒ๋œ ์„ฑ๋Šฅ์˜ ์–ธ์–ด๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ๋ฒ„๊ทธ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ณด๋‹ค ๊ฐ•๋ ฅํ•œ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜๋Š” ์–ธ์–ด๋กœ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊พธ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œ๋ฅผ ์‰ฝ๊ฒŒ ์œ ์ง€ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์กฐ์ง์—์„œ ๋” ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด๋กœ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์กฐ์ง์—์„œ Perl๊ณผ ๊ฐ™์€ ์ด์ „ ์–ธ์–ด๋ฅผ ์•„๋Š” ์‚ฌ๋žŒ์ด ๊ฑฐ์˜ ์—†๋Š” ๊ฒฝ์šฐ ๊ธฐ์กด์— ์‚ฌ์šฉ ์ค‘์ธ Perl ์ฝ”๋“œ๋ฅผ Python ๋˜๋Š” JavaScript์™€ ๊ฐ™์€ ๋” ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์–ธ์–ด๋กœ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ด ๋” ๋‚˜์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Copilot์„ ํ†ตํ•ด ํ•œ ์–ธ์–ด์—์„œ ๋‹ค๋ฅธ ์–ธ์–ด๋กœ ๋ณด๋‹ค ์‰ฝ๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ์™€ ๊ฐ™์€ ๋…๋ฆฝ ์‹คํ–‰ํ˜• ํŒŒ์ผ์„ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๋Š” ์ด ๋ฌธ์„œ์— ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ ํŒŒ์ผ์ด ํฌํ•จ๋œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ๋” ๋ณต์žกํ•œ ํ”„๋กœ์„ธ์Šค์ด๋ฉฐ Copilot์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ๋ฅผ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์— ์ž์„ธํžˆ ์„ค๋ช…๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์‹œ๋‚˜๋ฆฌ์˜ค

๋‹ค์Œ Perl ์Šคํฌ๋ฆฝํŠธ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•˜๋Š” ๋‚ด์šฉ์„ ํ™•์ธํ•˜๊ณ  ํ•ด๋‹น ๊ฒฝ๋กœ์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์ด ๋ฐœ๊ฒฌ๋˜๋ฉด ํŒŒ์ผ ๋‚ด์šฉ์˜ ๋‹จ์–ด ๊ฐœ์ˆ˜์™€ ๋ฌธ์ž ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

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";

์˜ˆ์‹œ ํ”„๋กฌํ”„ํŠธ

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 ์‘๋‹ต์€ ๋น„๊ฒฐ์ •์ ์ด๋ฏ€๋กœ ์—ฌ๊ธฐ์— ํ‘œ์‹œ๋œ ์‘๋‹ต๊ณผ ๋‹ค๋ฅธ ์‘๋‹ต์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

TypeScript
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๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  1. Node.js ์„ค์น˜: ์•„์ง ์„ค์น˜ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ณต์‹ ์›น ์‚ฌ์ดํŠธ(https://nodejs.org/)์—์„œ Node.js๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

  2. Install TypeScript:

    npm install -g typescript
    
  3. TypeScript ์ฝ”๋“œ ์ปดํŒŒ์ผ:

    tsc wordcount.ts
    

    ๊ทธ๋Ÿฌ๋ฉด ๋™์ผํ•œ ๋””๋ ‰ํ„ฐ๋ฆฌ์— wordcount.js ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

  4. Node.js๋กœ JavaScript ํŒŒ์ผ ์‹คํ–‰:

    node wordcount.js
    

์ถ”๊ฐ€ ์ฐธ๊ณ  ์ž๋ฃŒ