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.
I asked it to explain how let is different from my and here's its response:
In Perl, the my keyword is used to declare a variable with a limited scope within a block of code. This means that the variable is only visible within that block, and any nested blocks. Once the block is exited, the variable goes out of scope and cannot be accessed.
The proposed let keyword is similar to my, but there is an important difference: let allows you to create a new variable with the same name as an existing variable, without overwriting the existing variable. The new variable only exists within the scope of the block it is defined in, and does not affect the value of the existing variable outside of that block.
Here's an example to illustrate the difference:
sub foo {
my $x = 10;
{
let $x = 20; # create new variable with limited scope
say $x; # output: 20
}
say $x; # output: 10
}
In this example, we define a function foo that declares a variable $x with a value of 10. We then define a new block of code using curly braces, and use the let keyword to create a new variable $x with a value of 20 within that block. When we output the value of $x inside the block, we get 20, as expected. However, when we output the value of $x outside of the block, we get 10 - this is because the original variable has not been modified by the let statement.
So, in summary, the let keyword is similar to my, but allows you to create a new variable with the same name as an existing variable within a block, without overwriting the original variable. This can be useful in situations where you need to temporarily change the value of a variable within a limited scope.
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 forlet
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.I think of this as an ideas generator, and to get an outsider viewpoint on Perl. One of the issues I think we have is that we as a community tend to look inward a lot. Of the suggestions I'd agree that yield is interesting although there's prior art on CPAN and it's not clear to me it's generically useful enough for core. the async stuff I personally believe we need to have and I think 'data class' could be interesting if we could also hang meta descriptions off the fields