OO Perl, iterators

G

Gregory Toomey

I'm a bit confused about the Perl OO syntax. From my reading "foreach" acts
as an iterator.

Any somebody tell me in the standard example below:
- how to count the total number of $query->answer
- how to access the i'th answer; $query->answer[$i] does not work
- how to access a random answer

thanks
gtoomey
--
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "NS");

if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
print $rr->nsdname, "\n";
}
}
 
A

Anno Siegel

Gregory Toomey said:
I'm a bit confused about the Perl OO syntax. From my reading "foreach" acts
as an iterator.

It is a loop construct. An iterator is something that produces
values to iterate over said:
Any somebody tell me in the standard example below:

For all of these, do

my @answers = $query->answer;

- how to count the total number of $query->answer

my $total = @answers;
- how to access the i'th answer; $query->answer[$i] does not work

my $i_th = @answers[ $i];
- how to access a random answer

my $rand_ans = @answers[ rand @answers];
thanks
gtoomey
--
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "NS");

if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
^^^
You're not running under strict! You've been around long enough to
know better.
print $rr->nsdname, "\n";
}
}

Anno
 
G

Gregory Toomey

Anno said:
Gregory Toomey said:
I'm a bit confused about the Perl OO syntax. From my reading "foreach"
acts as an iterator.

It is a loop construct. An iterator is something that produces
values to iterate over said:
Any somebody tell me in the standard example below:

For all of these, do

my @answers = $query->answer;

- how to count the total number of $query->answer

my $total = @answers;
- how to access the i'th answer; $query->answer[$i] does not work

my $i_th = @answers[ $i];
- how to access a random answer

my $rand_ans = @answers[ rand @answers];
thanks
gtoomey
--
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "NS");

if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
^^^
You're not running under strict! You've been around long enough to
know better.
print $rr->nsdname, "\n";
}
}

Anno

That code was from the NET::DNS module.

I think I found the answer to my original question.
The syntax $query->answer[$i] is wrong; ($query->answer)[$i] works even
though it looks strange.

gtoomey
 
A

Anno Siegel

Gregory Toomey said:
Anno said:
Gregory Toomey said:
I'm a bit confused about the Perl OO syntax. From my reading "foreach"
acts as an iterator.

It is a loop construct. An iterator is something that produces
values to iterate over said:
Any somebody tell me in the standard example below:

For all of these, do

my @answers = $query->answer;

- how to count the total number of $query->answer

my $total = @answers;
- how to access the i'th answer; $query->answer[$i] does not work

my $i_th = @answers[ $i];
- how to access a random answer

my $rand_ans = @answers[ rand @answers];
thanks
gtoomey
--
use Net::DNS;
my $res = Net::DNS::Resolver->new;
my $query = $res->query("example.com", "NS");

if ($query) {
foreach $rr (grep { $_->type eq 'NS' } $query->answer) {
^^^
You're not running under strict! You've been around long enough to
know better.
print $rr->nsdname, "\n";
}
}

Anno

That code was from the NET::DNS module.

So? You are responsible for the code you post, even if you take it from
somewhere else.
I think I found the answer to my original question.
The syntax $query->answer[$i] is wrong; ($query->answer)[$i] works even
though it looks strange.

It's a normal list slice, nothing strange about it. However, it
will calculate the entire list of answers each time you want to
extract a single element. Caching the list, as in my code above,
avoids that.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
my $i_th = @answers[ $i];

Just a quick correction: @answers[$i] is an array slice; ITYM:

my $i_th = $answers[ $i];

;-)

Sinan
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
my $i_th = @answers[ $i];

Just a quick correction: @answers[$i] is an array slice; ITYM:

my $i_th = $answers[ $i];

Oh, that too... Same mistake as with the random element you mercifully
snipped. I was thinking Perl 6[1], that's my excuse.

Anno

[1] Though I don't understand how Larry can say Perl 6 is still going
to be Perl when the variables don't change their sigils like a
chameleon (ha!) its colors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top