Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

#!/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 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:

  • A literal tab character has a width of 8
  • To create a block, increase indent
  • To end a block, decrease indent, but no more than the indentation of the line before the block that you're ending
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: 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 => '', plp_site => '', 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:
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?

In reply to Mandatory indenting by Juerd

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2015-11-27 03:16 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (715 votes), past polls