ChatGPT for Perl Learning

How to learn Perl has been an eternal problem for the Perl community. Compared to many other languages that place an emphasis on teaching the basics and using education as a tool for evangelism we've had sporadic efforts along those lines. Given the size of our community and the fact that the best programmers are often in great demand, we have a hard time pumping out the needed docs and examples.

I've been using ChatGPT quite a lot lately and find that although it makes some mistakes it actually churns out pretty decent Perl if given good instructions. For example here's one I did this AM. We just had the dreaded hour shift here in the US and I'm just not up to thinking so I asked Chat GPT:

"write a perl subroutine that accepts two hashrefs and a list. For each item in the item merge the hashrefs by combining the values into an array ref. "

Here's what it pumped out:

sub merge_hashes {
    my ($hashref1, $hashref2, @list) = @_;

    foreach my $item (@list) {
        my @values = ();

        if (exists $hashref1->{$item}) {
            push @values, @{$hashref1->{$item}};
        }

        if (exists $hashref2->{$item}) {
            push @values, @{$hashref2->{$item}};
        }

        $hashref1->{$item} = \@values;
    }
}

Along with the following explanation:

"In this subroutine, we first receive two hashrefs and a list as input. We then iterate over each item in the list and create an empty array to hold the merged values.

We then check if each hashref has a value for the current item. If so, we push all the values for that item into the @values array. We then assign the merged @values array as the value for the current item in the first hashref.

Finally, we return the updated first hashref after all items have been processed.

Note: The original hashref1 is modified in place by this subroutine."

Not too bad. I than asked it to modify the code thus:

"change the subroutine so that the item value can be either a scalar or an array ref. we should normalize all values to arrayref"

And it came up with this:

sub merge_hashes {
    my ($hashref1, $hashref2, @list) = @_;

    foreach my $item (@list) {
        my @values = ();

        # Check if the item value is an array ref or scalar
        if (ref $hashref1->{$item} eq 'ARRAY') {
            push @values, @{$hashref1->{$item}};
        } elsif (defined $hashref1->{$item}) {
            push @values, $hashref1->{$item};
        }

        if (ref $hashref2->{$item} eq 'ARRAY') {
            push @values, @{$hashref2->{$item}};
        } elsif (defined $hashref2->{$item}) {
            push @values, $hashref2->{$item};
        }

        # Normalize the value to an array ref
        $hashref1->{$item} = scalar @values > 1 ? \@values : $values[0] || [];
    }
}

Lastly I asked:

"fix this so that the $item value is undefined we don't get an error"

And it produced:

Although its far from perfect this is a space we should be watching and toying with since it seems to me to be a possible way to help bootstrap programmers that need to or want to get into Perl but are not finding the existing learning and mentoring capacity sufficient.

8 Comments

I don't think we've had a hard time pumping out docs and examples. The bibliography of Perl books, including The Perl Cookbook, is long.

If someone wants to learn Perl, everything they need is in some book.

And yet, that's not how younger generations consume information. They are much more interactive than those of us who you might consider to be "Perl veterans". Think videos and short blogs vs. books and long blocks of text.

I'll also extend John's last comment to say "Because it's far from perfect, this is a space we should be watching carefully, before it becomes the next Matt's Script Archive."

And no, I am not *that* Matt. :-)

Well, if normalize all values to arrayref means what I think it does (i.e. $hashref1->{$item} = \@values;), that line in ChatGPT's code is buggy, and goes out of its way to be so.

But given the notorious difficulty of transcoding English, not a bad effort.

Reading might not be how younger generations consume info, but that's not the problem here. It has nothing to do with age or place in time.

People have always looked for shortcuts and for ways to avoid learning. New technology has not changed that. ChatGPT, like StackOverflow, is just another way to avoid building useful skills, and asking something to write code for you is a way to avoid learning.

If you only use sources that exactly fit your specification, what good are you when there's something you can't find sources for? This failure to actually learn so that you can synthesize info makes these people unfit for the task.

This is the problem with video. If you need video for every exact problem you have, then you should be ready to shell out the appropriate amount of money for the creator to make it. It's incredibly time intensive and wasteful. A lot less wasteful is a blog post on every particular task, but who are you going to get to write exactly what one person needs? That's still very expensive.

At some point, people need the skills to figure things that no one has done yet. There are plenty of Perl people providing that content: me, Gabor, Mohammad, and so on. It's not sporadic, but the Perl community ignores it for whatever reason.

Here's what bing chat (itself a cousin of ChatGPT) had to say about your opening paragraph, and I agree:

Using ChatGPT to generate Perl code can be a useful strategy for some people. It can help generate decent Perl code if given good instructions. However, it's important to note that it may make some mistakes and the generated code should be reviewed for accuracy and correctness. Ultimately, whether this is a good strategy for learning Perl depends on the individual's learning style and goals.

If you don't already know what you are doing, ChatGPT can be very misleading. It's designed to be the world's greatest bullshitter after all.

> ChatGPT, can you give me a regular expression that matches a repeating pattern of two characters that are different from each other?

> Certainly! The regular expression that matches a repeating pattern of two characters that are different from each other is:

(\w)\1

Explanation:
(\w) matches any word character and captures it as the first group.
\1 matches the exact same character as the one captured in the first group.

Therefore, this regular expression will match any repeating pattern of two characters that are different from each other. For example, it will match "abab", "1212", "x1x1", but not "aaa", "111", "xx".

It did get something closer to right when I told it how it was wrong, but you get the idea.

Perl is like the sharp guy in the back office who can solve any problem fast and efficiently. Doesn't care about the "fashion du jour" - just what works and is efficient. And then are the "me too"s, who spent their time sensing the daily winds and making sure they are sexier than the next one. And that's how they win the hearts and minds of the most young and superficial minds. But every now and then there is a "fashion" that is the real thing and somehow perl made a few bad bets.
Like jump on the lame OOP bandwagon but ignore the AI. Been doing perl since 1994. when OOP hit like a freight train - sure I looked into it and decided against it. Looked at LISP and was fascinated, but wasn't practical. So, I educated myself on FP using perl and thank goodness perl was flexible enough to allow for it. IMHO python is a sexy (not for me though) veneer (mostly hollow) that works ;one a glue for really good and extensive libraries. They just figured out that "sexy" is more profitable than "good". While this train left long time ago and I am not sure perl can do any catch-up - I still wish it got serious about the AI so dinosaurs like me can dip their finger into AI without gagging while trying to use python syntax ;)

Leave a comment

About john napiorkowski

user-pic I blog about Perl.