[24663] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6827 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 3 14:42:16 2004

Date: Tue, 3 Aug 2004 11:41:34 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 3 Aug 2004     Volume: 10 Number: 6827

Today's topics:
        The code below returns me nothing. <artgh@hotmail.com>
    Re: The code below returns me nothing. <gogala@sbcglobal.net>
    Re: The code below returns me nothing. <artgh@hotmail.com>
    Re: The code below returns me nothing. <uri@stemsystems.com>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
    Re: The code below returns me nothing. <gogala@sbcglobal.net>
    Re: The code below returns me nothing. <uri@stemsystems.com>
    Re: The code below returns me nothing. <gogala@sbcglobal.net>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
    Re: The code below returns me nothing. <nobull@mail.com>
    Re: The code below returns me nothing. <ebohlman@omsdev.com>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
    Re: The code below returns me nothing. <abigail@abigail.nl>
    Re: The code below returns me nothing. <uri@stemsystems.com>
    Re: The code below returns me nothing. <uri@stemsystems.com>
    Re: The code below returns me nothing. <eloelono1@sina.com>
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. (Anno Siegel)
    Re: The code below returns me nothing. <segraves_f13@mindspring.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 30 Jul 2004 12:52:35 +0800
From: Facco Eloelo <artgh@hotmail.com>
Subject: The code below returns me nothing.
Message-Id: <410bd340.95544605@news.individual.net>

I find NetAddr::IP is good.
But the code below returns me nothing.
can some body help me? thx.


#!/usr/bin/perl
use NetAddr::IP;

my $list_file  = 'IPlist.txt';
my $segment_file = 'IPsegment.txt';
my $match_file = 'match.txt';

open my $IPsegment,  '<', $segment_file  or die "Cannot open $in_file: $!";
open my $IPlist,  '<', $list_file  or die "Cannot open $in_file: $!";
open my $IPmatch, '>', $match_file or die "Cannot open $out_file: $!";

while ( <$IPsegment> ) {
     chomp;
	 $segment=$_;
     my $space = NetAddr::IP->new($segment);

      while ( <$IPlist> ) {
      chomp ($_);
      ($domain, $domip) = split(/,/, $_);
      for my $ip (map {NetAddr::IP->new($_) } $domip)
      {
        print my $IPmatch $domain,$ip, "\n"
        if $space->contains($ip);
      }
	  }
}


--
>I have a network segment list in a text file called IPsegment.txt,it looks like
>this:
>
>IPsegment.txt
>219.111.192.0/18
>68.132.0.0/17
>67.146.0.0/16
>192.162.0.0/16
>152.172.0.0/16
>34.132.0.0/14
>97.208.0.0/13
>
>
>And I have some IP addresses in another text file called IPlist.txt.It looks
>like this:
>
>IPlist.txt
>www.yahoo.com,66.94.230.51
>www.baidu.com,202.108.250.249
>www.sina.com.cn,61.135.152.77
>www.sohu.com,61.135.150.75
>...
>
>Now,I want to know whether the IP addresses in the IPlist.txt belongs the
>network segment writen in the IPsegment.txt.
>The mathing IP list is output in a new text file called matchinglist.txt.
>It looks like this:
>
>matchinglist.txt
>www.baidu.com,202.108.250.249
>www.sina.com.cn,61.135.152.77
>...
>
>
>How can I do that? Thank you in advance.



------------------------------

Date: Fri, 30 Jul 2004 01:59:26 -0400
From: Mladen Gogala <gogala@sbcglobal.net>
Subject: Re: The code below returns me nothing.
Message-Id: <pan.2004.07.30.05.59.25.476487@sbcglobal.net>

On Fri, 30 Jul 2004 12:52:35 +0800, Facco Eloelo wrote:

> ($domain, $domip) = split(/,/, $_);
>       for my $ip (map {NetAddr::IP->new($_) } $domip)

That "map" is incorrectly applied. Map works on lists and $domip is
a scalar variable, and consequently, map function doesn't do
anything. If you pushed your IP addresses into an array and applied 
map function to the array, it would work better. Here is what the fine
manual says:
       map BLOCK LIST
       map EXPR,LIST
               Evaluates the BLOCK or EXPR for each element of LIST (locally
               setting $_ to each element) and returns the list value composed
               of the results of each such evaluation.  In scalar context,
               returns the total number of elements so generated.  Evaluates
               BLOCK or EXPR in list context, so each element of LIST may pro-
               duce zero, one, or more elements in the returned value.
 

Notice the word "LIST", printed in capitals. Scalar is not a list.

-- 
A city is a large community where people are lonesome together.



------------------------------

Date: Fri, 30 Jul 2004 16:29:18 +0800
From: Facco Eloelo <artgh@hotmail.com>
Subject: Re: The code below returns me nothing.
Message-Id: <410a01bd.107444977@news.individual.net>

