use strict; use warnings; use Math::Algebra::Symbols; my $file = <) { chomp; s/;$//; my ($lhs, $rhs) = split /\s*=\s*/, $_, 2; $rhs =~ s/(\w+)/exists $subst{$1} ? "($subst{$1})" : $1/ge; $subst{$lhs} = $rhs; $last = "$lhs = $rhs"; } print $last; #### d = ~((a & b) | c)