a simple control in an nntp client

G

George

I would like to extend the following script:

#!/usr/bin/perl -w

use strict;
use Net::NNTP ();

use constant NUMBER_OF_ARTICLES => 10;
use constant GROUP_NAME => 'alt.solaris.x86';
use constant SERVER_NAME => 'news.individual.net';
use constant NNTP_DEBUG => 0;

my $nntp = Net::NNTP->new(SERVER_NAME, 'Debug' => NNTP_DEBUG) or die;
my $USER = '';
my $PASS = '';

$nntp->authinfo($USER,$PASS) or die $!;


my($article_count, $first_article, $last_article) =
$nntp->group(GROUP_NAME) or die;


# Which XOVER fields contain Subject: and From:?
my $count = 0;
my %xover_fmt = map( ($_, $count++), @{ $nntp->overview_fmt or die} );
die unless exists $xover_fmt{'Subject:'};
my $subject_offset = $xover_fmt{'Subject:'};
my $from_offset = $xover_fmt{'From:'};

my(@xover, $start_article);
RETRIEVE: while ($#xover+1 < NUMBER_OF_ARTICLES and $last_article >=
$first_article) {

# How many articles do we need? Stop retrieving if we have enough
my $articles_required = NUMBER_OF_ARTICLES - ($#xover+1) or last
RETRIEVE;


# Fetch overview information for the articles
$start_article = $last_article - ($articles_required-1);
$start_article = $start_article > $first_article ? $start_article :
$first_article;

my $xover_query = $start_article == $last_article ?
$start_article :
[$start_article, $last_article];
my $xover_ref = $nntp->xover($xover_query) or die;

# Store headers for the articles we've retrieved
foreach (sort {$b <=> $a} keys %$xover_ref) {
push @xover, $xover_ref->{$_};
}
} continue {
# Move the pointer forward to fetch previous articles
$last_article = $start_article - 1;
}

# Disconnect from the NNTP server
$nntp->quit;

print join("\n", map ($_->[$subject_offset].' from '.$_->[$from_offset],
@xover)),"\n";


# perl script1.pl 2>text50.txt >text51.txt

Current output is:

C:\MinGW\source>perl script1.pl
Re: Solaris 10 update 6 on Intel x86. from CJT <[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Ian Collins
<[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Atiqur Rahman <[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Ian Collins
<[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Atiqur Rahman <[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Atiqur Rahman <[email protected]>
Re: Solaris 10 update 6 on Intel x86. from Atiqur Rahman <[email protected]>
Re: Solaris 10 update 6 on Intel x86. from (e-mail address removed)
Solaris 10 update 6 on Intel x86. from Atiqur Rahman <[email protected]>
Re: Booting multi-user but w/o X from (e-mail address removed)

C:\MinGW\source>

The way I would like to extend this is the following:

If the article is from 'Atiqur Rahman', then I would like to print the
entire article.

This may sound trivially easy to those with more experience with perl. I
find the control structures very difficult in perl. I've done similar
things before, but it's been a while since I've used perl. I have
_Programming Perl_ as a reference.

Thanks for your comment.

--
George

We will stand up for our friends in the world. And one of the most
important friends is the State of Israel. My administration will be
steadfast in support Israel against terrorism and violence, and in seeking
the peace for which all Israelis pray.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Have you thought of 'extension' courses?

sln

No, but your response makes me recall fondly the moments when I used to
throw eurotrash around. Typ.
--
George

I believe that God has planted in every heart the desire to live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Typical George W. Bush fan.

sln

Dutch fuiks think they're so clever and sarcastic and appear not to see
what's funny about me posting as the idiot in chief.

They don't teach perl in continuing education classes around here. So it
is that I rely on a book and the net, and you've managed to **** up what
you could of that.

It's true I was a Reagan Youth when I was a European. With Bush II, we now
see Reagan's folly.
--
George

A dictatorship would be a heck of a lot easier, there's no question about
it.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
T

Tim Greer

George said:
Dutch fuiks think they're so clever and sarcastic and appear not to
see what's funny about me posting as the idiot in chief.

They don't teach perl in continuing education classes around here. So
it is that I rely on a book and the net, and you've managed to **** up
what you could of that.

It's true I was a Reagan Youth when I was a European. With Bush II,
we now see Reagan's folly.

A lot of people on this news group have filtered out the "sln" poster,
you might consider doing the same and saving the grief of dealing with
him. Not only does he argue with people when he doesn't get humor, but
he argues with himself as well. Filtering him out will save a lot of
nonsense.
 
G

George

A lot of people on this news group have filtered out the "sln" poster,
you might consider doing the same and saving the grief of dealing with
him. Not only does he argue with people when he doesn't get humor, but
he argues with himself as well. Filtering him out will save a lot of
nonsense.

I don't hang around long enough to foster antipathies except to the worst
of persons who violate civility. sln did give a working perl soln to my
bits/bytes problem, which I didn't ask for--actually I did but didn't mean
to--but was glad to have a solution in a another syntax. I keep on pecking
away at perl; one of these days I'll be able to use it skillfully.

If $from_offset is defined so:

# Which XOVER fields contain Subject: and From:?
my $count = 0;
my %xover_fmt = map( ($_, $count++), @{ $nntp->overview_fmt or die} );
die unless exists $xover_fmt{'Subject:'};
my $subject_offset = $xover_fmt{'Subject:'};
my $from_offset = $xover_fmt{'From:'};


, and this works:

my $s4 = join( map ($_->[$from_offset],@xover));

print STDOUT " s4 is $s4\n";

, why doesn't this:

if ($_->[$from_offset] eq "George <[email protected]>")
{
print STDOUT "got a match\n";
}

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

--
George

I believe that God has planted in every heart the desire to live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
T

Tim Greer

George said:
, why doesn't this:

if ($_->[$from_offset] eq "George <[email protected]>")
{
print STDOUT "got a match\n";
}

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

I think you meant "George <george\@example.invalid>" or else 'George
<[email protected]>', otherwise @example will mean something else.
 
J

Jürgen Exner

George said:
if ($_->[$from_offset] eq "George <[email protected]>")

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

Well, what value does the array @example have?
Had you used warnings and strict as has been strongly recommended over
and over again then perl would (probably) have warned you at compile
time that @example was undefined.

jue
 
J

Jürgen Exner

Jürgen Exner said:
George said:
if ($_->[$from_offset] eq "George <[email protected]>")

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

Well, what value does the array @example have?
Had you used warnings and strict as has been strongly recommended over
and over again then perl would (probably) have warned you at compile
time that @example was undefined.

s/undefined/undeclared/

of course

jue
 
G

George

if ($_->[$from_offset] eq "George <[email protected]>")


Execute this Perl program:

$_ = "George <[email protected]>";
print;

and then use single quotes instead.

:)

ok


C:\MinGW\source> perl tad1.pl
George <george.invalid>
C:\MinGW\source> perl tad1.pl
George <[email protected]>
C:\MinGW\source>

Clearly, I want single quotes. What happened as a side effect from the
double-quoted one?

One thing that happens in perl that doesn't happen in fortran is that
you're halfway through the program, and a statement like

....($something, @arts)= ...

creates an array. This would be strictly forbidden in fortran.
--
George

The deliberate and deadly attacks which were carried out yesterday against
our country were more than acts of terror. They were acts of war.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

George said:
, why doesn't this:

if ($_->[$from_offset] eq "George <[email protected]>")
{
print STDOUT "got a match\n";
}

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

I think you meant "George <george\@example.invalid>" or else 'George
<[email protected]>', otherwise @example will mean something else.

That's one side of the comparison. I see that this should be
single-quoted.

The stumper is how to use $_-> and $from_offset to get the from attribute
from a given message.

I swear, control mechanisms in perl use stealth.
--
George

The United States of America will never be intimidated by thugs and
assassins. The killers will fail, and the Iraqi people will live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Jürgen Exner said:
George said:
if ($_->[$from_offset] eq "George <[email protected]>")

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

Well, what value does the array @example have?
Had you used warnings and strict as has been strongly recommended over
and over again then perl would (probably) have warned you at compile
time that @example was undefined.

s/undefined/undeclared/

of course

jue

I don't see anything different after having added
use warnings;

and single quotes:


C:\MinGW\source>perl script2.pl
s4 is
Use of uninitialized value in string eq at script2.pl line 58.

C:\MinGW\source>perl script2.pl
s4 is
Use of uninitialized value in string eq at script2.pl line 59.

C:\MinGW\source>

#!/usr/bin/perl -w

use strict;
use warnings;
use Net::NNTP ();

use constant NUMBER_OF_ARTICLES => 10;
use constant GROUP_NAME => 'comp.lang.perl.misc';
use constant SERVER_NAME => 'news.individual.net';
use constant NNTP_DEBUG => 0;

my $nntp = Net::NNTP->new(SERVER_NAME, 'Debug' => NNTP_DEBUG) or die;
my $USER = '';
my $PASS = '';

$nntp->authinfo($USER,$PASS) or die $!;


my($article_count, $first_article, $last_article) =
$nntp->group(GROUP_NAME) or die;


# Which XOVER fields contain Subject: and From:?
my $count = 0;
my %xover_fmt = map( ($_, $count++), @{ $nntp->overview_fmt or die} );
die unless exists $xover_fmt{'Subject:'};
my $subject_offset = $xover_fmt{'Subject:'};
my $from_offset = $xover_fmt{'From:'};

my(@xover, $start_article);
RETRIEVE: while ($#xover+1 < NUMBER_OF_ARTICLES and $last_article >=
$first_article) {

# How many articles do we need? Stop retrieving if we have enough
my $articles_required = NUMBER_OF_ARTICLES - ($#xover+1) or last
RETRIEVE;


# Fetch overview information for the articles
$start_article = $last_article - ($articles_required-1);
$start_article = $start_article > $first_article ? $start_article :
$first_article;

my $xover_query = $start_article == $last_article ?
$start_article :
[$start_article, $last_article];
my $xover_ref = $nntp->xover($xover_query) or die;

# Store headers for the articles we've retrieved
foreach (sort {$b <=> $a} keys %$xover_ref) {
push @xover, $xover_ref->{$_};
}
} continue {
# Move the pointer forward to fetch previous articles
$last_article = $start_article - 1;
}


my $s4 = join( map ($_->[$from_offset],@xover));

print STDOUT " s4 is $s4\n";

if ($_->[$from_offset] eq 'George <[email protected]>')
{
print STDOUT "got a match\n";
}




$nntp->article($_,\*STDOUT) for @xover;

# Disconnect from the NNTP server
$nntp->quit;


# perl script2.pl



--
George

Now, there are some who would like to rewrite history - revisionist
historians is what I like to call them.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
T

Tad J McClellan

George said:
if ($_->[$from_offset] eq "George <[email protected]>")


Execute this Perl program:

$_ = "George <[email protected]>";
print;

and then use single quotes instead.


C:\MinGW\source> perl tad1.pl
George <george.invalid>
C:\MinGW\source> perl tad1.pl
George <[email protected]>
C:\MinGW\source>

Clearly, I want single quotes. What happened as a side effect from the
double-quoted one?


It interpolated the @example array.

Since it was empty, it was replaced with the empty string.

You should always enable warnings when developing Perl code.

perl -we '$_ = "George <[email protected]>"; print'

Possible unintended interpolation of @example in string at -e line 1.
Name "main::example" used only once: possible typo at -e line 1.

One thing that happens in perl that doesn't happen in fortran is that
you're halfway through the program, and a statement like

...($something, @arts)= ...

creates an array. This would be strictly forbidden in fortran.
^^^^^^
^^^^^^

You should also always enable strictures in Perl programs.

If you do, then such things are strictly forbidden in Perl too.

use warnings;
use strict;
 
T

Tad J McClellan

George said:
Jürgen Exner said:
if ($_->[$from_offset] eq "George <[email protected]>")

perl.exe says:

Use of uninitialized value in string eq at script2.pl line 58.

where line 58 is the test condition on the if control.

Well, what value does the array @example have?


The more pertinent question is what value does $_ have...

if ($_->[$from_offset] eq 'George <[email protected]>')
{
print STDOUT "got a match\n";
}


You have not put anything into $_.

Perhaps you meant to loop over @xover instead?

foreach ( @xover ) {
if ($_->[$from_offset] eq 'George <[email protected]>') {
print STDOUT "got a match\n";
}
}
 
P

Peter J. Holzer

One thing that happens in perl that doesn't happen in fortran is that
you're halfway through the program, and a statement like

...($something, @arts)= ...

creates an array. This would be strictly forbidden in fortran.

As Tad wrote, if you "use strict", you have to declare your variables.
But being able to create your values while you are "halfway through the
program" is a feature, not a bug. If you variables for the smallest
possible scope (possibly just a few lines) it is much easier to see
where the variable is used for. If you have to declare all your
variables at the start of your program, you need to examine your whole
program to see where the variable is used.

hp
 
G

George

The more pertinent question is what value does $_ have...

It seems to do a lot of things in perl.
if ($_->[$from_offset] eq 'George <[email protected]>')
{
print STDOUT "got a match\n";
}


You have not put anything into $_.

Perhaps you meant to loop over @xover instead?

foreach ( @xover ) {
if ($_->[$from_offset] eq 'George <[email protected]>') {
print STDOUT "got a match\n";
}
}

That's precisely what I needed to do.

C:\MinGW\source>perl script2.pl
got a match
got a match
got a match

C:\MinGW\source>

Thanks all for responses.
--
George

You teach a child to read, and he or her will be able to pass a literacy
test.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

As Tad wrote, if you "use strict", you have to declare your variables.
But being able to create your values while you are "halfway through the
program" is a feature, not a bug. If you variables for the smallest
possible scope (possibly just a few lines) it is much easier to see
where the variable is used for. If you have to declare all your
variables at the start of your program, you need to examine your whole
program to see where the variable is used.

hp

Fortran is a much different syntax than perl. Rightly or wrongly, I think
of perl as a fixit syntax. The policy on when you can declare an array,
while different, is a feature for both.
--
George

I believe that God has planted in every heart the desire to live in
freedom.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

George said:
if ($_->[$from_offset] eq "George <[email protected]>")


Execute this Perl program:

$_ = "George <[email protected]>";
print;

and then use single quotes instead.


( or, escape the at-sign: $_ = "George <george\@example.invalid>" )
ok
C:\MinGW\source> perl tad1.pl
George <george.invalid>
C:\MinGW\source> perl tad1.pl
George <[email protected]>
C:\MinGW\source>

Clearly, I want single quotes. What happened as a side effect from the
double-quoted one?


It interpolated the @example array.

Since it was empty, it was replaced with the empty string.

You should always enable warnings when developing Perl code.

perl -we '$_ = "George <[email protected]>"; print'

Possible unintended interpolation of @example in string at -e line 1.
Name "main::example" used only once: possible typo at -e line 1.

#!/usr/bin/perl -w

use strict;
use warnings;
use Net::NNTP ();

Does this not cover my bases here?
^^^^^^
^^^^^^

You should also always enable strictures in Perl programs.

If you do, then such things are strictly forbidden in Perl too.

use warnings;
use strict;

I've been thinking about the fix you gave with:
foreach ( @xover ) {

... send something to STDOUT ...

}
}

How is it different from this:

$nntp->article($_,\*STDOUT) for @xover;

?
--
George

We don't believe in planners and deciders making the decisions on behalf of
Americans.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top