Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Multiline Hash - how to get rid of formatting?

by tobyink (Abbot)
on Apr 30, 2012 at 07:34 UTC ( #968015=note: print w/replies, xml ) Need Help??

in reply to Multiline Hash - how to get rid of formatting?

Syntax::Feature::Ql is close to what you want. It gives you a quote-like operator ql such that:

use syntax 'ql'; use Test::More tests => 1; my $greeting = ql{ Hello World }; is($greeting, q{Hello World});

Following the same technique you could create a quote-like operator which trims whitespace from the start of each line but preserves line breaks.

If you're happy just to adjust the string with a regular expression though, you're probably looking for:

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: Multiline Hash - how to get rid of formatting?
by tobyink (Abbot) on Apr 30, 2012 at 10:18 UTC

    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.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: Multiline Hash - how to get rid of formatting?
by tospo (Hermit) on Apr 30, 2012 at 08:31 UTC
    sorry, but I think this will set the OP off on the wrong trajectory. I think the problem is confusion about the usage of data structures and formatting in script files vs. formatting that Perl sees.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968015]
[LanX]: I doubt
[LanX]: only nonlocal in Py3
[shmem]: so, python sucks
LanX NSFW!!!

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-03-20 17:53 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (256 votes). Check out past polls.