printing a subject line

printing a subject line

am 17.10.2007 04:36:51 von merl the perl

#!/usr/bin/perl

use strict;
use Net::NNTP ();

use constant NUMBER_OF_ARTICLES => 1;
use constant GROUP_NAME => 'comp.lang.perl.misc';
use constant SERVER_NAME => 'newsgroups.comcast.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;
# trouble is between here and the end
my $string1 = $_->[$subject_offset];

print join("\n", map ($_->[$subject_offset].' from

'.$_->[$from_offset], @xover)),"\n";
print $string1 ;
#end script begin comment
This script has tested ok on its ability to grab a usenet message. Now I
want to do soemthing with the subject, and I don't see where I go wrong. I
think I get the subject into my $string with:
my $string1 = $_->[$subject_offset];
Perl.exe says $string1 is unitialized in the ultimate print statement. What
gives?
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 17.10.2007 16:37:01 von Charlton Wilbur

>>>>> "WW" == Wade Ward writes:

WW> This script has tested ok on its ability to grab a usenet
WW> message. Now I want to do soemthing with the subject, and I
WW> don't see where I go wrong. I think I get the subject into my
WW> $string with: my $string1 = $_->[$subject_offset]; Perl.exe
WW> says $string1 is unitialized in the ultimate print statement.
WW> What gives?

I recommend running it under the debugger to find out. In particular,
you will be able to determine precisely whether you get the subject
into $string1 (which probably ought to be called, you know, $subject).

perldoc perldebtut

And consider use warnings; as well -- you're probably doing some
warnings-appropriate things there, but I'm not going to go through the
code to point them out when perl can do it automatically.

Charlton





--
Charlton Wilbur
cwilbur@chromatico.net

Re: printing a subject line

am 17.10.2007 23:20:16 von Charlton Wilbur

>>>>> "WW" == Wade Ward writes:

WW> Thanks for your reply and what a fantastic tool! That
WW> tutorial brought me about 75% there on this problem. With the
WW> script as: [snip]
WW> , how do I use the debugger to see the value of $subject?

My pager indicates that the section of perldoc perldebtut that covers
the use of the debugger is approximately 20% into the document.

Charlton





--
Charlton Wilbur
cwilbur@chromatico.net

Re: printing a subject line

am 18.10.2007 00:01:24 von merl the perl

"Charlton Wilbur" wrote in message
news:87tzopkdte.fsf@mithril.chromatico.net...
>>>>>> "WW" == Wade Ward writes:
>
> WW> This script has tested ok on its ability to grab a usenet
> WW> message. Now I want to do soemthing with the subject, and I
> WW> don't see where I go wrong. I think I get the subject into my
> WW> $string with: my $string1 = $_->[$subject_offset]; Perl.exe
> WW> says $string1 is unitialized in the ultimate print statement.
> WW> What gives?
>
> I recommend running it under the debugger to find out. In particular,
> you will be able to determine precisely whether you get the subject
> into $string1 (which probably ought to be called, you know, $subject).
Thanks for your reply and what a fantastic tool! That tutorial brought me
about 75% there on this problem. With the script as:
#!/usr/bin/perl

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

use constant NUMBER_OF_ARTICLES => 1;
use constant GROUP_NAME => 'comp.lang.perl.misc';
use constant SERVER_NAME => 'newsgroups.comcast.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;

my $subject = $_->[$subject_offset];

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

, how do I use the debugger to see the value of $subject?
http://www.zaxfuuq.net/perl10.htm
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 18.10.2007 01:09:11 von Michele Dondi

On Wed, 17 Oct 2007 15:01:24 -0700, "Wade Ward"
wrote:

># Disconnect from the NNTP server
>$nntp->quit;
>
>my $subject = $_->[$subject_offset];

Due to severe lack of time, I didn't read the whole code, but that
looks suspiscious: what is $_ at that point?!? Why should it contain
an arrayref? Suspect: didn't you by any chance think you're in a
C loop or other construct creating an alias to $_, when in fact
you're not?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: printing a subject line

am 18.10.2007 02:25:11 von merl the perl

"Michele Dondi" wrote in message
news:la5dh3t7qnrv56d4rl9to2mmuvrngphrqd@4ax.com...
> On Wed, 17 Oct 2007 15:01:24 -0700, "Wade Ward"
> wrote:
>
>># Disconnect from the NNTP server
>>$nntp->quit;
>>
>>my $subject = $_->[$subject_offset];
>
> Due to severe lack of time, I didn't read the whole code, but that
> looks suspiscious: what is $_ at that point?!? Why should it contain
> an arrayref? Suspect: didn't you by any chance think you're in a
> C loop or other construct creating an alias to $_, when in fact
> you're not?
On the one hand, a background in C is good for learning perl, OTOH, not so
much.

I kept on whittling down the output until I got what I wanted. The debugger
was a huge help. When I got nothing for
my $subject =$_->[$subject_offset]
, when I typed p $subject1 in the debugger, then I was in hot pursuit of a
soln.
The end of the script now looks like:

my $subject1 = "tja\n";
my $s2 = join("\n", map ($_->[$subject_offset].'f
'.$_->[$from_offset],@xover)),"\n";
my $s3 = join("\n", map ($_->[$subject_offset].'f ',@xover)),"\n";
my $s4 = join("\n", map ($_->[$subject_offset],@xover)),"\n";

print join("\n", map ($_->[$subject_offset].' from '.$_->[$from_offset],
@xover)),"\n";
print $subject1;
print STDOUT " s2 is $s2\n";
print STDOUT " s3 is $s3\n";
print STDOUT " s4 is $s4\n";
__END__
, and the output is:
Re: printing a subject line from Michele Dondi
tja
s2 is Re: printing a subject linef Michele Dondi
s3 is Re: printing a subject linef
s4 is Re: printing a subject line

^^^^
|
|
success

Thx Charlton & Michele for help. A couple questions:
Q1) What is map doing here:
my $s2 = join("\n", map ($_->[$subject_offset].'f
'.$_->[$from_offset],@xover)),"\n";
Q2) What is the first newline doing in the join here:
my $s4 = join("\n", map ($_->[$subject_offset],@xover)),"\n";
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 18.10.2007 02:40:40 von merl the perl

"Michele Dondi" wrote in message
news:la5dh3t7qnrv56d4rl9to2mmuvrngphrqd@4ax.com...
> On Wed, 17 Oct 2007 15:01:24 -0700, "Wade Ward"

> Due to severe lack of time, I didn't read the whole code, but that
> looks suspiscious: what is $_ at that point?!? Why should it contain
> an arrayref? Suspect: didn't you by any chance think you're in a
> C loop or other construct creating an alias to $_, when in fact
> you're not?
Oh, and since you mention C and presumably know more Italian than I, can you
help me translate this:

Wade Ward ha scritto:
> [repost]
> [snipped from elsewhere]
>
>
>>> I can't see how to cast an integer to a pointer.
>>>
>
>
>> You can't. Fortran doesn't do that - not even with the C interop stuff.
>> Of course,you can always play around with TRANSFER, but recall that the
>> standard says that the resulting values are undefined when you type
>> cheat with TRANSFER. And you can do it in C.
>>
>
> You can't do it portably in C, either. I believe you can memcpy() in
> to an (unsigned char *) and back again. If sizeof(int) >= sizeof(void*)
> you might be able to do the cast, but the use of the value of the
> int is non-portable. Those writing large model x86 code, and
> who try to do such casts, know all about this one.
>
>
>>> cptr = C_PTR(p+1)
>>>
>
>
>> I suppose you are trying to use a structure constructor here. You can't
>> do that with C_PTR as it has (intentionally) private components. The
>> whole point of private components is to keep you from fiddling with the
>> innards. You can't write a structure constructor for a private component
>> - you don't even know what the components are. Now maybe you know what a
>> C pointer better look like inside, but accordingh to the Fortran
>> compiler, you don't know.
>>
>
> From K&R2 (close, but not exactly, the C89 standard) A6.6:
>
> "Certain other conversions involving pointers and integers are
> permitted, but have implementation defined aspects. They must
> be specified by an explicit type-conversion operator, or cast."
>
Ovvero, per esempio, se
void* pippo;
long pluto;

Puoi assegnare pluto a pippo, ma con un recast di pluto:
pippo=(void*)pluto;
> "A pointer may be converted to an integral type large enough to
> hold it; the required size is implementation-dependent. The
> mapping function is also implementation dependent."
>
Ovvero, puoi assegnare il valore di un pointer ad una variabile
pluto=(long)pippo;

ma la variabile deve potere contenere il valore del pointer: se il
pointer e' un 32bit, occore
usare un tipo da 32 bit almeno... questo dipende dall' architettura del
processore.
Su un processore a 16bit, con 16bit di indirizzamento, un pointer e' un
16bit e basta un int.
Su processore a 32bit con 32bit di indirizzo ci vuole un tipo da 32bit
(di solito ma non
necessariamente, un long: basterebbe un int ma su alcuni 32bit, gli int
possono essere a 16bit..)

> "An object of integral type may be explicitly converted to a
> pointer. The mapping always carries a sufficiently wide integer
> converted from a pointer back to the same pointer, but is
> otherwise implementation-dependent."
>
>
Vedi sopra; Puoi assegnare pluto a pippo, ma con un recast e pluto deve
avere la stessa dimensione
fisica di pippo (se pippo e' 32bit, pluto deve essere almeno da 32bit,
salvo casi particolari ;-) )
pippo=(void*)pluto;
>
Può qualcuno che conosca questo soggetto commentare il suddetto?


A questo uno potrebbe chiedersi il perche' di tutte queste cose....
In realta' lo scambio interi/pointers ha solo alcune applicazioni
abbastanza particolari e non dovrebbe essere abusato.
Quella che mi viene in mente e' la gestione di hardware, con mappe
di indirizzi espresse in valori interi che poi vengono assegnate
a puntatori...
Ad ogni modo, queste operazioni sono molto legare al tipo di architettura
della CPU, alla dimensione dei tipi e quindi *poco* portabili, per cui.....

wade@zaxfuuq.net .
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 18.10.2007 12:08:59 von Michele Dondi

On Wed, 17 Oct 2007 17:25:11 -0700, "Wade Ward"
wrote:

>> Due to severe lack of time, I didn't read the whole code, but that
>> looks suspiscious: what is $_ at that point?!? Why should it contain
>> an arrayref? Suspect: didn't you by any chance think you're in a
>> C loop or other construct creating an alias to $_, when in fact
>> you're not?
>On the one hand, a background in C is good for learning perl, OTOH, not so
>much.

Huh?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?!?

>I kept on whittling down the output until I got what I wanted. The debugger

I wouldn't regard that as a very good way of proceeding, where a
better one would be to have some idea of what one is doing, but if you
like that...


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: printing a subject line

am 18.10.2007 12:19:04 von Michele Dondi

On Wed, 17 Oct 2007 17:40:40 -0700, "Wade Ward"
wrote:

>> Due to severe lack of time, I didn't read the whole code, but that
>> looks suspiscious: what is $_ at that point?!? Why should it contain
>> an arrayref? Suspect: didn't you by any chance think you're in a
>> C loop or other construct creating an alias to $_, when in fact
>> you're not?
>Oh, and since you mention C and presumably know more Italian than I, can you
>help me translate this:

Huh?!? (Again) Where in hell did I ever mention C at all?!?

Re the translation: no, it's way too OT here. Write to me by email and
I may be so gentle as to translate that for you.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: printing a subject line

am 18.10.2007 20:25:22 von pacman

In article <7kceh35npon4kgm38t5hd4h7dsuch8qko7@4ax.com>,
Michele Dondi wrote:
>On Wed, 17 Oct 2007 17:40:40 -0700, "Wade Ward"
>wrote:
>
>>> Due to severe lack of time, I didn't read the whole code, but that
>>> looks suspiscious: what is $_ at that point?!? Why should it contain
>>> an arrayref? Suspect: didn't you by any chance think you're in a
>>> C loop or other construct creating an alias to $_, when in fact
>>> you're not?
>>Oh, and since you mention C and presumably know more Italian than I, can you
>>help me translate this:
>
>Huh?!? (Again) Where in hell did I ever mention C at all?!?

This part right here:

>>> C loop

could very easily be interpreted as making a comparison to a C-language-style
for loop by someone who has never seen POD source before, or didn't expect to
see it in a post that otherwise contained only ordinary English text with
some small perly bits.

Putting undeclared POD markup in a Usenet message seems about as wise as
putting in other kinds of undeclared markup.

Although you might wish that every perl programmer document every program and
every subroutine with inline POD, they don't. (In some cases, we're lucky if
they \fIread\fR any documentation, let alone write it.)

--
Alan Curry
pacman@world.std.com

Re: printing a subject line

am 18.10.2007 21:14:04 von merl the perl

"Michele Dondi" wrote in message
news:7kceh35npon4kgm38t5hd4h7dsuch8qko7@4ax.com...
> On Wed, 17 Oct 2007 17:40:40 -0700, "Wade Ward"
> wrote:

> Re the translation: no, it's way too OT here. Write to me by email and
> I may be so gentle as to translate that for you.

A message (from mpjensen7@comcast.net) was received at 18 Oct 2007 18:07:00
+0000.

The following addresses had delivery problems:


Permanent Failure:
550_RCPT_TO:_Mailbox_disk_quota_exceeded
Delivery last attempted at Thu, 18 Oct 2007 18:07:02 -0000

Che cosa è il vostro indirizzo?
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 18.10.2007 21:41:06 von Michele Dondi

On Thu, 18 Oct 2007 12:14:04 -0700, "Wade Ward"
wrote:

>> Re the translation: no, it's way too OT here. Write to me by email and
>> I may be so gentle as to translate that for you.
>
>A message (from mpjensen7@comcast.net) was received at 18 Oct 2007 18:07:00
>+0000.
>
>The following addresses had delivery problems:
>
>
>Permanent Failure:
>550_RCPT_TO:_Mailbox_disk_quota_exceeded
>Delivery last attempted at Thu, 18 Oct 2007 18:07:02 -0000
>
>Che cosa è il vostro indirizzo?

I already gave you my real address and you did already write to me
directly.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: printing a subject line

am 18.10.2007 21:48:11 von Michele Dondi

On Thu, 18 Oct 2007 18:25:22 +0000 (UTC), pacman@TheWorld.com (Alan
Curry) wrote:

>This part right here:
>
>>>> C loop
>
>could very easily be interpreted as making a comparison to a C-language-style
>for loop by someone who has never seen POD source before, or didn't expect to
>see it in a post that otherwise contained only ordinary English text with
>some small perly bits.
>
>Putting undeclared POD markup in a Usenet message seems about as wise as
>putting in other kinds of undeclared markup.
>
>Although you might wish that every perl programmer document every program and
>every subroutine with inline POD, they don't. (In some cases, we're lucky if
>they \fIread\fR any documentation, let alone write it.)

D'Oh! I hadn't thought of that. Unfortunately C is not
unambiguous as other keywords and one easily want a special markup for
it, for which POD seems perfect. OTOH I hate when people writes and
all uppercase FOR instead, since that is simply *not* a keyword
itself.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: printing a subject line

am 18.10.2007 22:20:01 von merl the perl

"Alan Curry" wrote in message
news:ff88ei$kgs$1@pcls6.std.com...
> In article <7kceh35npon4kgm38t5hd4h7dsuch8qko7@4ax.com>,
> Michele Dondi wrote:

>>Huh?!? (Again) Where in hell did I ever mention C at all?!?
>
> This part right here:
>
>>>> C loop
>
> could very easily be interpreted as making a comparison to a
> C-language-style
> for loop by someone who has never seen POD source before, or didn't expect
> to
> see it in a post that otherwise contained only ordinary English text with
> some small perly bits.
>
> Putting undeclared POD markup in a Usenet message seems about as wise as
> putting in other kinds of undeclared markup.
>
> Although you might wish that every perl programmer document every program
> and
> every subroutine with inline POD, they don't. (In some cases, we're lucky
> if
> they \fIread\fR any documentation, let alone write it.)
I have a real tough time figuring out the control structures in perl. I'm
used to control structures being explicit, either in fortran or C. My
inability to pluck things in and out of control structures is why I had to
grab only one article to extract the subject. The loops, consequently, were
of length one, and I could use the default thingamajig $_ .
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."

