develooper Front page | perl.beginners | Postings from September 2008

Problem with Eval for Constructing AoA

Thread Next
From:
Gundala Viswanath
Date:
September 4, 2008 02:44
Subject:
Problem with Eval for Constructing AoA
Hi,

This cowde below tries to convert
the string in newick format into the corresponding
data structure (Array of Array).

But somehow the EVAL function doesn't work
as expected. What's wrong with my code here?

__BEGIN__
use Data::Dumper;
use Carp;

my $str = "(foo,(bar,qux))"; #Newick format
print "$str\n";

my $ar = conv_newick2aoa($str);
print Dumper $ar ;

sub conv_newick2aoa {

    my $nstr_in = shift;
    my $nstr    = $nstr_in;
    for ($nstr) {
        s/\\/\\\\/g;
        s/'/\\'/g;
        s/\(/['/g;
        s/\)/']/g;
        s/,/','/g;
    }

    return eval{$nstr};
}
__END__

Why it doesn't give this output instead:

$VAR1 = [
          'foo',
          [
            'bar',
            'qux'
          ]
        ];


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About