2008-11-30 21:11Hate PerlThat’s not a very helpful (or fair) title, but it’s what I’ve called a little file on my computer containing some helpful reminders about how unintuitive programming in Perl is. My incompatibility with Perl may well be to do with my personal preference, or inexperience with it, but compared to the half dozen programming languages I feel I know well, Perl seems to be deliberately difficult with no obvious benefit. Most of my observations below focus on Perl’s idea of scope, as the confusing rules it uses can, I’ve found, be grouped together to tell a story of misdirection and contradiction. If I was writing a post about all the things I dislike about the language, it would take me more than a month to complete, but just so you know, it would start with the fact that Perl doesn’t have Boolean data types or a The setupFor the purposes of this blog post, I will present the sections of my little file one at a time, and write a bit to explain the confusion it represents. At the end, I will include the whole thing so you can print it off and put it on your wall, to remind yourself not to waste your time with this language. Anyway, the first part of the file is a simple function declaration, or what would be a simple function declaration in any other language: #!/usr/bin/perl use strict; sub answer($) { my ($arg) = @_; if ($arg) { print "1\n"; } else { print "0\n"; } } The idea is that I can call the Attempt 1my $a = 1; sub foo() { answer($a); } foo(); Simple right? I’m declaring a variable Attempt 2sub bar() { answer($b); } my $b = 1; bar(); So this time we expect the output to be 1, because Attempt 3my $c = 1; sub baz() { answer($c); } $c = 0; baz(); When the function is defined, Attempt 4my $d = 0; if (1) { my $d = 1; } answer($d); What could go wrong? The The full scriptBelow is the full script, with comments to help you remember why Perl acts so strangely. #!/usr/bin/perl use strict; sub answer($) { my ($arg) = @_; if ($arg) { print "1\n"; } else { print "0\n"; } } # Output will be 0, because $a isn’t an argument to foo(), right? my $a = 1; sub foo() { answer($a); } foo(); # No, because foo() uses the $a in the outer scope. # Output will be 1, because $b is set before the call to bar(), right? sub bar() { answer($b); } my $b = 1; bar(); # No, because $b is set after bar() which uses it is defined. # Output will be 1, because $c is defined before the function, right? my $c = 1; sub baz() { answer($c); } $c = 0; baz(); # No, because $c is updated after the function definition. # Output will be 1, because $d is always set, right? my $d = 0; if (1) { my $d = 1; } answer($d); # No, because $d was declared in a different scope. As explained, the script outputs: 1 0 0 0 RevealI hope you all spotted the obvious trick I used there. If I hadn’t put those sections in the correct order, the script would produce a warning, wouldn’t it? For those of you who don’t realise what I mean (but haven’t given up on the idea of ever understanding Perl), try changing Global symbol "$e" requires explicit package name at ./hate-perl.pl line 17. Execution of ./hate-perl.pl aborted due to compilation errors. Some of you may still be asking why So what’s the best language in the world then? Surely not Java, with its verbose syntax and strict object-oriented structuring of code? No, the answer is Groovy. |
QuicksearchCategoriesSyndicate This BlogBlog Administration |
The title of this post is even less helpful (and less fair) than the title of the blog post which inspired it, so it should probably be viewed as parody. However, I do sadly have a file on my computer called hate.php containing a series of lines of valid PHP source code which produce unexpected and even perplexing results.
Tracked: Apr 30, 19:30