a simple control in an nntp client

T

Tad J McClellan

George said:
It seems to do a lot of things in perl.


Not in my Perl programs.

$_ is simply the common default variable.

My programs never use the default, I always give them an explicit variable.

In my production code, I use $_ only in map() and grep().

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";
}
}


So in my code, that would instead be:

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

Look Ma! No $_!

That's precisely what I needed to do.


Then the problem was not with Perl being hard to understand at all.

The problem was simply a programmer's logic error.
 
T

Tad J McClellan

George said:
#!/usr/bin/perl -w

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

Does this not cover my bases here?


It would if you removed the -w switch.

I don't even know what happens when *both* forms of warnings are enabled...
 
G

George

[reordered]
That's precisely what I needed to do.


Then the problem was not with Perl being hard to understand at all.

The problem was simply a programmer's logic error.

No. I bought the camel book 15 months ago and am not mature with the
syntax.
Not in my Perl programs.

$_ is simply the common default variable.

My programs never use the default, I always give them an explicit variable.

In my production code, I use $_ only in map() and grep().
....
So in my code, that would instead be:

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

Look Ma! No $_!

#!/usr/bin/perl

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

use constant NUMBER_OF_ARTICLES => 100;
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;
}


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

foreach my $counter ( @xover ) {
if ($counter->[$from_offset] eq 'George <[email protected]>')
{
print STDOUT "'Look Ma! No \$_!'\n";
}
}



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

# perl script3.pl

C:\MinGW\source>perl script3.pl
got a match
got a match
got a match
got a match
got a match
got a match
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'
'Look Ma! No $_!'

C:\MinGW\source>

Thanks, Tad. Please never accuse me of having an inability to learn.
--
George

This young century will be liberty's century.
George W. Bush

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

Tad J McClellan

George said:
[reordered]
That's precisely what I needed to do.


Then the problem was not with Perl being hard to understand at all.

The problem was simply a programmer's logic error.

No. I bought the camel book 15 months ago and am not mature with the
syntax.


Your problem was not caused by syntax.

It was caused by semantics (a missing loop).
 
J

Jürgen Exner

George said:
I never understood the shebang line on windows.

It is ignored except that perl (but not the command shell!) still reads
and interprets options.

jue
 
J

Jürgen Exner

George said:
No. I bought the camel book 15 months ago and am not mature with the
syntax.

$_ has _NOTHING_ to do with syntax. It is simply a normal variable,
which happens to be used as default in many Perl commands if you don't
explicitely specify a different variable. But as mentioned by Tad
already that is semantic, not syntax.

jue
 
H

Hans Mulder

Jürgen Exner said:
It is ignored except that perl (but not the command shell!) still reads
and interprets options.

Are you sure?

Last time I payed attention, perl would also read the path and try
to run the program indicated. So if your script begins with:

#!/bin/sh

, then perl will try to exec /bin/sh (which is unlikely to succeed
on windows). Perl doesn't do this if the string "perl" occurs
anywhere on the shebang line.

At least, this is what my copy of perlrun says. I do not have a
Windows machine at hand to try it.

-- HansM
 
A

A. Sinan Unur

G

George

Are you sure?

Last time I payed attention, perl would also read the path and try
to run the program indicated. So if your script begins with:

#!/bin/sh

, then perl will try to exec /bin/sh (which is unlikely to succeed
on windows). Perl doesn't do this if the string "perl" occurs
anywhere on the shebang line.

At least, this is what my copy of perlrun says. I do not have a
Windows machine at hand to try it.

-- HansM

Give me a test. I'll run it and get back to you.

I haven't discovered the circumstance where shebang is relevant.
--
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:
No. I bought the camel book 15 months ago and am not mature with the
syntax.

$_ has _NOTHING_ to do with syntax. It is simply a normal variable,
which happens to be used as default in many Perl commands if you don't
[explicitly] specify a different variable. But as mentioned by Tad
already that is semantic, not syntax.

jue

Perl is a programming syntax defined by a single implementation. $_ is a
syntactic element of perl. My ongoing difficulty--somewhat allayed by this
thread--is how $_ fits in with loops, which are semantic.

You can argue, reductionistically, that everything boils down to syntax, in
the same way that it boils down to ones and zeroes.

--
George

America must not ignore the threat gathering against us. Facing clear
evidence of peril, we cannot wait for the final proof, the smoking gun that
could come in the form of a mushroom cloud.
George W. Bush

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

Jürgen Exner

George said:
Perl is a programming syntax defined by a single implementation.

Hmmmm, no. Perl is a programming language which consists of grammar (aka
syntax), context rules, and semantic.
$_ is a syntactic element of perl.

A variable could be considered a syntactical element as a special case
of an expression. But that would be true for any variable, not just for
$_.
As far as the syntax, context, or even semantic is concerned $_ is just
a good old plain variable, nothing special about it at all. As I said
before, it just happens that many functions use $_ as the default if you
don't specify your own variable. But that has nothing to do with syntax.
My ongoing difficulty--somewhat allayed by this
thread--is how $_ fits in with loops, which are semantic.

Loops are not just semantic. They have syntax, context, and semantic
components, just like (almost) every other Perl langauge construct.
And again, $_ is just another variable. You could use $foo or $bar or
$mygreatprivatsecretvariable instead and nothing would change.
You can argue, reductionistically, that everything boils down to syntax, in
the same way that it boils down to ones and zeroes.

Actually I am arguing the opposite way: $_ has nothing to do with
syntax. Neither the tokenizer nor the parser cares if you you are using
$_ or $foo or $mygreatprivatsecretvariable, therefore $_ cannot be a
syntactical issue.

jue
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top