Still have some problem  :-(

#!/usr/bin/perl
use NetAddr::IP;
use warnings;

my $list_file  = 'IPlist.txt';
my $segment_file = 'IPsegment.txt';
my $match_file = 'match.txt';

open my $IPsegment,  '<', $segment_file  or die "Cannot open $segment_file: $!";
open my $IPlist,  '<', $list_file  or die "Cannot open $list_file: $!";
open my $IPmatch, '>', $match_file or die "Cannot open $match_file: $!";

@line=<$IPsegment>;
$num = @line;
for($i=0;$i<$num;$i++)
{
        my $space = NetAddr::IP->new($line[$i]);

        for my $ip (map { NetAddr::IP->new($_) } <$IPlist>)
        {
        print $ip, "\n"
        if $space->contains($ip);
        }
}
--
>On Fri, 30 Jul 2004 12:52:35 +0800, Facco Eloelo wrote:
>
>> ($domain, $domip) = split(/,/, $_);
>>       for my $ip (map {NetAddr::IP->new($_) } $domip)
>
>That "map" is incorrectly applied. Map works on lists and $domip is
>a scalar variable, and consequently, map function doesn't do
>anything. If you pushed your IP addresses into an array and applied 
>map function to the array, it would work better. Here is what the fine
>manual says:
>       map BLOCK LIST
>       map EXPR,LIST
>               Evaluates the BLOCK or EXPR for each element of LIST (locally
>               setting $_ to each element) and returns the list value composed
>               of the results of each such evaluation.  In scalar context,
>               returns the total number of elements so generated.  Evaluates
>               BLOCK or EXPR in list context, so each element of LIST may pro-
>               duce zero, one, or more elements in the returned value.
> 
>
>Notice the word "LIST", printed in capitals. Scalar is not a list.



------------------------------

Date: Fri, 30 Jul 2004 08:40:30 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The code below returns me nothing.
Message-Id: <x7isc5khw1.fsf@mail.sysarch.com>

>>>>> "MG" == Mladen Gogala <gogala@sbcglobal.net> writes:

  MG> On Fri, 30 Jul 2004 12:52:35 +0800, Facco Eloelo wrote:
  >> ($domain, $domip) = split(/,/, $_);
  >> for my $ip (map {NetAddr::IP->new($_) } $domip)

  MG> That "map" is incorrectly applied. Map works on lists and $domip is
  MG> a scalar variable, and consequently, map function doesn't do
  MG> anything. If you pushed your IP addresses into an array and applied 
  MG> map function to the array, it would work better. Here is what the fine
  MG> manual says:
  MG>        map BLOCK LIST
  MG>        map EXPR,LIST

  MG> Notice the word "LIST", printed in capitals. Scalar is not a list.

that is wrong. a single value is just a list with one element. there is
no problem with using map (nor any other func that takes a list) on a
single scalar. ever try push @foo, $bar? works fine and it too takes a
list as its second argument.

so the OP's problem lies elsewhere.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: 30 Jul 2004 09:01:55 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ced2q3$bvd$1@mamenchi.zrz.TU-Berlin.DE>

Mladen Gogala  <gogala@sbcglobal.net> wrote in comp.lang.perl.misc:
> On Fri, 30 Jul 2004 12:52:35 +0800, Facco Eloelo wrote:
> 
> > ($domain, $domip) = split(/,/, $_);
> >       for my $ip (map {NetAddr::IP->new($_) } $domip)
> 
> That "map" is incorrectly applied.

It doesn't make much sense, but the map application is valid.

>                                    Map works on lists and $domip is
> a scalar variable, and consequently, map function doesn't do
> anything.

Not true.  When the syntax requires a LIST, any mixture of scalars,
arrays (and even hashes) can be given.  They will all be flattened
into one list.

>           If you pushed your IP addresses into an array and applied 
> map function to the array, it would work better.

That's not necessary.  A special case of the above is

    map NetAddr::IP->new($_), $domip;

That will execute and return NetAddr::IP->new($domip).  "map" is
unnecessary, but it works.

>                                                  Here is what the fine
> manual says:



>        map BLOCK LIST
>        map EXPR,LIST
>                Evaluates the BLOCK or EXPR for each element of LIST (locally
>                setting $_ to each element) and returns the list value composed
>                of the results of each such evaluation.  In scalar context,
>                returns the total number of elements so generated.  Evaluates
>                BLOCK or EXPR in list context, so each element of LIST may pro-
>                duce zero, one, or more elements in the returned value.
>  
> 
> Notice the word "LIST", printed in capitals. Scalar is not a list.

Sometimes it is.

Anno


------------------------------

Date: 30 Jul 2004 09:10:59 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ced3b3$bvd$2@mamenchi.zrz.TU-Berlin.DE>

Facco Eloelo  <artgh@hotmail.com> wrote in comp.lang.perl.misc:
> I find NetAddr::IP is good.
> But the code below returns me nothing.
> can some body help me? thx.
> 
> 
> #!/usr/bin/perl
> use NetAddr::IP;
> 
> my $list_file  = 'IPlist.txt';
> my $segment_file = 'IPsegment.txt';
> my $match_file = 'match.txt';
> 
> open my $IPsegment,  '<', $segment_file  or die "Cannot open $in_file: $!";
> open my $IPlist,  '<', $list_file  or die "Cannot open $in_file: $!";
> open my $IPmatch, '>', $match_file or die "Cannot open $out_file: $!";
> 
> while ( <$IPsegment> ) {
>      chomp;
> 	 $segment=$_;
>      my $space = NetAddr::IP->new($segment);
> 
>       while ( <$IPlist> ) {

You are trying to read the same file repeatedly in a loop.  After the
first time through, the file will be positioned at eof and stay there.
You need to rewind the file (see perldoc -f seek).

A better solution would probably be to read $IPlist once and store
the contents in a data structure.  A hash keyed on $ip looks reasonable.

[snip]

Anno


------------------------------

Date: Fri, 30 Jul 2004 17:58:02 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <K_vOc.20420$iK.10897@newsread2.news.atl.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:ced3b3$bvd$2@mamenchi.zrz.TU-Berlin.DE...
> Facco Eloelo  <artgh@hotmail.com> wrote in comp.lang.perl.misc:
> > I find NetAddr::IP is good.
> > But the code below returns me nothing.
> > can some body help me? thx.
<snip>
> You are trying to read the same file repeatedly in a loop.  After the
> first time through, the file will be positioned at eof and stay there.
> You need to rewind the file (see perldoc -f seek).
>
> A better solution would probably be to read $IPlist once and store
> the contents in a data structure.  A hash keyed on $ip looks reasonable.
<snip>

FYI, the OP is trying to adapt the code example (which works fine here) for
"Matching against your address space" at
http://mipagina.cantv.net/lem/perl/iptut.htm to his particular problem.

Cheers.
--
Bill Segraves




------------------------------

Date: 30 Jul 2004 19:01:59 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <cee5v7$3m9$1@mamenchi.zrz.TU-Berlin.DE>

Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:ced3b3$bvd$2@mamenchi.zrz.TU-Berlin.DE...
> > Facco Eloelo  <artgh@hotmail.com> wrote in comp.lang.perl.misc:
> > > I find NetAddr::IP is good.
> > > But the code below returns me nothing.
> > > can some body help me? thx.
> <snip>
> > You are trying to read the same file repeatedly in a loop.  After the
> > first time through, the file will be positioned at eof and stay there.
> > You need to rewind the file (see perldoc -f seek).
> >
> > A better solution would probably be to read $IPlist once and store
> > the contents in a data structure.  A hash keyed on $ip looks reasonable.
> <snip>
> 
> FYI, the OP is trying to adapt the code example (which works fine here) for
> "Matching against your address space" at
> http://mipagina.cantv.net/lem/perl/iptut.htm to his particular problem.

It runs?  It contains a trivial but fatal error, twice.  After correction
it does work as expected.  It also made clearer to me what the purpose
of the code is.

So the OP is trying to match the same list of IP's (from some file,
that part is snipped) against a series of IP spaces (from another file).

So first read the list of addresses.  Assuming each line is ready for
consumption by NetAddr::IP (which may not be true):

    my @addrs;
    while ( <$addrs> ) {
        chomp;
        push @addrs, NetAddr::IP->new($_);
    }

Now read each range in sequence and check all addresses against each
one:

    while ( <$spaces> ) {
        chomp;
        my $space = NetAddr::IP->new( $_);
        for ( @addrs ) {
            if ( $space->contains( $_) ) {
                # report match
            }
        }
    }

The same method could be applied with the roles of $addrs and $spaces
reversed:  Read all spaces into a list, then apply them to each address
as it is read.  If the list of addresses is huge, but the list of spaces
isn't, that may be preferable.  Both methods report the same matches,
but in a different sequence.

Anno


------------------------------

Date: Fri, 30 Jul 2004 22:52:38 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <WiAOc.20672$iK.9523@newsread2.news.atl.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:cee5v7$3m9$1@mamenchi.zrz.TU-Berlin.DE...
> Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
<snip>
> > FYI, the OP is trying to adapt the code example (which works fine here)
for
> > "Matching against your address space" at
> > http://mipagina.cantv.net/lem/perl/iptut.htm to his particular problem.
>
> It runs?  It contains a trivial but fatal error, twice.  After correction
> it does work as expected.  It also made clearer to me what the purpose
> of the code is.
>

Hmm, Anno, I'm not sure what you mean by "It". I was referring to the code
at the above link, to which I had referred the OP. I just checked it again.
Runs fine here. If you are aware of errors in the example I cited at
http://mipagina.cantv.net/lem/perl/iptut.htm, perhaps you could point them
out, especially for the benefit of the OP.

> So the OP is trying to match the same list of IP's (from some file,
> that part is snipped) against a series of IP spaces (from another file).
>
> So first read the list of addresses.  Assuming each line is ready for
> consumption by NetAddr::IP (which may not be true):

Actually, not true. See the OP's original list of addresses, where the
addresses are presented as multiple instances of "FQDN,IP address", e.g.,

IPlist.txt
www.yahoo.com,66.94.230.51
www.baidu.com,202.108.250.249
www.sina.com.cn,61.135.152.77
www.sohu.com,61.135.150.75

>
>     my @addrs;
>     while ( <$addrs> ) {
>         chomp;
>         push @addrs, NetAddr::IP->new($_);
>     }
>
> Now read each range in sequence and check all addresses against each
> one:
>
>     while ( <$spaces> ) {
>         chomp;
>         my $space = NetAddr::IP->new( $_);
>         for ( @addrs ) {
>             if ( $space->contains( $_) ) {
>                 # report match
>             }
>         }
>     }
>
> The same method could be applied with the roles of $addrs and $spaces
> reversed:  Read all spaces into a list, then apply them to each address
> as it is read.  If the list of addresses is huge, but the list of spaces
> isn't, that may be preferable.  Both methods report the same matches,
> but in a different sequence.

IMO, looping through the spaces more than once should be avoided, as a
seemingly short list of spaces can indeed be huge, when expanded. OTOH, the
OP has given us nothing on the lengths (no. of lines) of the input files.

Cheers and thanks for helping the OP.

--
Bill Segraves








------------------------------

Date: Fri, 30 Jul 2004 21:30:56 -0400
From: Mladen Gogala <gogala@sbcglobal.net>
Subject: Re: The code below returns me nothing.
Message-Id: <pan.2004.07.31.01.30.56.118374@sbcglobal.net>

On Fri, 30 Jul 2004 08:40:30 +0000, Uri Guttman wrote:

> 
> so the OP's problem lies elsewhere.

I stand corrected. It is syntactically corect, but doesn't make
much sense.


-- 
A city is a large community where people are lonesome together.



------------------------------

Date: Sat, 31 Jul 2004 05:32:42 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The code below returns me nothing.
Message-Id: <x7fz78rbbp.fsf@mail.sysarch.com>

>>>>> "MG" == Mladen Gogala <gogala@sbcglobal.net> writes:

  MG> On Fri, 30 Jul 2004 08:40:30 +0000, Uri Guttman wrote:
  >> 
  >> so the OP's problem lies elsewhere.

  MG> I stand corrected. It is syntactically corect, but doesn't make
  MG> much sense.

passing a single scalar to map does make sense in some circumstances. i
use it to alias a scalar to $_ when i have code that must act on
$_. check out Sort::Maker for a case like that where i generate map code
on scalar values.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Sat, 31 Jul 2004 02:54:47 -0400
From: Mladen Gogala <gogala@sbcglobal.net>
Subject: Re: The code below returns me nothing.
Message-Id: <pan.2004.07.31.06.54.47.429414@sbcglobal.net>

On Sat, 31 Jul 2004 05:32:42 +0000, Uri Guttman wrote:

> passing a single scalar to map does make sense in some circumstances. i
> use it to alias a scalar to $_ when i have code that must act on
> $_. check out Sort::Maker for a case like that where i generate map code
> on scalar values.

Wow! Thanks Uri, you're really a master! I don't consider myself
a perl guru, as a matter of fact, I'm rather far from it, and I'm
grateful for anything that I can learn.

-- 
A city is a large community where people are lonesome together.



------------------------------

Date: 31 Jul 2004 06:57:25 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ceffsl$ovl$1@mamenchi.zrz.TU-Berlin.DE>

Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:cee5v7$3m9$1@mamenchi.zrz.TU-Berlin.DE...
> > Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> <snip>
> > > FYI, the OP is trying to adapt the code example (which works fine here)
> for
> > > "Matching against your address space" at
> > > http://mipagina.cantv.net/lem/perl/iptut.htm to his particular problem.
> >
> > It runs?  It contains a trivial but fatal error, twice.  After correction
> > it does work as expected.  It also made clearer to me what the purpose
> > of the code is.
> >
> 
> Hmm, Anno, I'm not sure what you mean by "It". I was referring to the code
> at the above link, to which I had referred the OP. I just checked it again.
> Runs fine here. If you are aware of errors in the example I cited at
> http://mipagina.cantv.net/lem/perl/iptut.htm, perhaps you could point them
> out, especially for the benefit of the OP.

Yes, I mean the code under "Matching against your address space" on
http://mipagina.cantv.net/lem/perl/iptut.htm.  It goes:

    use NetAddr::IP;

    my $space = new NetAddr::IP->new('10.128.0.0/17');

    for my $ip (map { new NetAddr::IP->new($_) } <DATA>)
    {
        print $ip, "\n"
            if $space->contains($ip);
    }

    __DATA__
    172.16.1.1
    172.16.1.5
    ...


There's a "new" too many in "new NetAddr::IP->new( ...);".  I suppose the
call was originally in indirect object syntax and was re-written to use
arrow notation.  Now it contains both :)

