Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.perl.misc

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2012, 12:53 AM
Shmuel (Seymour J.) Metz
Guest
 
Posts: n/a
Default Re: Lifetime of my variables?

References: <4f311a37$1$fuzhry+tra$mr2ice@news.patriot.net>
<87lioeu4pt.fsf@vps1.hacking.dk>
<4f3169f6$1$fuzhry+tra$mr2ice@news.patriot.net>
<87ehu6tobv.fsf@vps1.hacking.dk>
Mail-Copies-To: nobody
Organization: Atid/2
X-Treme: C&C,DWS
X-WebTV-Stationery: Standard; BGColor=black; TextColor=black

In <87ehu6tobv.fsf@vps1.hacking.dk>, on 02/07/2012
at 08:50 PM, Peter Makholm <peter@makholm.net> said:

>Have you enabled 'use warnings' and 'use strict'?


Only use strict

>'sub doReceived' generates a function at compile time, refering to
>the variable existing at compile time. Therefore it is not using the
>variable initialized by the my statement in the outer loop.


Even though it is contained in the loop?

I get the same residual value with

mailfile: while (my $mailfile=shift) {
...
foreach (@Received[$ReceivedIx..$#Received]) {

...
doReceived($+{FROM}, $+{HELO} // $+{IP}, $+{RDNS} // '', $+{IP},
$+{BY1}, $+{BY2} // '') || last;
...
}
...
}
...


my $prevHELO;
sub doReceived {
my ($From, $HELO, $rDNS, $IP, $by1, $by2) = @_;
msg("\ndoReceived parameters:\n");
msg("\n\$From=$From\n");
msg("\n\$HELO=$HELO\n");
msg("\n\$rDNS=$rDNS\n");
msg("\n\$IP =$IP \n");
msg("\n\$by1 =$by1 \n");
msg("\n\$by2 =$by2 \n");

$HELO = uc $HELO;
$rDNS = uc $rDNS;
$IP = "[$IP]" unless $IP =~ /\[/;
my $goodHELO;
if ($MAIN:revHELO) {
msg("\t\$prevHELO=$MAIN:revHELO\n");
msg("\t\$prevSrc=$prevSrc\n");
unless (uc $by1 eq $MAIN:revHELO or
"\U$by2.$by1" eq $MAIN:revHELO or
uc $by1 eq $prevSrc or
"\U$by2.$by1" eq $prevSrc) {
$MAIN:revHELO=$HELO;
msg("\t\$prevHELO after mismatch set to $MAIN:revHELO\n");
return undef();
}
if ($prevBogus) {
msg("\tPrevious Received field was bad; skipping $From\n");
return undef();
}
} elsif ($lookup && $MARF) {
$host_info{$IP}{MARF}=1 if $IP;
$host_info{$rDNS}{MARF}=1 if $rDNS;
}
$MAIN:revHELO=$HELO;
msg("\t\$prevHELO set to $MAIN:revHELO\n");
$prevSrc=$rDNS;
$prevIP=$IP;

# Check for loopback or RFC 1918 source IP.
my $skipIP = localIP(inet_aton substr $IP, 1, -1);
$goodIP=$IP unless $skipIP;
$host_info{$IP}{skipIP}=$skipIP;
msg("\nDumper(\$skipIP)\n");
msg(Dumper($skipIP));
msg("\nDumper(\$goodIP)\n");
msg(Dumper($goodIP));
if ($skipIP) {
push @{$host_info{$goodIP}{msg}},
": the spam was routed via $skipIP IP $IP with HELO
$HELO\n";
return 1;
};

# Set up HELO and sent-from processing.
$_ = $HELO;
my $sent;
$sent = 'the spam was sent from';
$sent .= ' or relayed by' if /(?:$relayedDom)$/;

msg("\nTest HELO $HELO for IP or TLD\n");
# Don't process HELO/EHLO if it's TLD;
# validity check if it's IP address.
my $rDNSeff = $rDNS;
$rDNSeff =~ s/^\[$RE{net}{IPv4}\]$//o;
$rDNSeff =~ s/^$RE{net}{IPv4}$//o;
$rDNSeff =~ s/^[\w-]+$//o;
msg("\n\$rDNS=$rDNS, \$rDNSeff=$rDNSeff\n");
if (/^\[$RE{net}{IPv4}\]$/ || /^$RE{net}{IPv4}$/) {
msg("\nHELO $HELO is IP address.\n");
if ($IP eq $_) {
msg("\nHELO $HELO is matching and compliant IP address.\n");
$goodHELO = 1;
} elsif ($IP eq "[$_]") {
msg("\nHELO $HELO is matching but noncompliant IP
address.\n");
} else {
msg("\nHELO $HELO is bogus IP address.\n");
$prevBogus=1;
}
$host_info{$IP}{SMTP}{$rDNSeff.$IP} = $rDNSeff||$IP;
} elsif (/^[\w-]+$/ | $_ eq '.') {
msg("\nHELO $HELO not valid domain.\n");
$host_info{$IP}{SMTP}{$rDNSeff.$IP} = $rDNSeff||$IP;
} elsif ($rDNS eq $HELO) {
msg("\nrDNS $rDNS equal HELO $HELO\n");
$host_info{$IP}{SMTP}{$From} = $rDNS;
} else {
$goodHELO = 1;
$host_info{$HELO}{isHELO} = 1;
$host_info{$HELO}{SMTP}{$From} = $IP;
push @{$host_info{$HELO}{msg}},
": $sent $From\n";
$host_info{$IP}{SMTP}{$From} = $rDNS;
}
$host_info{$IP}{isIP} = 1;
msg("\n\$From from $From\n");
if ($goodHELO) {
push @{$host_info{$IP}{msg}},
": $sent $From in your IP space.\n";
} else {
push @{$host_info{$IP}{msg}},
": $sent $rDNSeff $IP in your IP space.\n";
}
if ($rDNS =~ /$notTLDpat/) {
$host_info{$rDNS}{SrcIP} = $IP;
$host_info{$rDNS}{SMTP}{$From} = $IP;
push @{$host_info{$rDNS}{msg}},
": $sent $From\n";
}
return 1;
}

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org


Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-08-2012, 02:53 AM
Ben Morrow
Guest
 
Posts: n/a
Default Re: Lifetime of my variables?


Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
> In <87ehu6tobv.fsf@vps1.hacking.dk>, on 02/07/2012
> at 08:50 PM, Peter Makholm <peter@makholm.net> said:
>
> >'sub doReceived' generates a function at compile time, refering to
> >the variable existing at compile time. Therefore it is not using the
> >variable initialized by the my statement in the outer loop.

>
> Even though it is contained in the loop?


Yes. This is a necessary consequence of the fact that named subs are
global in scope and created at compile time; given something like

for my $x (1..4) {
sub foo { $x }
}
foo();

which instance of $x is foo goint to use? (The answer, in fact, is that
it uses the one from the first time the loop is entered, since that's
the one that existed at compile time.)

> I get the same residual value with
>
> mailfile: while (my $mailfile=shift) {
> ...
> foreach (@Received[$ReceivedIx..$#Received]) {
>
> ...
> doReceived($+{FROM}, $+{HELO} // $+{IP}, $+{RDNS} // '', $+{IP},
> $+{BY1}, $+{BY2} // '') || last;
> ...
> }
> ...
> }
> ...
>
>
> my $prevHELO;
> sub doReceived {

<snip>
> if ($MAIN:revHELO) {


Um, *what* are you doing here? This is not the 'my $prevHELO' you just
declared, it's a global in the package MAIN (which doesn't usually
exist: the initial default package is called 'main', and package names
are case-sensitive).

(Do I owe Rainer an apology?)

Even if you had used the lexical, you should have expected to keep its
value across calls to the sub, since it is declared outside the scope of
the sub.

Ben

Reply With Quote
  #3 (permalink)  
Old 02-08-2012, 09:31 AM
Shmuel (Seymour J.) Metz
Guest
 
Posts: n/a
Default Re: Lifetime of my variables?

In <s58909-86q2.ln1@anubis.morrow.me.uk>, on 02/08/2012
at 03:53 AM, Ben Morrow <ben@morrow.me.uk> said:

>Um, *what* are you doing here?


Well, what I'm *trying* to do is to have a single variable named
$prevHELO that is used inside the while loop and maintained by the
subroutine. I thought that Rainer was suggesting that I prefix the
package name when using the variable inside the subroutine.

>the initial default package is called 'main'


Whoops!

>(Do I owe Rainer an apology?)


No; doReceived is inside the lexical scope, by design. Whether that
design is flawed is a separate issue :-(

--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>

Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org

Reply With Quote
  #4 (permalink)  
Old 02-08-2012, 04:45 PM
Ben Morrow
Guest
 
Posts: n/a
Default Re: Lifetime of my variables?


Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
> In <s58909-86q2.ln1@anubis.morrow.me.uk>, on 02/08/2012
> at 03:53 AM, Ben Morrow <ben@morrow.me.uk> said:
>
> >Um, *what* are you doing here?

>
> Well, what I'm *trying* to do is to have a single variable named
> $prevHELO that is used inside the while loop and maintained by the
> subroutine. I thought that Rainer was suggesting that I prefix the
> package name when using the variable inside the subroutine.


While that would work, at least if you included the 'undef $prevHELO'
you suggested xthread, it has no advantages over the file-scoped lexical
you were using there. As a general rule of thumb the only reason to ever
use package globals is if you (or some module, like Exporter) need to
get at the variable from outside the current file.

Ben

Reply With Quote
  #5 (permalink)  
Old 02-08-2012, 05:01 PM
Rainer Weikusat
Guest
 
Posts: n/a
Default Re: Lifetime of my variables?

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>:
>> In <s58909-86q2.ln1@anubis.morrow.me.uk>, on 02/08/2012
>> at 03:53 AM, Ben Morrow <ben@morrow.me.uk> said:
>>
>> >Um, *what* are you doing here?

>>
>> Well, what I'm *trying* to do is to have a single variable named
>> $prevHELO that is used inside the while loop and maintained by the
>> subroutine. I thought that Rainer was suggesting that I prefix the
>> package name when using the variable inside the subroutine.

>
> While that would work, at least if you included the 'undef $prevHELO'
> you suggested xthread,


It will also work when localizing $prevHELO during each loop-iteration.

> it has no advantages over the file-scoped lexical you were using
> there.


It has actually disadvantages: A my-Variable is accessed by indexing
into the appropriate lexical pad, ie, the name is resolved at compile
time, while accesing something which resides in the symbol table of
the package requires a symbol table aka 'hash' lookup.

> As a general rule of thumb the only reason to ever use package
> globals is if you (or some module, like Exporter) need to get at the
> variable from outside the current file.


The other reason would be local: It is possible to create a binding
for a package-global variable which only exists during the dynamic
extent of the lexical scope that established it.

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:10 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.