This seems like something that would be ideal as an OO module, your subs could be converted to methods. This would also make the objects safe to mix, instead of simply having one "global" indent level.

As for the code you posted, holli already pointed out that module names in lower case are by convention pragmas. Also, just a small note on ( shift || "" ): this means that if $_[0] is "0" or 0, it will be replaced by an empty string. You probably want the defined-or // instead, but it's usually fine to implement a printf replacement simply as sprintf(shift, @_).

Since your post inspired me, here's my idea using overloading:

use warnings; use strict; { package Indent; sub new { my $c=shift; bless { i=>shift//"\t", c=>0 }, $c } use overload '++' => sub { ++shift->{c} }, '--' => sub { my $s=shift; $$s{c}=0 if --$$s{c}<0; $$s{c} }, '""' => sub { my $s=shift; $$s{i} x $$s{c} }, '=' => sub { my $s=shift; bless {%$s}, ref $s }, } my $recurse; $recurse = sub { my ($lvl,$i) = @_ ? @_ : (0, Indent->new("foo")); print "<", $i++, "> Enter\n"; $recurse->($lvl+1,$i) unless $lvl>=2; print "<", --$i, "> Leave\n"; }; $recurse->(); __END__ <> Enter <foo> Enter <foofoo> Enter <foofoo> Leave <foo> Leave <> Leave

In reply to Re: Simple indentation. Does it need improvement? by haukex
in thread Simple indentation. Does it need improvement? by siberia-man

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!
  • 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, details, 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, summary, 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.