For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > November 2007 > Re: retrieving news messages









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: retrieving news messages
David Harmon

2007-11-29, 4:25 am

On Thu, 05 Jul 2007 12:25:20 -0700 in comp.lang.perl.misc, Joe Smith
<joe@inwap.com> wrote,
> my $nntp = Net::NNTP->new('newsgroups.comcast.net', { Debug => 1} );
> die "Unable to create NNTP object" unless $nntp;
>
>You have got to get those two statements working before proceeding
>any further.


It appears to me, that style of passing arguments to Net::NNTP->new
cannot work. I see this code in Net/NNTP.pm:

sub new
{
my $self = shift;
my $type = ref($self) || $self;
my ($host,%arg);
if (@_ % 2) {
$host = shift ;
%arg = @_;
} else {
%arg = @_;
$host=delete $arg{Host};
}


The above looks wrong to me; I think it ought to be more like

if (not (@_ % 2)) {

However, I'm still something of a Perl newbie, and am not quite sure
of all that I am looking at here. It appears that the constructor
fails to pick up the Host argument, but then it goes on to look for
the NNTPSERVER environment variable and use that, as documented.
I guess that everyone who got it to work had some such luck.

What do you all think?
Gerry Ford

2007-11-29, 8:05 am


"David Harmon" <source@netcom.com> wrote in message
news:13ksp20d0m9g2c5@corp.supernews.com...
> On Thu, 05 Jul 2007 12:25:20 -0700 in comp.lang.perl.misc, Joe Smith
> <joe@inwap.com> wrote,
>
> It appears to me, that style of passing arguments to Net::NNTP->new
> cannot work. I see this code in Net/NNTP.pm:
>
> sub new
> {
> my $self = shift;
> my $type = ref($self) || $self;
> my ($host,%arg);
> if (@_ % 2) {
> $host = shift ;
> %arg = @_;
> } else {
> %arg = @_;
> $host=delete $arg{Host};
> }
>
>
> The above looks wrong to me; I think it ought to be more like
>
> if (not (@_ % 2)) {
>
> However, I'm still something of a Perl newbie, and am not quite sure
> of all that I am looking at here. It appears that the constructor
> fails to pick up the Host argument, but then it goes on to look for
> the NNTPSERVER environment variable and use that, as documented.
> I guess that everyone who got it to work had some such luck.
>
> What do you all think?

Dude, they're dissing you.

Your constructor fails. They know why, and apparently, you don't.
--
Gerry Ford




----== Posted via Newsgroups.com - Usenet Access to over 100,000 Newsgroups ==----
Get Anonymous, Uncensored, Access to West and East Coast Server Farms at!
----== Highest Retention and Completion Rates! HTTP://WWW.NEWSGROUPS.COM ==----
Tad McClellan

2007-11-29, 8:05 am

David Harmon <source@netcom.com> wrote:
> On Thu, 05 Jul 2007 12:25:20 -0700 in comp.lang.perl.misc, Joe Smith
><joe@inwap.com> wrote,
>
> It appears to me, that style of passing arguments to Net::NNTP->new
> cannot work.



That is correct.

You are not calling it correctly.

The OPTIONS are "hash like", but you are passing a hash reference instead.


> I see this code in Net/NNTP.pm:
>
> sub new
> {
> my $self = shift;
> my $type = ref($self) || $self;
> my ($host,%arg);
> if (@_ % 2) {
> $host = shift ;
> %arg = @_;
> } else {
> %arg = @_;
> $host=delete $arg{Host};
> }
>
>
> The above looks wrong to me; I think it ought to be more like
>
> if (not (@_ % 2)) {



If that were right, then it ought to be the original condition
but with the if and else blocks swapped.

The if block will be evaluated if there are an odd number of arguments,
the else block will be evaluated if there are an even number of args.

> However, I'm still something of a Perl newbie, and am not quite sure
> of all that I am looking at here. It appears that the constructor
> fails to pick up the Host argument, but then it goes on to look for
> the NNTPSERVER environment variable and use that, as documented.
> I guess that everyone who got it to work had some such luck.
>
> What do you all think?



You are calling new() with an even number of arguments (two), a
string and a hash reference.

Fix your call by passing an odd number of arguments instead
by losing the curlies:

my $nntp = Net::NNTP->new('newsgroups.comcast.net', Debug => 1 );


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
A. Sinan Unur

2007-11-29, 8:05 am

David Harmon <source@netcom.com> wrote in
news:13ksp20d0m9g2c5@corp.supernews.com:

> On Thu, 05 Jul 2007 12:25:20 -0700 in comp.lang.perl.misc, Joe Smith
> <joe@inwap.com> wrote,

....
[color=darkred]
>
> It appears to me, that style of passing arguments to Net::NNTP->new
> cannot work. I see this code in Net/NNTP.pm:
>
> sub new
> {
> my $self = shift;
> my $type = ref($self) || $self;
> my ($host,%arg);
> if (@_ % 2) {
> $host = shift ;
> %arg = @_;
> } else {
> %arg = @_;
> $host=delete $arg{Host};
> }
>
>
> The above looks wrong to me; I think it ought to be more like
>
> if (not (@_ % 2)) {
>


I know nothing about Net::NNTP, however, you are wrong here:


sub new
{
my $self = shift;

# $self now contains the class name or the
# object reference on which new was called.
# The argument list is shortened by one element.

my $type = ref($self) || $self;

my ($host,%arg);
if (@_ % 2) {
$host = shift ;
%arg = @_;

# if the argument list now contains an odd number
# of elements, shift the first one in to $host.
# After that, there would be an even number of
# arguments left. Use them as key => value pairs
# to form an arguments hash.


} else {
%arg = @_;
$host=delete $arg{Host};

# If there are an even number of arguments to begin with,
# interpret them as key => value pairs to from the
# arguments hash. Then, put the 'Host' argument in
# $host, at the same time removing it from %arg so that
# subsequent code does not need special cases.

}

At this point, I do not know who posted the original code, but look at
it:

my $nntp = Net::NNTP->new('newsgroups.comcast.net', { Debug => 1} );

The constructor is wrong. You can see that by realizing that the code
above would produce

%args = ( 'newsgroups.comcast.net' => { Debug => 1} );

from that constructor. Either of the following would work:

my $nntp = Net::NNTP->new('newsgroups.comcast.net', Debug => 1 );

or

my $nntp = Net::NNTP->new(
Host => 'newsgroups.comcast.net',
Debug => 1,
);

The documentation for Net::NNTP states:

"OPTIONS are passed in a hash like fashion, using key and value pairs."

> I guess that everyone who got it to work had some such luck.


No, they read the documentation.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

Michele Dondi

2007-11-29, 7:08 pm

On Wed, 28 Nov 2007 23:03:22 -0800, David Harmon <source@netcom.com>
wrote:

>
>It appears to me, that style of passing arguments to Net::NNTP->new
>cannot work. I see this code in Net/NNTP.pm:


Yes, the issue was resolved some months ago. How gentle of you to turn
it up again...

>sub new
>{
> my $self = shift;
> my $type = ref($self) || $self;
> my ($host,%arg);
> if (@_ % 2) {
> $host = shift ;
> %arg = @_;
> } else {
> %arg = @_;
> $host=delete $arg{Host};
> }

[snip]
>The above looks wrong to me; I think it ought to be more like
>
> if (not (@_ % 2)) {


No, that's exactly what's intended. If you don't like what's intended,
then it's a wholly different problem. If you have a list with an odd
number of elements, then one is shifted off to be assigned to $host.
The remaining ones are interpreted as "named" parameters and put into
a hash. Otherwise, the parameters are assigned to a hash in the first
place.


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

2007-11-30, 4:20 am

On Thu, 29 Nov 2007 12:18:42 GMT in comp.lang.perl.misc, "A. Sinan
Unur" <1usa@llenroc.ude.invalid> wrote,
>Either of the following would work:
>
>my $nntp = Net::NNTP->new('newsgroups.comcast.net', Debug => 1 );
>
>or
>
>my $nntp = Net::NNTP->new(
> Host => 'newsgroups.comcast.net',
> Debug => 1,
> );


Thanks for the explanation.

Nobody in the original thread figured it out. The documentation
could be more clear, instead of requiring a look at the source to
make sense of it. Nothing to do with a hash after all, just pairs
of argument names and values; I think mentioning hash just
the original poster.
Michele Dondi

2007-11-30, 7:07 pm

On Thu, 29 Nov 2007 20:59:23 -0800, David Harmon <source@netcom.com>
wrote:

>
>Thanks for the explanation.
>
>Nobody in the original thread figured it out. The documentation


Somebody did figure it out.

>could be more clear, instead of requiring a look at the source to
>make sense of it. Nothing to do with a hash after all, just pairs


I gave a p into the source as well. The documentation is clear
enough. Somebody just mistakenly assumed that the options should have
been passed as a hashref, which is not the case: some modules do that,
but there's no general rule. I'm really blessing 6's signatures now!

>of argument names and values; I think mentioning hash just
>the original poster.


Who incidentally *appears* to be enough in his own right...


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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com