Forums

Perl

Sort:
skelos

@stephen_33 has started a topic for Python, intending to provide information about installation, troubleshooting, development, debugging and whatever else comes up.

It may be that Python is more popular than perl these days; I don't know. I've been paid to write both but am definitely more experienced with perl since I was using it from version 2.x before Python existed.

I am not going to try to duplicate Stephen's work here: for one, I am not expert enough with Windows to be able to troubleshoot all problems there, and for a second thing I prefer a text editor to write code in and a shell (Unix/Linux/OS X/MacOS) to run code in or console window (Windows) rather than an IDE (Integrated Development Environment).

I know IDEs are the norm on Windows, but I've done most of my programming professionally for projects that targeted multiple operating systems, and IDEs are typically not portable.

skelos

Windows general concepts:

  • Strawberry Perl from strawberryperl.com (it has useful additions over the more minimalist versions available via perl.org)
  • If the installation doesn't do it for you (and it didn't for me) set up a .pl file association and add .pl to the list of extensions which should be run (.bat, .exe, etc)
  • Again if the installation did not do so, you might want to add directories containing perl.exe and helper programs to PATH.
skelos

OS X general comments:

  • Ships with perl
  • You may prefer a later binary package (see perl.org)
  • You might or might not need to fiddle with PATH depending how you configure it (or don't).
skelos

Linux general concepts:

  • Most, perhaps all, distributions ship with perl
  • Generally you will want to use the distribution's package installer (apt for Debian derived distributions, yum for Red Hat derived distributions)
  • If you install via a package installer you should not need to touch PATH.
skelos

Perl version notes:

  • There is a perl 6 project "out there" but it's "out there". Perl 5 is mainstream
  • 5.26.1 is latest but anything after about 5.14 should be fine
  • Yes, this is unlike the 2.7/3.x dichotomy for Python, but perl might face that with Perl 6. We'll see. Perl 4.x to 5.x migration took some time even though there was little in perl 4 deprecated in Perl 5, but that was all last century so nobody cares. happy.png
skelos

Example code by yours truly is available from GitHub, and after today I need to update to say that the code definitely works on Windows 10 with Strawberry perl. (Probably Windows 7 too, and I will test if anyone asks.)

When updating the documentation I will review the sample code; I might be able to make some of it cleaner now. Or not!

 

https://github.com/cc-skelos/chess-com-perl

skelos

[reserved]

skelos

[reserved]

skelos

[reserved]

skelos

[reserved]

stephen_33

I completed the codecademy course on Ruby but I know nothing about Perl. What are the main differences between Perl & Python/Ruby?

BTW - when you work in a text editor such as Notebook++, does it automatically indent code blocks? One feature of the Python IDLE is that it does this for any iterative/looping, or conditional statement.

skelos

I know almost nothing about Ruby so can't make any sensible comments.

Perl is older than Python, and was designed for Unix tools and text wrangling from the beginning. Later it got some object oriented features (best kept to modules, generally) and its "exceptions" are for truly exceptional events (as in Go) and not something used regularly.

Python from the beginning had more of the OO idea in mind, and includes exceptions; you're not using the language in idiomatic form unless you use them, and you have to a lot anyway for core functionality and extension modules both.

Each language provides robust module interfaces and scope for variables. Both automate memory management and perl has always done so with reference counting; with the different implementations of Python around I'm not sure if they're all the same in that respect.

Perl started on Unix and became cross-platform later. I think Python may have intended to be cross-platform from earlier in its history.

Both may use C extensions for speed; both I believe may be linked to from C (and thus C++) as extension languages ... the version control system "Mercurial" pre-dates git (which has won the mindshare battle I believe) and Mercurial has a lot of Python in it.

Perl and Python both have arrays, associative arrays (dictionaries in Python) as their basic data structures above the basic types. Neither (nearly sure about Python on this one) worry too much what is a string and what is an integer or floating point value; they work it out and mostly translate as required.

Where the languages differ most strongly in is in syntax.

Python doesn't use { braces } like C, C++, Java et al but indentation to specify what is inside and what is outside a loop or if-then-else.

Perl puts a character in front of every variable to give a clue to its type: $ for scalars (strings and numbers [Edit: also references to associative arrays, lists, and a few other things], @ for arrays and % for associative arrays (dicts).

 

In Python, group[c][member] might be a dict group with index c containing another dict, with here the member being the second key.

In Perl, $$group{$c}{member} or $$group{$c}{$member} depending whether "member" is a variable or not. Or $group->{$c}->{$m} for the former or $group->{$c}->{member} for the latter.

If a portion of a complex data structure is an array some operator precendence gets involved so things like @{$group{$c}} might be seen as "@" binds too tightly when $group{$c} is the thing that is a list.

 

A couple of code fragments, which I won't try to explain in detail

First, perl. Note the one line "if" statement where the conditional comes last. Use of that feature can help make code nice, short, and readable or totally obscure.

for my $c (@clubs) {
    my $url = "https://api.chess.com/pub/club/$c/members";
    my $data = json_data($ua, $url);

    # 1. Club members
    for my $m (@{$data->{weekly}}, @{$data->{monthly}}, @{$data->{all_time}}) {
        $club{$c}{$$m{username}} = 1;
    }

    # 2. "Forget" club admins, who are also members
    $url = "https://api.chess.com/pub/club/$c";
    $data = json_data($ua, $url);
    for my $a (@{$$data{admin}}) {
        # Club members gives usernames, admins from the profile get URLs
	$a =~ s,.*/,,;
        warn "$c forgetting admin $a\n" if defined $opt_D;
	delete $club{$c}{$a};
    }

    ...
}

Python:

    for c in clubs:
        # 1. Club members
        club[c] = {}
        url = "https://api.chess.com/pub/club/{0}/members".format(c)
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        data = r.json()
        for period in data["weekly"], data["monthly"], data["all_time"]:
            for m in period:
                username = m["username"]
                club[c][username] = 1

        # 2. "Forget" club admins, who are also members
        url = "https://api.chess.com/pub/club/{}".format(c)
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        data = r.json()
        for a in data["admin"]:
            m = p.search(a)
            del club[c][m.group()]

Now, as for IDEs, perl's complicated syntax makes it hard, but emacs' perl mode does a good job generally and I expect any other editor that knows the laguage at all wll do a reasonble job. Maybe with an extra press of the tab key here and there to say "indent another level" but it's not a major difficulty. I have more trouble with vi/vim for Python as i've not looked for how to turn hard tabs off and always use spaces.

skelos

One of the existing perl modules on CPAN is Chess::Rep. This module is intended not to play chess but to provide a board representation and move format translation for other tools to use.

 

As of the beginning of June, 2018 I am now the primary maintainer of this module. Requests and suggestions are thus most welcome.

Note please that there is going to be a backwardly incompatible release at some time: the module currently uses die() internally and that should be Croak() or perhaps just an error return depending on cases.

Otherwise, code dependent upon bugs (e8=Q being returned as 'san') will be corrected to match FIDE's SAN definition in the current laws of chess and e8Q will be returned instead.

An additional move output of 'pgn' will be added, plus I think UCI. (I don't personally particularly need 'pgn' but it makes sense, and UCI I do want to be able to convert PGN to UCI for talking to chess engines.)