As I said, it's a trivial error and easily corrected.

Anno


------------------------------

Date: Sat, 31 Jul 2004 15:45:55 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <T8POc.3676$Jp6.534@newsread3.news.atl.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:ceffsl$ovl$1@mamenchi.zrz.TU-Berlin.DE...
<snip>
> Yes, I mean the code under "Matching against your address space" on
> http://mipagina.cantv.net/lem/perl/iptut.htm.  It goes:
>
>     use NetAddr::IP;
>
>     my $space = new NetAddr::IP->new('10.128.0.0/17');
>
>     for my $ip (map { new NetAddr::IP->new($_) } <DATA>)
>     {
>         print $ip, "\n"
>             if $space->contains($ip);
>     }
>
>     __DATA__
>     172.16.1.1
>     172.16.1.5
>     ...
>
>
> There's a "new" too many in "new NetAddr::IP->new( ...);".  I suppose the
> call was originally in indirect object syntax and was re-written to use
> arrow notation.  Now it contains both :)
>

You've a sharp eye, Anno. Thanks for the clarification. I'll point out the
error(s) to the author of NetAddr::IP.

> As I said, it's a trivial error and easily corrected.

I was a bit befuddled when you wrote (earlier that) the errors were "fatal",
as they certainly weren't fatal on this Win32 with (Indigo) Perl 5.6.1
workstation. That said (written), I've corrected the errors on my copy of
the script.

