Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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:

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


Comment on Re: Multiline Hash - how to get rid of formatting?
Select or Download Code
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.
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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968015]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-23 04:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls