ChatGPT suggests new keywords for Perl

I asked ChatGPT to suggest some new keywords that might make Perl more desirable. Here's the dialog:

JOHN: suggest some new keywords that Perl could add to its core syntax that would make the language more desirable to modern programmers

CHATGPT: Perl is a mature language that has undergone significant development since its inception. Despite this, there are always new features that can be added to a language to improve its appeal to modern programmers. Here are some suggestions for new keywords that Perl could add to its core syntax:

async and await: Asynchronous programming has become an increasingly important aspect of modern programming. Adding support for async and await keywords in Perl would enable developers to write non-blocking code that can take full advantage of modern hardware.

let: The let keyword is commonly used in languages like JavaScript and Rust to create variables with a limited scope. Adding let to Perl would make it easier to write clean and concise code.

match or switch: The match or switch keyword would provide a more concise and expressive way to perform pattern matching than the traditional if and else statements. This would make it easier to write code that is more readable and easier to maintain.

yield: The yield keyword is commonly used in languages like Python and JavaScript to create generator functions. Adding yield to Perl would allow developers to create functions that can return a sequence of values over time, rather than all at once.

dataclass: The dataclass keyword is used in Python to create classes that are primarily used to store data. Adding this feature to Perl would provide a more concise and expressive way to define data structures and would make it easier to work with complex data types.

These are just a few examples of new keywords that could be added to Perl to make the language more desirable to modern programmers. Of course, any new feature should be carefully considered to ensure it fits well with the language's existing syntax and design philosophy.

JOHN: improve the previous response by adding code examples for each keyword

CHATGPT: Sure, I'd be happy to provide code examples for each of the suggested keywords.

async and await:

use feature 'async'; # use async keyword

async sub fetch_data {
  my $response = await do_http_request($url); # use await keyword to wait for HTTP request
  # process response
}

my $result = await fetch_data(); # use await keyword to wait for result

let:

sub foo {
  my $x = 10;
  {
    let $x = 20; # create new variable with limited scope
    say $x; # output: 20
  }
  say $x; # output: 10
}

match or switch:

given ($value) {
  when (/^\d+$/) { say "Integer"; }
  when (/^\w+$/) { say "Word"; }
  when (/^\s+$/) { say "Whitespace"; }
  default { say "Other"; }
}

yield:

sub generate_numbers {
  my $start = shift;
  while (1) {
    yield $start++;
  }
}

my $generator = generate_numbers(1);
say $generator->(); # output: 1
say $generator->(); # output: 2
say $generator->(); # output: 3

dataclass:

use feature 'dataclass'; # use dataclass keyword

dataclass Point {
  has $x;
  has $y;
}

my $p = Point->new(x => 1, y => 2);
say $p->x; # output: 1
say $p->y; # output: 2

Note that some of these examples use the feature pragma to enable experimental features, as these are not yet part of the Perl core syntax. However, this should give you a good idea of how these keywords might work in practice.

5 Comments

It's quite amazing that it can make such suggestions. Think you could soon get it to make a pull request on git development branch to actually incorporate one of it's suggestions into perl?

Of course that example already works exactly as described if you use my. There is no need for let in Perl.

All ChatGPT can do is generate a plausible response to a prompt. Here's a question. Were any of these suggestions a surprise? (I mean... other than let. Ho-hum.)

Verrrrrry interesting.

I understand yield to call for something like an iterator. You can do this in Perl now, but I would be curious to know details.

The most thought-provoking (to me at least) was dataclass, which I understand to be a restricted hash on steroids.

Leave a comment

About john napiorkowski

user-pic I blog about Perl.