Best wishes.

--
Bill Segraves




------------------------------

Date: 31 Jul 2004 20:46:44 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: The code below returns me nothing.
Message-Id: <u9brhw9cyj.fsf@wcl-l.bham.ac.uk>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:

> Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> > "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> > news:cee5v7$3m9$1@mamenchi.zrz.TU-Berlin.DE...

> > > It runs?  It contains a trivial but fatal error, twice.
> > 
> > Hmm, Anno, I'm not sure what you mean by "It". I was referring to the code
> > at the above link, to which I had referred the OP. I just checked it again.
> > Runs fine here.
> 
> Yes, I mean the code [...]  It goes:
> 
>     my $space = new NetAddr::IP->new('10.128.0.0/17');
> 
> There's a "new" too many in "new NetAddr::IP->new( ...);".  I suppose the
> call was originally in indirect object syntax and was re-written to use
> arrow notation.  Now it contains both :)

But, of course, that's not fatal.  It first calls new() as a class
method and constructs an object, then it calls new() as an instance
method (probably meaningless).

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


------------------------------

Date: 31 Jul 2004 22:56:30 GMT
From: Eric Bohlman <ebohlman@omsdev.com>
Subject: Re: The code below returns me nothing.
Message-Id: <Xns9537B732B97F7ebohlmanomsdevcom@130.133.1.4>

