Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 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:

  • 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: 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?

In reply to Mandatory indenting by Juerd

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (7)
    As of 2014-08-29 00:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (275 votes), past polls