in reply to Re: Multiline Hash - how to get rid of formatting?
in thread Multiline Hash - how to get rid of formatting?
This is quite cute...
use 5.010; use strict; QTRIM_SETUP: { my $trimmer; BEGIN { $trimmer = sub ($) { @_ = map { s/(^\s+)|(\s+$)//g; $_ } split /\n/, shift; shift until length $_[0]; pop until length $_[-1]; join "\n", @_; } } use PerlX::QuoteOperator qtrim => { -emulate => 'q', -with => $t +rimmer }; use PerlX::QuoteOperator qqtrim => { -emulate => 'qq', -with => $t +rimmer }; } my $string = qtrim{ Hello World }; say "[$string]";
Obviously, if you were doing this in "real life" you'd move the QTRIM_SETUP block into a module, so that you could just do:
use 5.010; use strict; use PerlX::Qtrim; my $string = qtrim{ Hello World }; say "[$string]";
There's a slight annoyance with qqtrim in that this:
my $embedded = "\n \n"; my $multiline = qqtrim{ Hello $embedded World };
... will also end up stripping the spaces in $embedded which is somewhat counter-intuitive.
|
|---|