Uri Guttman <uri@stemsystems.com> wrote in 
news:x7fz78rbbp.fsf@mail.sysarch.com:

> passing a single scalar to map does make sense in some circumstances. i
> use it to alias a scalar to $_ when i have code that must act on
> $_. check out Sort::Maker for a case like that where i generate map code
> on scalar values.

What advantage does that have over the more traditional use of for() for 
that purpose?


------------------------------

Date: 31 Jul 2004 23:13:33 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ceh92t$qck$1@mamenchi.zrz.TU-Berlin.DE>

Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:ceffsl$ovl$1@mamenchi.zrz.TU-Berlin.DE...
> <snip>
> > Yes, I mean the code under "Matching against your address space" on
> > http://mipagina.cantv.net/lem/perl/iptut.htm.  It goes:
> >
> >     use NetAddr::IP;
> >
> >     my $space = new NetAddr::IP->new('10.128.0.0/17');
> >
> >     for my $ip (map { new NetAddr::IP->new($_) } <DATA>)
> >     {
> >         print $ip, "\n"
> >             if $space->contains($ip);
> >     }
> >
> >     __DATA__
> >     172.16.1.1
> >     172.16.1.5
> >     ...
> >
> >
> > There's a "new" too many in "new NetAddr::IP->new( ...);".  I suppose the
> > call was originally in indirect object syntax and was re-written to use
> > arrow notation.  Now it contains both :)
> >
> 
> You've a sharp eye, Anno. Thanks for the clarification. I'll point out the
> error(s) to the author of NetAddr::IP.
> 
> > As I said, it's a trivial error and easily corrected.
> 
> I was a bit befuddled when you wrote (earlier that) the errors were "fatal",
> as they certainly weren't fatal on this Win32 with (Indigo) Perl 5.6.1
> workstation. That said (written), I've corrected the errors on my copy of
> the script.

It wasn't my sharp eye but a run time error that alerted me to the bug.

When I call "new NetAddr::IP->new('10.128.0.0/17')", I get to see "Can't
call method "new" on an undefined value...".  The call translates to
"'NetAddr::IP'->new->new('10.128.0.0/17')" (Deparse), and since new()
without arguments returns undef, I can't see how it would ever
work.  So what I see is a fatal error.

Anno


------------------------------

