I'm trying to use the Text::Balanced perl module for the purpose of extracting the text enclosed in the curly brackets. In this particular case the input is a bind
configuration file /etc/named.conf
, but I'm looking for general use.
The problem is, that the I'm getting undef
result and as a consequence an uninitialized value error.
Here is my code ( it's just a stump now, not dealing with recursion and other stuff yet):
#!/usr/bin/env perl
use strict;
use diagnostics;
use Text::Balanced qw (
extract_bracketed
) ;
my $config = '/etc/named.conf';
open my $fh, '<', $config or die "Cannot open $config: $!";
$/ = undef;
my $text = <$fh>;
close $fh;
my $content = extract_bracketed( $text, '{}' );
print $content ;
I have read http://perldoc.perl.org/Text/Balanced.html and cannot figure out, what am I doing wrong ( I use scalar variable instead of array, but this shouldn't be a problem )
Also there is a similar issue here Text::Balanced and multiline xml, but it gets answered in the way that does not fit my use case ( specific solution for .xml parsing)
Please advise
Thanks
In regex you can use this:
my $str = '
<statement-1> ["<statement-1-name>"] [<statement-1-class>] {
{ {<option-1>;}
<option-2>;}
<option-N>;
};
<statement-2> ["<statement-2-name>"] [<statement-2-class>] {
<option-1>;
<option-2>;
<option-N>;
};
<statement-N> ["<statement-N-name>"] [<statement-N-class>] {
<option-1>;
<option-2>;
<option-N>;
};';
print "L: $1\n" while($str=~m/\{((?:[^{}]++|\{(?1)\})++)\}/gs);