From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

String::Nudge - Indents all lines in a multi-line string

Requires Perl 5.10+ coverage 100.0% Build status at Github

VERSION

Version 1.0002, released 2021-07-29.

SYNOPSIS

sub out {
print nudge q{
A long
text.
};
}
# is exactly the same as
sub out {
print q{
A long
text.
};
}

DESCRIPTION

String::Nudge provides nudge, a simple function that indents all lines in a multi line string.

METHODS

nudge $string

# ' hello'
my $string = nudge 'hello';

nudge $number_of_spaces, $string

# ' hello'
my $string = nudge 8, 'hello';

If $number_of_spaces is not given (or isn't an integer >= 0) its default value is 4.

Every line in $string is indented by $number_of_spaces. Lines only consisting of white space is trimmed (but not removed).

MORE EXAMPLES

Usage with qi

Syntax::Feature::Qi adds qi and qqi that removes the same amount of leading whitespace as the first (significant) line has from all lines in a string:

# these three packages are equivalent:
package Example::Nudge {
use syntax 'qi';
sub out {
print nudge qi{
sub funcname {
print 'stuff';
}
};
}
}
package Example::Q {
sub out {
print q{
sub funcname {
print 'stuff';
}
};
}
}
sub out {
(my $text = <<" END") =~ s{^ {8}}{}gm;
sub funcname {
print 'stuff';
}
END
print $text;
}
}

Usage with qs

Syntax::Feature::Qs adds qs and qqs that removes all leading whitespace from all lines in a string:

# these three packages are equivalent:
package Example::Nudge {
use syntax 'qs';
sub out {
print nudge qs{
This is
a multi line
string.
};
}
}
package Example::Q {
sub out {
print q{
This is
a multi line
string.
};
}
}
sub out {
(my $text = <<" END") =~ s{^ {8}}{}gm;
This is
a multi line
string.
END
print $text;
}
}

SEE ALSO

SOURCE

https://github.com/Csson/p5-String-Nudge

HOMEPAGE

https://metacpan.org/release/String-Nudge

AUTHOR

Erik Carlsson <info@code301.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Erik Carlsson.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.