Date: 31 Jul 2004 23:17:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ceh99n$qck$2@mamenchi.zrz.TU-Berlin.DE>

Brian McCauley  <nobull@mail.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> 
> > Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> > > "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> > > news:cee5v7$3m9$1@mamenchi.zrz.TU-Berlin.DE...
> 
> > > > It runs?  It contains a trivial but fatal error, twice.
> > > 
> > > Hmm, Anno, I'm not sure what you mean by "It". I was referring to the code
> > > at the above link, to which I had referred the OP. I just checked it again.
> > > Runs fine here.
> > 
> > Yes, I mean the code [...]  It goes:
> > 
> >     my $space = new NetAddr::IP->new('10.128.0.0/17');
> > 
> > There's a "new" too many in "new NetAddr::IP->new( ...);".  I suppose the
> > call was originally in indirect object syntax and was re-written to use
> > arrow notation.  Now it contains both :)
> 
> But, of course, that's not fatal.  It first calls new() as a class
> method and constructs an object, then it calls new() as an instance
> method (probably meaningless).

But NetAddr::IP->new() returns undef (experimentally), so the object
method call doesn't happen.  It could work, but in the given case it
doesn't.

Anno


------------------------------

Date: Sat, 31 Jul 2004 23:31:02 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <WYVOc.21623$iK.1389@newsread2.news.atl.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:ceh92t$qck$1@mamenchi.zrz.TU-Berlin.DE...
<snip>
> It wasn't my sharp eye but a run time error that alerted me to the bug.
>
> When I call "new NetAddr::IP->new('10.128.0.0/17')", I get to see "Can't
> call method "new" on an undefined value...".  The call translates to
> "'NetAddr::IP'->new->new('10.128.0.0/17')" (Deparse), and since new()
> without arguments returns undef, I can't see how it would ever
> work.  So what I see is a fatal error.

Interesting! What OS and Perl versions are you running? I see you're using
trn for news; but I don't know the environment in which you experienced the
run-time errors. I'll run the same script that works fine on this Win32
workstation on the same workstation running Linux to see what *I* see, if
you're interested.

Regards,

--
Bill Segraves




------------------------------

Date: 31 Jul 2004 23:36:35 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <cehae3$r51$1@mamenchi.zrz.TU-Berlin.DE>

Eric Bohlman  <ebohlman@omsdev.com> wrote in comp.lang.perl.misc:
> Uri Guttman <uri@stemsystems.com> wrote in 
> news:x7fz78rbbp.fsf@mail.sysarch.com:
> 
> > passing a single scalar to map does make sense in some circumstances. i
> > use it to alias a scalar to $_ when i have code that must act on
> > $_. check out Sort::Maker for a case like that where i generate map code
> > on scalar values.
> 
> What advantage does that have over the more traditional use of for() for 
> that purpose?

I agree that one-shot "for" is more frequently seen, but map has
different properties that may be advantages.

One is the usual difference between map and for.  Map returns a value,
the statement in for must have a side effect.  That's the same, whether
you use the one-shot form or a real loop.

Another difference is that map isn't a statement modifier, so you
can combine it with variable declarations.

    my ( $y) = map ..., $x;

is fine, but this is disallowed:

    my $y = ... for $x;

Anno


------------------------------

Date: 1 Aug 2004 00:01:16 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <cehbsc$rr3$1@mamenchi.zrz.TU-Berlin.DE>

Bill Segraves <segraves_f13@mindspring.com> wrote in comp.lang.perl.misc:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:ceh92t$qck$1@mamenchi.zrz.TU-Berlin.DE...
> <snip>
> > It wasn't my sharp eye but a run time error that alerted me to the bug.
> >
> > When I call "new NetAddr::IP->new('10.128.0.0/17')", I get to see "Can't
> > call method "new" on an undefined value...".  The call translates to
> > "'NetAddr::IP'->new->new('10.128.0.0/17')" (Deparse), and since new()
> > without arguments returns undef, I can't see how it would ever
> > work.  So what I see is a fatal error.
> 
> Interesting! What OS and Perl versions are you running? I see you're using
> trn for news; but I don't know the environment in which you experienced the
> run-time errors. I'll run the same script that works fine on this Win32
> workstation on the same workstation running Linux to see what *I* see, if
> you're interested.

Situation here is Darwin 7.4.0, Perl 5.8.5, NetAddr::IP 3.20.

A glance at NetAddr::IP shows that new() is prepared to be called as an
object method, so if the call without an argument happens to return
an actual object on a system, that would make it appear to work.

Otherwise, what I see at a glance doesn't make me too happy.  The
new() method has a prototype.  There *may* be a good reason for that,
but I doubt it.  I'm afraid the author wasn't aware that prototypes
have no effect on methods.

The next few lines contain this sequence:

    my $ip      = lc $_[1];
    # ...
    $ip = 'default' unless defined $ip;

Well, it's too late to check $ip for undef.  This doesn't build trust in
the code.

