NAME
String::Bash - Parameter expansion in strings
VERSION
version 1.110960
SYNOPSIS
# pass hashref
bash
"Hello %{name:-Guest}!"
, {
name
=>
'Alex'
};
# or key/value pairs
bash
"Hello %{name:-Guest}!"
,
name
=>
'Alex'
;
# or object which can('name');
my
$user
= My::Users->new(
name
=>
'Alex'
);
bash
"Hello %{name:-Guest}!"
,
$user
;
# or use lexical vars
my
$name
=
'Alex'
;
bash
"Hello %{name:-Guest}!"
;
all will print
Hello Alex
or if name is undefined or empty
Hello Guest
DESCRIPTION
String::Bash is based on shell parameter expansion from Bash, thus it allows to provide default values, substrings and in-place substitutions, changing case of characters and nesting.
The String::Bash provides bash
exported with Sub::Exporter.
REPLACEMENT VALUES
Replacements can be provided in four different ways:
Hash reference
my
$hashref
= {
param1
=>
'value1'
, ... };
bash
$format
,
$hashref
;
Key/value pairs
bash
$format
,
param1
=>
'value1'
, ...;
Object
bash
$format
,
$object
;
Please note that $object
needs to implement read/write accessors (if "%{param:=word}" is used, otherwise read-only are sufficient) for all parameters used in $format
.
Lexical variables
my
$param1
= ...;
our
$param2
= ...;
bash
$format
;
Lexical (my
) and package (our
) scalar variables visible at the scope of bash
caller are available as replacement.
FORMAT SYNTAX
Please assume that following variables are visible in below examples:
my
$param
=
'hello'
;
my
$not_set
;
my
$param2
=
'WELCOME'
;
%{param}
bash
"%{param}"
;
# hello
Value of $param
is substituted.
%{param:-word}
bash
"%{param:-word}"
;
# hello
bash
"%{not_set:-word}"
;
# word
If $param
is unset or null, the expansion of word is substituted. Otherwise, the value of $param
is substituted.
The word can be another parameter so nesting is possible:
bash
"%{not_set:-%{param2}}"
;
# WELCOME
%{param:=word}
bash
"%{not_set:=word}"
;
# word
If $param
is unset or null, the expansion of word is assigned to $param
. The value of $param
is then substituted.
Notes on replacement syntax:
If "Object" is passed as replacement than assignment will execute following code:
$obj
->
$param
(
'word'
);
If "Key/value pairs" are passed as replacement then the assignment will be applied to occurrences of param after the assignment has been done, and will be disregarded after parsing is done.
If "Lexical variables" are used, then their value will be set to word.
%{param:+word}
bash
"%{param:+word}"
;
# word
bash
"%{not_set:+word}"
;
#
If $param
is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
%{param:offset}
%{param:offset:length}
bash
"%{param:2}"
;
# llo
bash
"%{param:2:2}"
;
# ll
Expands to up to length characters of $param
starting at the character specified by offset. If length is omitted, expands to the substring of $param
starting at the character specified by offset.
%{#param}
bash
"%{#param}"
; # 5
The length in characters of the value of $param
is substituted.
%{param#word}
%{param##word}
bash
"%{param#he*l}"
; # lo
bash
"%{param##he*l}"
; # o
The word is expanded to produce a pattern (see "Pattern expansion"). If the pattern matches the beginning of the value of $param
, then the result of the expansion is the expanded value of $param
with the shortest matching pattern (the '#' case) or the longest matching pattern (the '##' case) deleted.
%{param%word}
%{param%%word}
bash
"%{param%l*o}"
;
# hel
bash
"%{param%%l*o}"
;
# he
The word is expanded to produce a pattern (see "Pattern expansion"). If the pattern matches a trailing portion of the value of $param
, then the result of the expansion is the value of $param
with the shortest matching pattern (the '%' case) or the longest matching pattern (the '%%' case) deleted.
%{param/pattern/string}
bash
"%{param/l/t}"
;
# hetlo
bash
"%{param//l/t}"
;
# hetto
bash
"%{param/#h/t}"
; # tello
bash
"%{param/%o/t}"
;
# hellt
The pattern is expanded to produce a pattern (see "Pattern expansion"). The longest match of pattern against $param
value is replaced with string. If pattern begins with '/', all matches of pattern are replaced with string. Normally only the first match is replaced. If pattern begins with '#', it must match at the beginning of the value of $param
. If pattern begins with '%', it must match at the end of the $param
. If string is null, matches of pattern are deleted and the / following pattern may be omitted.
%{param^pattern}
%{param^^pattern}
%{param,pattern}
%{param,,pattern}
bash
"%{param^}"
;
# Hello
bash
"%{param^^}"
;
# HELLO
bash
"%{param2,}"
;
# wELCOME
bash
"%{param2,,}"
;
# welcome
bash
"%{param^[hl]}"
;
# Hello
bash
"%{param^^[hl]}"
;
# HeLLo
bash
"%{param2,[WE]}"
;
# wELCOME
bash
"%{param2,,[WE]}"
;
# weLCOMe
This expansion modifies the case of alphabetic characters in $param
. The pattern is expanded to produce a pattern (see "Pattern expansion"). The '^' operator converts lowercase letters matching pattern to uppercase; the ',' operator converts matching uppercase letters to lowercase. The '^^' and ',,' expansions convert each matched character in $param
; the '^' and ',' expansions match and convert only the first character in the value of $param
. If pattern is omitted, it is treated like a '?', which matches every character.
NOTES
Pattern expansion
Pattern expansion is performed using following rules (based on filename expansion):
# Character # Replacement (perl syntax)
* .*
? .
[a-z] [a-z]
Please do not use perl regular expression syntax in pattern substitutions, or you may get unexpected results.
COMPATIBILITY WITH BASH
String::Bash provides only syntax described above and some of Bash features (like expansions of arrays) are not available - but please let me know if you need them.
SEE ALSO
AUTHOR
Alex J. G. Burzyński <ajgb@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Alex J. G. Burzyński <ajgb@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.