siberia-man has asked for the wisdom of the Perl Monks concerning the following question:
The resulting output is as follows:# 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;
The full source of the module is below: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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple indentation. Does it need improvement?
by haukex (Archbishop) on Dec 12, 2017 at 22:33 UTC | |
by siberia-man (Friar) on Dec 13, 2017 at 04:33 UTC | |
|
Re: Simple indentation. Does it need improvement?
by holli (Abbot) on Dec 12, 2017 at 21:57 UTC | |
by choroba (Cardinal) on Dec 12, 2017 at 22:07 UTC | |
by siberia-man (Friar) on Dec 12, 2017 at 22:51 UTC | |
|
Re: Simple indentation. Does it need improvement?
by Laurent_R (Canon) on Dec 12, 2017 at 22:38 UTC | |
|
Re: Simple indentation. Does it need improvement?
by karlgoethebier (Abbot) on Dec 13, 2017 at 12:22 UTC | |
|
Re: Simple indentation. Does it need improvement?
by siberia-man (Friar) on Dec 17, 2017 at 19:52 UTC |