Mind you, there can still be a lot of useful knowledge and functionality
in the module, but I wouldn't be the least surprised if it showed
irregular behavior under unusual circumstances.

Anno


------------------------------

Date: Sun, 01 Aug 2004 00:46:21 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <x3XOc.21687$iK.5984@newsread2.news.atl.earthlink.net>

"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:cehbsc$rr3$1@mamenchi.zrz.TU-Berlin.DE...
<snip>
> Situation here is Darwin 7.4.0, Perl 5.8.5, NetAddr::IP 3.20.
>

Thanks. I'll try it with the latest versions of Linux and perl around here,
probably on Knoppix 3.4.

> A glance at NetAddr::IP shows that new() is prepared to be called as an
> object method, so if the call without an argument happens to return
> an actual object on a system, that would make it appear to work.
>
> Otherwise, what I see at a glance doesn't make me too happy.  The
> new() method has a prototype.  There *may* be a good reason for that,
> but I doubt it.  I'm afraid the author wasn't aware that prototypes
> have no effect on methods.
>
> The next few lines contain this sequence:
>
>     my $ip      = lc $_[1];
>     # ...
>     $ip = 'default' unless defined $ip;
>
> Well, it's too late to check $ip for undef.  This doesn't build trust in
> the code.
>
> Mind you, there can still be a lot of useful knowledge and functionality
> in the module, but I wouldn't be the least surprised if it showed
> irregular behavior under unusual circumstances.

I hope the author of NetAddr::IP is following this thread, now that I've let
him know about the bugs you spotted, re: extraneous "new" in his examples.

Thanks again for your help.

Best regards,

--
Bill Segraves




------------------------------

Date: 01 Aug 2004 01:08:13 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: The code below returns me nothing.
Message-Id: <slrncgogjt.6hs.abigail@alexandra.abigail.nl>

Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMCMLXXXVII
September MCMXCIII in <URL:news:cehae3$r51$1@mamenchi.zrz.TU-Berlin.DE>:
))  
))  Another difference is that map isn't a statement modifier, so you
))  can combine it with variable declarations.
))  
))      my ( $y) = map ..., $x;
))  
))  is fine, but this is disallowed:
))  
))      my $y = ... for $x;


Well, that's not disallowed (yet) - but it's behaviour is certainly not "DWIM".

       my $y;
          $y = "foo" for 1;
    print $y;                     # Prints foo.


       my $y = "foo" for 1;
    print $y;                     # Compiles, but gives an uninitialized value
                                  # warning.


I would have expected it to print 'foo' both times.



Abigail
-- 
sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");


------------------------------

Date: Sun, 01 Aug 2004 04:06:19 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The code below returns me nothing.
Message-Id: <x7vfg3cxjq.fsf@mail.sysarch.com>

>>>>> "MG" == Mladen Gogala <gogala@sbcglobal.net> writes:

  MG> On Sat, 31 Jul 2004 05:32:42 +0000, Uri Guttman wrote:
  >> passing a single scalar to map does make sense in some circumstances. i
  >> use it to alias a scalar to $_ when i have code that must act on
  >> $_. check out Sort::Maker for a case like that where i generate map code
  >> on scalar values.

  MG> Wow! Thanks Uri, you're really a master! I don't consider myself
  MG> a perl guru, as a matter of fact, I'm rather far from it, and I'm
  MG> grateful for anything that I can learn.

then send your praise to damian conway for that little tidbit as i got
the idea from him. :)

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Sun, 01 Aug 2004 04:08:25 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: The code below returns me nothing.
Message-Id: <x7smb7cxg7.fsf@mail.sysarch.com>

>>>>> "EB" == Eric Bohlman <ebohlman@omsdev.com> writes:

  EB> Uri Guttman <uri@stemsystems.com> wrote in 
  EB> news:x7fz78rbbp.fsf@mail.sysarch.com:

  >> passing a single scalar to map does make sense in some circumstances. i
  >> use it to alias a scalar to $_ when i have code that must act on
  >> $_. check out Sort::Maker for a case like that where i generate map code
  >> on scalar values.

  EB> What advantage does that have over the more traditional use of for() for 
  EB> that purpose?

i needed the result of getting stuff FROM $_ and but not modifying
it. the extract code you pass into Sort::Maker::make_sorter works on $_
and i needed a simple way to alias the full record to $_ and map was the
right trick. look at the code to see how i used it to do that.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Sun, 01 Aug 2004 13:18:09 +0800
From: eloelo <eloelono1@sina.com>
Subject: Re: The code below returns me nothing.
Message-Id: <2n3cq5Fr9kqoU1@uni-berlin.de>

Anno Siegel wrote:

> The same method could be applied with the roles of $addrs and $spaces
> reversed:  Read all spaces into a list, then apply them to each address
> as it is read.  If the list of addresses is huge, but the list of spaces
> isn't, that may be preferable.  Both methods report the same matches,
> but in a different sequence.


why these code below can run only once.
#!/usr/bin/perl
use NetAddr::IP;

open(D2,"IPlist");

