http://www.perlmonks.org?node_id=266609

In Python, a programming language that I don't like much, you don't use curly braces or keywords to delimit code blocks. Instead, indenting is required. The block ends when indenting ends.

I hate forced indenting. Even though I always indent, I don't want this to be necessary - I want to code in my own style, and be able to change that style when I feel like doing so. This is the number one reason for me to not learn anything about Python.

But apparently some people like forced indentation and some even use Python because of only that. Well, Perl can do this too. Let's start with an example script, that I will call test.pl:

#!/usr/bin/perl -w use strict; $a = 0; while ($a < 5) { print ++$a, "\n"; for $b (1..10) { print "$a $b\n"; } for $b (11, 12) { print "$a $b\n"; } } print "done\n";
(Yes, this does use package globals, yes, strict is kind of useless because of that. I just typed "use strict;" because it's a habit. A good one, IMO.)

Now let's remove the curlies, since when I'm done, they will be inserted automatically.

#!/usr/bin/perl -w use lib "."; use Test; use strict; $a = 0; while ($a < 5) print ++$a, "\n"; for $b (1..10) print "$a $b\n"; for $b (11, 12) print "$a $b\n"; print "done\n";
As you can see, I added use Test;, because my test module will be named Test.pm. I like keeping things simple and obvious. use lib "."; is there to make sure I'm not using the other module called Test :).

I have no idea how Python's indenting rules are, and don't really want to know either. So I decided to create some rules:

foo bar; baz quux xyzzy blah; moo
equals, more or less:
foo { bar; } baz; { quux { xyzzy { blah; } } } moo

Source filtering is cool. Dangerous and scary, but extremely cool. And with Filter::Simple, it's also very easy.

package Test; use strict; use Filter::Simple; FILTER_ONLY code => sub { no warnings; my @lines = split /\n/, $_; my @indents; for (@lines) { my ($indent) = /^(\s*)/; $indent =~ s/\t/12345678/g; $indent = length $indent; if ($indent > $indents[-1]) { s/^/{/; push @indents, $indent; } elsif ($indent < $indents[-1]) { while ($indent < $indents[-1]) { defined pop @indents or last; s/^/}/; } } } $_ = join "\n", @lines; $_ .= "}" x @indents; }; 1;
As this is a quick hack, I added no warnings; to get rid of warnings caused by the initial emptiness of @indents. I also did not do much testing. To be honest: test.pl was the only test I did. I hate forced indenting, so I'd rather not write more testing code for this :)

Anyway, this works. Maybe Python coders like Perl better now. Perhaps not.

Perl can, without a lot of code, support forced indenting. I wonder if Python people can hack up something to support curly delimited blocks in Python, without mandatory indenting.

Is Python flexible enough? (I'm asking this in the wrong place; I should be trolling a Python forum instead.)

Please do not use this module. Mandatory indenting is evil.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

P.S. / Update: I'm not actually being serious :)

Another update:
10:09 < scrottie> hrm.  should write a source filter that reads white-space structured 
                  perl and adds the { and }'s
10:09 <@Juerd> scrottie: Sorry.
10:09 <@Juerd> scrottie: http://perlmonks.org/index.pl?node_id=266609
10:09 <@Juerd> I win :)
10:09 < scrottie> no kidding!?
10:09 <@Juerd> This was 2 days ago, even :)
10:09 < scrottie> you're a crazy fuck, you know that?