# use 4 spaces indents and automatic line ending
use indent indent_size => 4, indent_eol => 1;
indent::new;
indent::printf "Hello, world!";
indent::new;
indent::printf "Hello, world!";
indent::back;
indent::printf "Hello, world!";
indent::back;
####
Hello, world!
Hello, world!
Hello, world!
####
package indent;
use strict;
use warnings;
our $INDENT = " " x 4;
our $EOL = $\;
sub import {
shift;
goto &config;
}
sub config {
my %opts = @_;
if ( $opts{indent_tab} ) {
$INDENT = "\t";
} elsif (
defined $opts{indent_size}
&& $opts{indent_size} =~ /^\d+$/
) {
$INDENT = " " x $opts{indent_size};
}
if ( defined $opts{indent_eol} ) {
$EOL = $opts{indent_eol} ? "\n" : $\;
}
}
my @indent = ( "" );
sub reset {
@indent = $indent[0];
}
sub current {
$indent[-1];
}
sub new {
push @indent, current . $INDENT;
}
sub back {
pop @indent if @indent > 1;
}
sub print {
local $\ = $EOL;
CORE::print current, @_;
}
sub vprint {
local $\ = $EOL;
CORE::print current, $_ for ( @_ );
}
sub printf {
indent::print sprintf(( shift || "" ), @_);
}
1;