open(D3,"IPsegment");
@line=<D3>;
$num=@line;

for($i=0;$i<$num;$i++)
{
        $segment= $line[$i];
        my $space = NetAddr::IP->new($segment);
        
        for my $ip (map {NetAddr::IP->new($_) } <D2>)
        {
        print $ip, "\n"
        if $space->contains($ip);
        }
}


IPlist
62.111.0.29
207.12.133.45
207.12.133.46

IPsegment
62.111.0.0/16
207.12.0.0/16

result
62.111.0.29

it means the "for"(the external one) loop run only once,why?




------------------------------

Date: 1 Aug 2004 10:03:31 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ceif5j$g4u$1@mamenchi.zrz.TU-Berlin.DE>

Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMCMLXXXVII
> September MCMXCIII in <URL:news:cehae3$r51$1@mamenchi.zrz.TU-Berlin.DE>:
> ))  
> ))  Another difference is that map isn't a statement modifier, so you
> ))  can combine it with variable declarations.
> ))  
> ))      my ( $y) = map ..., $x;
> ))  
> ))  is fine, but this is disallowed:
> ))  
> ))      my $y = ... for $x;
> 
> 
> Well, that's not disallowed (yet) - but it's behaviour is certainly not "DWIM".

Well, discouraged.  From perlsyn:

       NOTE: The behaviour of a "my" statement modified with a statement modi-
       fier conditional or loop construct (e.g. "my $x if ...") is undefined.
       The value of the "my" variable may be "undef", any previously assigned
       value, or possibly anything else.  Don't rely on it.  Future versions
       of perl might do something different from the version of perl you try
       it out on.  Here be dragons.

Anno


------------------------------

Date: 1 Aug 2004 11:27:54 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: The code below returns me nothing.
Message-Id: <ceik3q$iqv$1@mamenchi.zrz.TU-Berlin.DE>

eloelo  <eloelono1@sina.com> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> 
> > The same method could be applied with the roles of $addrs and $spaces
> > reversed:  Read all spaces into a list, then apply them to each address
> > as it is read.  If the list of addresses is huge, but the list of spaces
> > isn't, that may be preferable.  Both methods report the same matches,
> > but in a different sequence.
> 
> 
> why these code below can run only once.

For the same reason I gave in my reply to your original posting.

> #!/usr/bin/perl
> use NetAddr::IP;
> 
> open(D2,"IPlist");
> 
> open(D3,"IPsegment");
> @line=<D3>;
> $num=@line;
> 
> for($i=0;$i<$num;$i++)
> {
>         $segment= $line[$i];
>         my $space = NetAddr::IP->new($segment);
>         

The following loop reads the filehandle D2 once to eof.  This happens
the first time through the outer loop.  On the second and further rounds,
D2 already is at eof, so the inner loop doesn't do anything.

>         for my $ip (map {NetAddr::IP->new($_) } <D2>)
>         {
>         print $ip, "\n"
>         if $space->contains($ip);
>         }
> }
> 
> 
> IPlist
> 62.111.0.29
> 207.12.133.45
> 207.12.133.46
> 
> IPsegment
> 62.111.0.0/16
> 207.12.0.0/16
> 
> result
> 62.111.0.29
> 
> it means the "for"(the external one) loop run only once,why?

It runs as often as you make it, but only the first round *does*
anything.

Anno


------------------------------

Date: Sun, 01 Aug 2004 22:53:39 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: The code below returns me nothing.
Message-Id: <TvePc.22431$iK.10947@newsread2.news.atl.earthlink.net>

"Bill Segraves" <segraves_f13@mindspring.com> wrote in message
news:x3XOc.21687$iK.5984@newsread2.news.atl.earthlink.net...
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:cehbsc$rr3$1@mamenchi.zrz.TU-Berlin.DE...
> <snip>
> > Situation here is Darwin 7.4.0, Perl 5.8.5, NetAddr::IP 3.20.
> >
>
> Thanks. I'll try it with the latest versions of Linux and perl around
here,
> probably on Knoppix 3.4.
>

O.K. I'm back with this. Admin safeguards (unplug everything) for T-storms
kept me away from it for a while.

My config for similar tests: Knoppix Linux 3.3 (kernel v. 2.4.22), Perl
5.8.1, NetAddr::IP 3.20.

The example code I'm running passes the syntax checker (syntax O/K.)
with/without the extraneous "new". FYI, I treated the first "new" to be the
extraneous one, in each instance where it appeared.

With the extraneous "new" included in the code, there is a runtime error
"Uncaught exception from user code: Can't call method "new" on an undefined
value ...", same as what you saw.

The same tests, OTOH, performed on the same workstation, running IndigoPerl
(5.6.1), produce different results, in that the code with the extraneous
"new" does not throw the aforesaid runtime error.

So, IMO, you have indeed caught a bug in the author's code; or at the very
least, in his examples.

Best regards,

Bill Segraves






------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 6827
***************************************


home help back first fref pref prev next nref lref last post