As part of a program I'm writing, I need to read some variables from a bash script and do some bash-style interpolation. My current code:
sub get_depend {
my $ebuildfname = shift;
my $ebuildcontents;
my %ebuildvars;
my $pkgname = $ebuildfname;
$pkgname =~ s|/usr/portage/||;
$pkgname =~ s|(.+)/.+/(.+).ebuild|$1/$2|;
my $pkg = parse_package_name($pkgname);
$pkg->{version} =~ s/^-//;
$ebuildvars{PV} = "$pkg->{version}";
open EBUILD, "< $ebuildfname" or die "Couldn't open '$ebuildfname'\n
+";
while(<EBUILD>) {
$ebuildcontents .= $_;
}
close EBUILD;
while($ebuildcontents =~ /\b([-A-Z0-9_]+)=\"(.*?)\"{1}?/sgc) {
$ebuildvars{$1} = $2;
}
foreach(keys %ebuildvars) {
$ebuildvars{$_} =~ s/\$\{?([-A-Z0-9_]+)\}?/$ebuildvars{$1}/gs;
}
my $depend = $ebuildvars{'DEPEND'} || '';
$depend =~ s/(\s+|\n+)/ /gs;
return $depend;
}
This one-pass interpolation works somewhat, but it doesn't get all the variables (for example: VAR1="something $VAR2" VAR2="test $VAR3" VAR3="anything $VAR4" would give me VAR1="something test $VAR3" VAR2="test $VAR3" VAR3="anything $VAR4"). How can I make this work? If I need to, I can pass it through bash. I just don't know how (with the ability to get the values back).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.