The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mpp::Text - Subs for manipulating typical makefile text

pattern_substitution

  @pieces = pattern_substitution($pattern, $dest, @words)

Performs a pattern substitution like the $(patsubst ) function (in fact, $(patsubst ) is implemented using this. $pattern contains a % as a wildcard, and $dest contains a matching %. The substitution is applied to each word in @words, and the result returned as an array.

For example:

  @pieces = pattern_substitution('%.c', '%.o', 'file1.c', 'file2.c')

returns ('file1.o', 'file2.o').

index_ignoring_quotes

  my $index = index_ignoring_quotes($string, 'substr'[, position]);

Works like index($string, 'substr'[, position]), except that the substring may not be inside quotes or a make expression.

index_ignoring_single_quotes

This is similar, but ignores only the characters in '' and the one after \.

max_index_ignoring_quotes

Like index_ignoring_quotes, except that it returns the index to the last instance rather than the first.

split_on_whitespace

  @pieces = split_on_whitespace($string);

Works just like

  @pieces = split(' ', $string)

except that whitespace inside quoted strings is not counted as whitespace. This should be called after expanding all make variables; it does not know anything about things like "$(make expressions)".

There are three kinds of quoted strings, as in the shell. Single quoted strings are terminated by a matching single quote. Double quoted strings are terminated by a matching double quote that isn't escaped by a backslash. Backquoted strings are terminated by a matching backquote that isn't escaped by a backslash.

join_with_protection

  $string = join_with_protection(@pieces);

Works just like

  $string = join(' ', @pieces)

except that strings in @pieces that contain shell metacharacters are protected from the shell.

split_on_colon

  @pieces = split_on_colon('string');

This subroutine is equivalent to

  @pieces = split(/:+/, 'string');

except that colons inside double quoted strings or make expressions are passed over. Also, a semicolon terminates the expression; any colons after a semicolon are ignored. This is to support grokking of this horrible rule:

  $(srcdir)/cat-id-tbl.c: stamp-cat-id; @:

unquote

  $text = unquote($quoted_text)

Removes quotes and escaping backslashes from a name. Thus if you give it as an argument \""a bc"'"'

it will return the string

    "a bc"

You must already have expanded all of the make variables in the string. unquote() knows nothing about make expressions.

requote

  $quoted_text = requote($unquoted_text);

Puts quotes around the text, and escapes any quotes inside the text, so that calling unquote() on $quoted_text will return the same string as $unquoted_text.

hash_neq

  if (hash_neq(\%a, \%b)) { ... }

Returns true (actually, returns the first key encountered that's different) if the two associative arrays are unequal, and '' if not.

is_cpp_source_name

  if (is_cpp_source_name($filename))  { ... }

Returns true if the given filename has the appropriate extension to be a C or C++ source or include file.

is_object_or_library_name

  if (is_object_or_library_name($filename)) { ... }

Returns true if the given filename has the appropriate extension to be some sort of object or library file.

getopts

  getopts %vars, strictflag, [qw(o optlong), \$var, wantarg, handler], ...

Almost as useful as Getopt::Long and much smaller :-)

%vars is optional, any VAR=VALUE pairs get stored in it if passed.

strictflag is optional, means to stop at first non-option.

Short opt may be empty, longopt may be a regexp (grouped if alternative).

$var gets incremented for each occurrence of this option or, if optional wantarg is true, it gets set to the argument. This can be undef if you don't need it.

If an optional handler is given, it gets called after assigning $var, if it is a ref (a sub). Any other value is assigned to $var.