Re: printing a subject line

am 18.10.2007 22:42:39 von Sherm Pendley

pacman@TheWorld.com (Alan Curry) writes:

> Putting undeclared POD markup in a Usenet message seems about as wise as
> putting in other kinds of undeclared markup.
>
> Although you might wish that every perl programmer document every program and
> every subroutine with inline POD, they don't. (In some cases, we're lucky if
> they \fIread\fR any documentation, let alone write it.)

LOL!

Now I'm wondering how many folks will recognize undeclared roff markup. :-)

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Re: printing a subject line

am 19.10.2007 15:55:14 von Ted Zlatanov

On Wed, 17 Oct 2007 17:25:11 -0700 "Wade Ward" wrote:

WW> On the one hand, a background in C is good for learning perl, OTOH,
WW> not so much.

I bow to your indecisive wisdom, almost Zen-like in its emptiness.

Ted

Re: printing a subject line

am 20.10.2007 05:20:19 von merl the perl

"Ted Zlatanov" wrote in message
news:m27iljyzst.fsf@lifelogs.com...
> On Wed, 17 Oct 2007 17:25:11 -0700 "Wade Ward"
> wrote:
>
> WW> On the one hand, a background in C is good for learning perl, OTOH,
> WW> not so much.
>
> I bow to your indecisive wisdom, almost Zen-like in its emptiness.
Thank you, Joshu. How's your neck?
--
wade ward
"Nicht verzagen, Bruder Grinde fragen."