Perl - TAP::Harness and test_args - perl

I am using TAP::Harness in perl like this:
$harness = TAP::Harness->new({
formatter_class => 'TAP::Formatter::Console',
merge => 1,
verbosity => 1,
normalize => 1,
color => 1,
test_args => ['--url', $url, '--session', $session],
});
Sometime later I call $harness->runtests(), passing an array of several tests.
Problem is, in all of my tests:
use Data::Dumper;
print Dumper \#ARGV;
Outputs:
$VAR1 = [];
Does test_args not come out in #ARGV in the underlying tests? I need to pass some options through to each test.

Does your initialization work? I had to pass hashref to the constuctor to make it work:
use TAP::Harness;
$harness = TAP::Harness->new({
formatter_class => 'TAP::Formatter::Console',
merge => 1,
verbosity => 1,
normalize => 1,
color => 1,
test_args => ['--url', $url, '--session', $session],
});
$harness->runtests('simple.t');
In the test simple.t:
use Test::More;
use Data::Dump qw(dump);
dump [#ARGV]; # prints ["--url", "", "--session", ""]
done_testing;

Related

Perl XML::Simple output nested node

As input I have
$XML = {'node1' => {'node2' => {'node3' => {'node4'}}}};
then I generate the out xml
print(XMLout($XML, KeepRoot => 1));
and I get
<node1>
<node2 name="node3" node4="" />
</node1>
How can I get this as output
<node1>
<node2>
<node3>
<node4></node4>
</node3>
</node2>
</node1>
perl -MXML::Simple -e '$XML = {"n1" => {"n2" => [{"n3" => [{"n4"=>[{}]}]}]}};
print(XMLout($XML, KeepRoot=>1));'
Gives
<n1>
<n2>
<n3>
<n4></n4>
</n3>
</n2>
</n1>
The use of XML::Simple is discouraged. https://metacpan.org/pod/XML::Simple
XML::Simple isn't actually simple to use. You are better off with XML::LibXML.
Here is an example that shows how you can find how the data structure should look.
use warnings;
use strict;
use XML::Simple;
use Data::Dumper;
my $sample =
"<node1>
<node2>
<node3>
<node4></node4>
</node3>
</node2>
</node1>";
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 1);
my $ref = $xs->XMLin($sample);
print "\n---------------\n", Dumper($ref), "\n--------------\n";
my $xml = $xs->XMLout($ref);
print "\n----------------\n";
print $xml;
The output produced from Data::Dumper shows the data structure.
---------------
$VAR1 = {
'node1' => [
{
'node2' => [
{
'node3' => [
{
'node4' => [
{}
]
}
]
}
]
}
]
};
--------------
----------------
<node1>
<node2>
<node3>
<node4></node4>
</node3>
</node2>
</node1>
Without ForceArray it looks like the output you received.

Why does Data::Dumper show values that link to other values?

using Data Dumper after parsing some JSON data, I got something like this:
$VAR1 = {
param1 => 'foo',
param2 => $VAR1->{param1}
};
Do I get it right, that param2 is a linked to param1 value?
What is this called? Dynamic hash?
Thanks in Advance, Steve
No need to be confused, the value of param2 is simply a reference that was encountered before in the structure, so Data::Dumper by default shows it as the reference it is. You can set $Data::Dumper::Deepcopy = 1; and have Data::Dumper print the actual values instead if you need it for something.
E.g.
my $foo = 'foo';
my $test = {
param1 => \$foo,
param2 => \$foo
};
print Dumper($test);
will print out
$VAR1 = {
'param2' => \'foo',
'param1' => $VAR1->{'param2'}
};
But if you start with something like:
use Data::Dumper;
$Data::Dumper::Deepcopy = 1;
Your output will be:
$VAR1 = {
'param1' => \'foo',
'param2' => \'foo'
};
The default behavior is more useful for visual inspection.

How to get "Var1" value of dumper in perl

When i use below code then it gives output, But i want "width", "file_media_type", "file_ext" values, But I am unable to get this value in individual. I am very new with Perl Please help me!
Code
use warnings ;
use strict;
use Image::Info qw[image_info];
use Data::Dumper;
my $file = 'd:\perl\test\a.jpg';
my $info = Dumper(image_info($file));
print $info;
Output
$VAR1 = {
'width' => 45,
'file_media_type' => 'image/png',
'file_ext' => 'png',
'PNG_Chunks' => [
'IHDR',
'gAMA',
'cHRM',
'IDAT',
'IEND'
],
'Chunk-cHRM' => ' z% Çâ ·  ÇF u0 O` :ù ?o',
'PNG_Filter' => 'Adaptive',
'color_type' => 'RGB',
'height' => 20,
'Gamma' => '0.45454',
'resolution' => '1/1',
'SampleFormat' => 'U8',
'Compression' => 'Deflate'
};
image_info($file) returns a hash reference. Looking at the dump you know the keys available (the keys are strings before =>)
$info = image_info($file);
foreach my $key ( qw/width file_media_type file_ext/ ){
print "$key:$info->{$key}\n";
}

Not a GLOB reference at ...IO/Select.pm line 61

