Personally, I would rather use YAML or one of the config-modules from CPAN, but it seems, other non-Perl programs are also depending on the environment, right?

This imperfect script should do some very basic parsing and interpolation. However, it would fail in situations like Y=`echo fails` or where here-documents are involved. If you need every functionality that you shell provides, stay with it. You might be able to get rid of the wrapper script using exec or system, though.

use strict; use warnings; use Shell::Parser; use Data::Dumper; sub parse_shellscript_from_fh { my ($fh) = @_; my %vars; my $parser = new Shell::Parser syntax => 'bash', handlers => { assign => sub { my ($self, %args) = @_; my ($name, $value) = $args{token} =~ /^\s*(\w+)\s*=\s*\"(.*)\"\s*$/s; if ( defined $value ) { #-- multi-line/array? $value =~ s/\n//msg; $vars{$name} = $value; } else { warn "No value for variable ", ($name//'?'), "\n"; } } }; #-- parsing my $text = join("", <$fh>); $parser->parse( $text ); $parser->eof; #-- rudimentary variable interpolation my $max_passes = 5; my $not_substituted; for (1 .. $max_passes ) { $not_substituted = 0; foreach my $varname ( keys %vars ) { $vars{$varname} =~ s|\$(\w+)| $vars{$1} // do{ "\$$1" } |ge; $not_substituted += $vars{$varname} =~ tr/$/$/; } last unless $not_substituted; } die "Recursion or too few parses ... " if $not_substituted; return \%vars; } print "Vars: ", Dumper( parse_shellscript_from_fh( *DATA ) ), "\n"; __DATA__ A="foo" B="some other data" C="appending $A" D=" some list with more data and $B " X=<<EOF does not work there EOF Y=`echo fails`

Result:

No value for variable ? No value for variable ? Vars: $VAR1 = { 'A' => 'foo', 'D' => ' some list with more data and some other data ', 'C' => 'appending foo', 'B' => 'some other data' };


In reply to Re: Parsing bash data by Perlbotics
in thread Parsing bash data by ag4ve

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.