This question has an accepted answer, but it is not complete. Bio::TreeIO has an
as_text method that is available if you upgrade your BioPerl to a recent version, and you may use this method instead of saving to a file.
my $treeio = Bio::TreeIO->new(-fh => \*DATA, -format => 'newick');
my $tree = $treeio->next_tree;
Explanation: If we had tried to print
$treeio above, instead of calling
as_text on the tree, we will see that
$treeio is Bio::TreeIO::newick object.
print ref $treeio; # prints Bio::TreeIO::newick
Printing the bare object handle will technically print the object name and a reference showing the underlying data structure, and while you can inspect this, it is usually not very informative unless you know what you are looking at.
Similar to the above, Bio::Phylo has the ability to write the tree string to STDOUT. Here is the equivalent code using Bio::Phylo, which is slightly different.
my $tree = Bio::Phylo::IO->parse( -handle => \*DATA,
-format => 'newick',
-keep => ['A','C','D'])->first;
print $tree->to_newick, "\n";
The same applies here about printing with a method versus printing the object itself.
print ref $treeio; # prints Bio::Phylo::Forest::Tree
The actual answer as to why your print statement is printing '1' is that you are trying to assign the modified tree to a variable, but really you are assigning the return value of the method, which is '1' (meaning success). Just call the method on the tree object itself (as in the code above), without assignment, and print or save the tree.