I am passing an IO::Select object as handle to IO::Select::add and getting the following error:
Not a GLOB reference at ...IO/Select.pm line 61
This worked for years and since a couple of days (weeks) people are beginning to get the error. Looking online I was not able to find out if I was using the call in the wrong way or if some recent perl or DNS::Resolver update could be the cause of the problem.
I am doing the following
#!/usr/bin/perl
use IO::Select;
use Net::DNS;
# create a resolver object
my $res = Net::DNS::Resolver->new();
# create an IO::Select object
my $sel = IO::Select->new();
# perform the background DNS query
my $sock = $res->bgsend('corti.li');
# adding the socket generates the error
$sel->add($sock);
Adding some debugging I don't see anything wrong:
#!/usr/bin/perl
use Data::Dumper;
use IO::Select;
use Net::DNS;
# create a resolver object
my $res = Net::DNS::Resolver->new();
warn Dumper $res;
# create an IO::Select object
my $sel = IO::Select->new();
warn Dumper $sel;
# perform the background DNS query
my $sock = $res->bgsend('corti.li');
warn Dumper $sock;
# adding the socket generates the error
$sel->add($sock);
produces
$VAR1 = bless( {
'force_v4' => 0,
'retrans' => 5,
'persistent_udp' => 0,
'adflag' => 0,
'force_v6' => 0,
'port' => 53,
'answerfrom' => '',
'prefer_v6' => 0,
'defnames' => 1,
'tcp_timeout' => 120,
'udp_timeout' => 30,
'igntc' => 0,
'udppacketsize' => 0,
'dnsrch' => 1,
'recurse' => 1,
'srcaddr' => 0,
'persistent_tcp' => 0,
'retry' => 4,
'cdflag' => 0,
'nameserver4' => [
'129.132.98.12'
],
'searchlist' => [
'd.ethz.ch',
'ethz.ch'
],
'tsig_rr' => undef,
'nameserver6' => [
'2001:67c:10ec::c'
],
'usevc' => 0,
'dnssec' => 0,
'debug' => 0,
'errorstring' => 'unknown error or no error',
'srcport' => 0
}, 'Net::DNS::Resolver' );
$VAR1 = bless( [
undef,
0
], 'IO::Select' );
$VAR1 = bless( [
',
1,
undef,
undef,
undef,
[
bless( \*Symbol::GEN0, 'IO::Socket::IP' ),
1448125508,
'129.132.98.12',
54103
]
], 'IO::Select' );
Not a GLOB reference at /Users/corti/perl5/perlbrew/perls/perl-5.22.0/lib/5.22.0/darwin-2level/IO/Select.pm line 61.
I am using Perl 5.22 and Net::DNS 1.03
Using Perl 5.18 and Net::DNS 0.74 the code works.
Am I doing something wrong or it is a bug?
The problem is caused by a change in the API of Net::DNS::Resolver bgsend
See #108745: Net::DNS::Resolver bgsend

bioperl package Bio::Tree::Tree can't locate object method as_text

I'm trying to use as_text method from Bio::Tree::Tree I get this message: can't locate object method as_text via package Bio::Tree::Tree
I'm using the example here
Note that I tried other methods in the same package and they worked normally.
my $input = new Bio::TreeIO(-file => "bintree.nw",
-format => "newick");
my $tree = $input->next_tree;
my $tree_as_string = $tree->as_text($format);
print $tree_as_string;
The print Dumper($input) give this result:
$VAR1 = bless( {
'_bootstrap_style' => 'traditional',
'_handler' => bless( {
'_treelevel' => 0,
'_currentnodes' => [],
'_lastitem' => {
'tree' => 0,
'current' => [],
'id' => 0,
'node' => 0,
'leaf' => 0
},
'nodetype' => 'Bio::Tree::Node',
'_root_verbose' => 0,
'treetype' => 'Bio::Tree::Tree',
'_currentitems' => [],
'_nodect' => [
undef,
2,
0,
0,
0,
0,
0,
0,
0
]
}, 'Bio::TreeIO::TreeEventBuilder' ),
'_file' => 'bintree.nw',
'newline_each_node' => undef,
'internal_node_id' => 'id',
'_root_cleanup_methods' => [
sub { "DUMMY" }
],
'_flush_on_write' => 1,
'_filehandle' => \*Symbol::GEN0,
'_root_verbose' => 0,
'_print_tree_count' => 0
}, 'Bio::TreeIO::newick' );
Here is the Print Dumper ($tree)
is there a mistake ? or it's a bug ?
Thanks in advance
Your code is not working because you have not set the variable $format to anything, so the Bio::TreeIO class cannot find a class to load for the format. Try this code (it works for me):
#!/usr/bin/env perl
use strict;
use warnings;
use Bio::TreeIO;
my $usage = "$0 treefile\n";
my $infile = shift or die $usage;
my $treeio = Bio::TreeIO->new(-file => $infile, -format => 'newick');
print $treeio->next_tree->as_text('newick');
EDIT: Here is a version using your tree as the input:
#!/usr/bin/env perl
use strict;
use warnings;
use Bio::TreeIO;
my $treeio = Bio::TreeIO->new(-fh => \*DATA, -format => 'newick');
print $treeio->next_tree->as_text('newick');
__DATA__
(((A:5,B:5)90:2,C:4)25:3,D:10);
If we run this code, it prints the tree, as expected.
$ perl so18645089.pl
(((A:5,B:5)90:2,C:4)25:3,D:10);
I'm using BioPerl 1.6.901, the latest version (and the version the documentation on CPAN describes). Version 1.6.0 is very old (>5 years) and is not even on CPAN anymore. I bet if you upgrade, your troubles will disappear.