Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Parsing bash data

by Perlbotics (Archbishop)
on Aug 23, 2013 at 15:40 UTC ( [id://1050684]=note: print w/replies, xml ) Need Help??


in reply to Parsing bash data

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' };

Replies are listed 'Best First'.
Re^2: Parsing bash data
by ag4ve (Monk) on Aug 23, 2013 at 17:57 UTC
    That's awesome. Thanks. I don't like that that module was last maintained ~8 years ago, but it works. (I've also got to figure out why $not_substituted is getting set with my data - seems to work when I remove that die)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1050684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-03-28 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found