[31322] in Perl-Users-Digest
Perl-Users Digest, Issue: 2567 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Aug 23 21:53:34 2009
Date: Sun, 23 Aug 2009 18:09:08 -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 Sun, 23 Aug 2009 Volume: 11 Number: 2567
Today's topics:
Re: How to code an implicit $_ argument? <nospam-abuse@ilyaz.org>
Re: How to code an implicit $_ argument? <nospam-abuse@ilyaz.org>
Re: How to code an implicit $_ argument? <rvtol+usenet@xs4all.nl>
Re: How to code an implicit $_ argument? sln@netherlands.com
Possibly useful perl script to filter lines in one file <benburch@pobox.com>
Re: Possibly useful perl script to filter lines in one <tadmc@seesig.invalid>
Re: Possibly useful perl script to filter lines in one (Tim McDaniel)
Re: Possibly useful perl script to filter lines in one <uri@StemSystems.com>
Re: Possibly useful perl script to filter lines in one sln@netherlands.com
Re: Possibly useful perl script to filter lines in one <nat.k@gm.ml>
Re: Preventing lines from printing (Tim McDaniel)
Re: Preventing lines from printing <tadmc@seesig.invalid>
Re: Preventing lines from printing (Tim McDaniel)
Re: Preventing lines from printing <uri@StemSystems.com>
Re: Preventing lines from printing sln@netherlands.com
Re: Preventing lines from printing <tadmc@seesig.invalid>
Re: windows one liner to output unix line feed <hjp-usenet2@hjp.at>
Re: windows one liner to output unix line feed <kst-u@mib.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 23 Aug 2009 20:05:00 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <slrnh9387c.tjj.nospam-abuse@chorin.math.berkeley.edu>
On 2009-08-23, Christian Winter <thepoet_nospam@arcor.de> wrote:
>> Thanks, indeed. (I always wanted to implement
>>
>> hard_set(\@array, $slot) = $var;
>>
>> which would alias a slot in array/hash to a scalar...)
> IMHO that shouldn't be too difficult in xs
Of course. The semantic of aliasing is much simpler to implement than
that of copying the value.
The situation is the same as with lvalue-subroutines: it is not that
one needs tricks to make the return value writable - it is the
opposite: Perl core makes extra steps to make the return value of
"usual subroutines" non-modifiable.
The harder part is to find IN WHICH MODULE one may put this
functionality.
Ilya
P.S. And of course, passing the patch through (the mess of?) p5p
might be still tricky. IIRC, I may sit on a hundred of patches
to Perl implementing IMO very important functionality which
were ignored by p5p...
------------------------------
Date: Sun, 23 Aug 2009 20:07:33 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <slrnh938c5.tjj.nospam-abuse@chorin.math.berkeley.edu>
On 2009-08-23, Dr.Ruud <rvtol+usenet@xs4all.nl> wrote:
> Ilya Zakharevich wrote:
>
>> (I always wanted to implement
>>
>> hard_set(\@array, $slot) = $var;
>>
>> which would alias a slot in array/hash to a scalar...)
>
> Data::Alias
Can't locate Data/Alias.pm in @INC ...
It should be in the core.
Ilya
------------------------------
Date: Sun, 23 Aug 2009 22:29:08 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: How to code an implicit $_ argument?
Message-Id: <4a91a695$0$195$e4fe514c@news.xs4all.nl>
Ilya Zakharevich wrote:
> Dr.Ruud:
>> Ilya Zakharevich:
>>> (I always wanted to implement
>>>
>>> hard_set(\@array, $slot) = $var;
>>>
>>> which would alias a slot in array/hash to a scalar...)
>>
>> Data::Alias
>
> Can't locate Data/Alias.pm in @INC ...
>
> It should be in the core.
Chip is implementing ":=", see p5p.
I am not sure that slot aliasing is included, but if so then it would
simply look like:
my @array = qw/ q w e r t y /;
my $key := $array[ 2 ];
or
my $key = "e";
my @array = qw/ q w # r t y /;
$array[ 2 ] := $key;
--
Ruud
------------------------------
Date: Sun, 23 Aug 2009 16:25:34 -0700
From: sln@netherlands.com
Subject: Re: How to code an implicit $_ argument?
Message-Id: <qsj395t8pevrvm4dv37kctsis3l499gl0n@4ax.com>
On Sun, 23 Aug 2009 22:29:08 +0200, "Dr.Ruud" <rvtol+usenet@xs4all.nl> wrote:
>Ilya Zakharevich wrote:
>> Dr.Ruud:
>>> Ilya Zakharevich:
>
>>>> (I always wanted to implement
>>>>
>>>> hard_set(\@array, $slot) = $var;
>>>>
>>>> which would alias a slot in array/hash to a scalar...)
>>>
>>> Data::Alias
>>
>> Can't locate Data/Alias.pm in @INC ...
>>
>> It should be in the core.
>
>Chip is implementing ":=", see p5p.
>
>I am not sure that slot aliasing is included, but if so then it would
>simply look like:
>
> my @array = qw/ q w e r t y /;
> my $key := $array[ 2 ];
^^
Suspiciously looks like a Pascal assignment operator.
Lets get even more confusing.
-sln
------------------------------
Date: Sun, 23 Aug 2009 13:46:26 -0500
From: Ben Burch <benburch@pobox.com>
Subject: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <230820091346263431%benburch@pobox.com>
Hi!
I needed to take the email addresses that bounced out of an original
mailing list. grep -v -f was far to slow, and comm produced unexpected
results, and so I just wrote something to do it in perl. Thought this
might be useful to somebody else;
#!/usr/bin/perl
#
# filter $file1 $file2
#
# Filters all lines in file1 against lines in file2, copying only lines
# from file 1 not found in file2 to STDOUT
#
# get arguments
my $file1 = shift;
my $file2 = shift;
if(!defined($file1) || !defined($file2))
{
print "\nError, must have two arguments.\n";
print "filter <masterfile> <excludefile>\n";
exit 1;
}
# Copy all lines from file2 into a hash
open (EXCLUDE, $file2);
my %exclude = ();
while ($line = <EXCLUDE>)
{
chomp($line);
$exclude{$line} = 1;
}
close EXCLUDE;
# Now go through input line-by-line comparing to hash and only
# printing lines that do not match
open (DATA, $file1);
while ($line = <DATA>)
{
chomp($line);
if(!exists($exclude{$line}))
{
print "$line\n";
}
}
close DATA;
exit 0;
------------------------------
Date: Sun, 23 Aug 2009 14:37:45 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <slrnh93629.934.tadmc@tadmc30.sbcglobal.net>
Ben Burch <benburch@pobox.com> wrote:
> #!/usr/bin/perl
use warnings;
use strict;
> open (EXCLUDE, $file2);
You should always, yes *always*, check the return value from open():
open my $EXCLUDE, '<', $file2 or die "could not open '$file2' $!";
> while ($line = <EXCLUDE>)
while ($line = <$EXCLUDE>)
> if(!exists($exclude{$line}))
unless ( exists $exclude{$line} )
or at least make wise use of whitespace and punctuation:
if ( ! exists $exclude{$line} )
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sun, 23 Aug 2009 19:48:18 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <h6s6e2$479$1@reader1.panix.com>
In article <230820091346263431%benburch@pobox.com>,
Ben Burch <benburch@pobox.com> wrote:
>I needed to take the email addresses that bounced out of an original
>mailing list. grep -v -f was far to slow, and comm produced
>unexpected results, and so I just wrote something to do it in perl.
comm requires that both input files be sorted -- presumably in byte
value order rather than by dictionary order. When comm bites me, it's
because I've forgotten that.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 23 Aug 2009 17:16:12 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <87skfi1bab.fsf@quad.sysarch.com>
>>>>> "BB" == Ben Burch <benburch@pobox.com> writes:
BB> I needed to take the email addresses that bounced out of an original
BB> mailing list. grep -v -f was far to slow, and comm produced unexpected
BB> results, and so I just wrote something to do it in perl. Thought this
BB> might be useful to somebody else;
i find it hard to believe that grep -v -f is slower than perl. did you
benchmark the final results?
BB> #!/usr/bin/perl
BB> #
no warnings or strict. use them.
BB> # get arguments
BB> my $file1 = shift;
BB> my $file2 = shift;
BB> if(!defined($file1) || !defined($file2))
BB> {
BB> print "\nError, must have two arguments.\n";
BB> print "filter <masterfile> <excludefile>\n";
BB> exit 1;
BB> }
much simpler and slightly more accurate is to check @ARGV if it has 2
elements:
unless( @ARGV == 2 ) {
die 'blah' ;
}
and use better names than file1 and file2. they are files of different data
my( $exc_file, $data_file ) = @ARGV ;
BB> # Copy all lines from file2 into a hash
BB> open (EXCLUDE, $file2);
BB> my %exclude = ();
BB> while ($line = <EXCLUDE>)
BB> {
BB> chomp($line);
BB> $exclude{$line} = 1;
BB> }
BB> close EXCLUDE;
use File::Slurp ;
my %exclude = map { chomp; $_ => 1 } read_file( $exc_file ) ;
BB> # Now go through input line-by-line comparing to hash and only
BB> # printing lines that do not match
BB> open (DATA, $file1);
don't use DATA for a file handle as it is the handle name for data in
the source file after the __END__ marker
BB> while ($line = <DATA>)
BB> {
BB> chomp($line);
BB> if(!exists($exclude{$line}))
BB> {
BB> print "$line\n";
BB> }
invert that logic for simpler code:
next if $exclude{ $line } ;
print "$line\n" ;
and if your bounce line file isn't that large (for some definition of
large) you can also slurp and filter it out too.
and since your bounce and exclude lines are all ending in newline, there
is no need to chomp in either case. it makes this much easier.
<untested entire main code>
my %exclude = map { $_ => 1 } read_file( $exc_file ) ;
print grep { !$exclude{ $_ } } read_file( $data_file ) ;
ain't perl cool! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 23 Aug 2009 16:50:51 -0700
From: sln@netherlands.com
Subject: Re: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <jtk395hegh5ke4r6gcgroa11ejivc6vo6p@4ax.com>
On Sun, 23 Aug 2009 14:37:45 -0500, Tad J McClellan <tadmc@seesig.invalid> wrote:
>Ben Burch <benburch@pobox.com> wrote:
>
>> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
>> open (EXCLUDE, $file2);
>
>You should always, yes *always*, check the return value from open():
>
> open my $EXCLUDE, '<', $file2 or die "could not open '$file2' $!";
>
>> while ($line = <EXCLUDE>)
>
> while ($line = <$EXCLUDE>)
>
>> if(!exists($exclude{$line}))
>
> unless ( exists $exclude{$line} )
>
>or at least make wise use of whitespace and punctuation:
>
> if ( ! exists $exclude{$line} )
Hi Tad.
I've seen that always check the return value of open
here on this NG, then die if not true?
Why die if open didn't die? Whats the worse thing that can happen?
I think the worse thing is that a read or write doesen't happen.
It won't crash the system or mess up the file allocation tables.
Its funny, if you pass a failed open filehandle like
open my $fh, 'non-existant-file.txt'
to a read $fh,... the read passivily fails. There is no
fatal error.
But if you pass an undefined filehandle to read, it
die's.
Something to consider since a failed open does not really
cause problems because and apparently an undefined handle is
enough to cause a die from Perl's i/o functions (well at least read ).
So, why is it always, yes always, necessary to check the return
value from open() ?
-sln
------------------------------
Date: Sun, 23 Aug 2009 17:27:13 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: Possibly useful perl script to filter lines in one file out of another.
Message-Id: <B7lkm.180341$ZN.170400@newsfe23.iad>
sln@netherlands.com wrote:
> On Sun, 23 Aug 2009 14:37:45 -0500, Tad J McClellan
> <tadmc@seesig.invalid> wrote:
>
>>Ben Burch <benburch@pobox.com> wrote:
>>
>>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>>> open (EXCLUDE, $file2);
>>
>>You should always, yes *always*, check the return value from open():
>>
>> open my $EXCLUDE, '<', $file2 or die "could not open '$file2' $!";
>>
>>> while ($line = <EXCLUDE>)
>>
>> while ($line = <$EXCLUDE>)
>>
>>> if(!exists($exclude{$line}))
>>
>> unless ( exists $exclude{$line} )
>>
>>or at least make wise use of whitespace and punctuation:
>>
>> if ( ! exists $exclude{$line} )
>
> Hi Tad.
>
> I've seen that always check the return value of open
> here on this NG, then die if not true?
>
> Why die if open didn't die? Whats the worse thing that can happen?
> I think the worse thing is that a read or write doesen't happen.
> It won't crash the system or mess up the file allocation tables.
>
> Its funny, if you pass a failed open filehandle like
> open my $fh, 'non-existant-file.txt'
> to a read $fh,... the read passivily fails. There is no
> fatal error.
>
> But if you pass an undefined filehandle to read, it
> die's.
>
> Something to consider since a failed open does not really
> cause problems because and apparently an undefined handle is
> enough to cause a die from Perl's i/o functions (well at least read ).
>
> So, why is it always, yes always, necessary to check the return
> value from open() ?
>
> -sln
If you want to open/read/write to a file, there's an intended reason.
It doesn't have to be a die, the point is to be aware of the problem
and have it output or log the problem, which helps troubleshoot
problems (and unintended bugs). He said to always check the return
value, he didn't say to always die. If you have a script that doesn't
need to open a file you told it to, why are you opening it?
------------------------------
Date: Sun, 23 Aug 2009 18:46:31 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Preventing lines from printing
Message-Id: <h6s2q7$4td$1@reader1.panix.com>
In article <h6qobl$6is$1@news-01.bur.connect.com.au>,
Diamond, Mark <dot@dot.dot> wrote:
>In AWK I had a BEGIN block, no print lines in the middle, and a very
>long END block.
Just to pick a nit: you *could* do the same thing in Perl.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
075.awk:
#! /usr/bin/awk -f
BEGIN { sum = 0; count = 0; }
{ sum += $2 * 100; ++count; }
END { print "The average of column 2 is this percent: ", sum / count;
}
075.in:
chase .0225
citi .0110
uhcu .03
075.awk < 075.in:
The average of column 2 is this percent: 2.11667
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
075.pl:
#! /usr/bin/perl -wan
BEGIN { $sum = 0; $count = 0; }
{ $sum += $F[1] * 100; ++$count; }
END { print "The average of column 2 is this percent: ", ($sum / $count), "\n"; }
075.pl < 075.in:
The average of column 2 is this percent: 2.11666666666667
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mind you, I don't recommend this. I always do "use strict;", which
requires me (in general) to declare variables with "my".
But the scope of "my $sum" and "my $count" in any of those blocks ends
with its block, and I don't know how to "my" a variable across a BEGIN
block boundary.
So I think you're right to rewrite a large complicated piece of code
to be more Perly and not rely on "-n -a", and for short pieces of
code, you can just use awk as before.
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 23 Aug 2009 14:47:25 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Preventing lines from printing
Message-Id: <slrnh936ke.934.tadmc@tadmc30.sbcglobal.net>
Tim McDaniel <tmcd@panix.com> wrote:
> I don't know how to "my" a variable across a BEGIN
> block boundary.
You can have a file-scoped lexical declared before the BEGIN block:
my($sum, $count);
BEGIN { $sum = 0; $count = 0; }
Note this excerpt from "Private Variables via my()" in perlsub.pod:
A C<my> has both a compile-time and a run-time effect. At compile
time, the compiler takes notice of it. The principal usefulness
of this is to quiet C<use strict 'vars'>
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sun, 23 Aug 2009 20:51:45 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Preventing lines from printing
Message-Id: <h6sa51$ch4$1@reader1.panix.com>
In article <slrnh936ke.934.tadmc@tadmc30.sbcglobal.net>,
Tad J McClellan <tadmc@seesig.invalid> wrote:
>Tim McDaniel <tmcd@panix.com> wrote:
>
>> I don't know how to "my" a variable across a BEGIN
>> block boundary.
>
>You can have a file-scoped lexical declared before the BEGIN block:
>
> my($sum, $count);
> BEGIN { $sum = 0; $count = 0; }
>
>Note this excerpt from "Private Variables via my()" in perlsub.pod:
>
> A C<my> has both a compile-time and a run-time effect. At compile
> time, the compiler takes notice of it. The principal usefulness
> of this is to quiet C<use strict 'vars'>
But it interacts non-intuitively with "perl -n". My test program,
transmogrified:
#! /usr/bin/perl -wan
use strict;
my ($sum, $count);
BEGIN { $sum = 0; my $count = 0; }
$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
Data:
chase .0225
citi .0110
uhcu .03
Output:
After 1 lines, sum is 2.25
After 1 lines, sum is 1.1
After 1 lines, sum is 3
The average of column 2 is this percent: 2.25/1=2.25
$count staying 1 on each iteration is easy to see. As "man perlrun"
says, the program is effectively
LINE:
while (<>) {
use strict;
my ($sum, $count);
BEGIN { $sum = 0; my $count = 0; }
$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
}
that is, $sum and $count are redeclared and zorched on every loop
iteration.
But I don't know why $sum keeps its value from the FIRST iteration
... maybe $sum being used in BEGIN somehow squirrels away a reference
to that iteration's value of $sum, and that's somehow what END sees,
instead of the last iteration's value of $sum, or $::sum, or sum such?
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Sun, 23 Aug 2009 17:18:36 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Preventing lines from printing
Message-Id: <87ocq61b6b.fsf@quad.sysarch.com>
>>>>> "TM" == Tim McDaniel <tmcd@panix.com> writes:
TM> #! /usr/bin/perl -wan
TM> BEGIN { $sum = 0; $count = 0; }
no need to initialize those to 0 as += won't warn when adding to
undef. same is true for ++ and .= .
now you can declare those vars without the BEGIN
TM> { $sum += $F[1] * 100; ++$count; }
TM> END { print "The average of column 2 is this percent: ", ($sum / $count), "\n"; }
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 23 Aug 2009 16:35:40 -0700
From: sln@netherlands.com
Subject: Re: Preventing lines from printing
Message-Id: <27k3951m9kbh8jp63jds8t67rnfcm4fh2s@4ax.com>
On Sun, 23 Aug 2009 20:51:45 +0000 (UTC), tmcd@panix.com (Tim McDaniel) wrote:
>In article <slrnh936ke.934.tadmc@tadmc30.sbcglobal.net>,
>Tad J McClellan <tadmc@seesig.invalid> wrote:
>>Tim McDaniel <tmcd@panix.com> wrote:
>>
>>> I don't know how to "my" a variable across a BEGIN
>>> block boundary.
>>
>>You can have a file-scoped lexical declared before the BEGIN block:
>>
>> my($sum, $count);
>> BEGIN { $sum = 0; $count = 0; }
>>
>>Note this excerpt from "Private Variables via my()" in perlsub.pod:
>>
>> A C<my> has both a compile-time and a run-time effect. At compile
>> time, the compiler takes notice of it. The principal usefulness
>> of this is to quiet C<use strict 'vars'>
>
>But it interacts non-intuitively with "perl -n". My test program,
>transmogrified:
>
>#! /usr/bin/perl -wan
>use strict;
>my ($sum, $count);
>BEGIN { $sum = 0; my $count = 0; }
>$sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
>END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
>Data:
>
>chase .0225
>citi .0110
>uhcu .03
>
>Output:
>
>After 1 lines, sum is 2.25
>After 1 lines, sum is 1.1
>After 1 lines, sum is 3
>The average of column 2 is this percent: 2.25/1=2.25
>
>$count staying 1 on each iteration is easy to see. As "man perlrun"
>says, the program is effectively
>
> LINE:
> while (<>) {
> use strict;
> my ($sum, $count);
> BEGIN { $sum = 0; my $count = 0; }
> $sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
> END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
> }
>
>that is, $sum and $count are redeclared and zorched on every loop
>iteration.
>
>But I don't know why $sum keeps its value from the FIRST iteration
>... maybe $sum being used in BEGIN somehow squirrels away a reference
>to that iteration's value of $sum, and that's somehow what END sees,
>instead of the last iteration's value of $sum, or $::sum, or sum such?
BEGIN, END, labels and goto's are crutches that should not be part of
scoped languages with if,then,else blocks.
And now somebody is implementing a := operator ala Pascal.
Perl 6 is going to do // operator...
Its just rediculous and totally out of control.
If Perl could have done ANSI, they would have. Its too much.
-sln
------------------------------
Date: Sun, 23 Aug 2009 18:39:06 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Preventing lines from printing
Message-Id: <slrnh93k6p.b3k.tadmc@tadmc30.sbcglobal.net>
Tim McDaniel <tmcd@panix.com> wrote:
> In article <slrnh936ke.934.tadmc@tadmc30.sbcglobal.net>,
> Tad J McClellan <tadmc@seesig.invalid> wrote:
>>Tim McDaniel <tmcd@panix.com> wrote:
>>
>>> I don't know how to "my" a variable across a BEGIN
>>> block boundary.
>>
>>You can have a file-scoped lexical declared before the BEGIN block:
>>
>> my($sum, $count);
>> BEGIN { $sum = 0; $count = 0; }
>>
>>Note this excerpt from "Private Variables via my()" in perlsub.pod:
>>
>> A C<my> has both a compile-time and a run-time effect. At compile
>> time, the compiler takes notice of it. The principal usefulness
>> of this is to quiet C<use strict 'vars'>
>
> But it interacts non-intuitively with "perl -n".
Oh. I didn't notice the -n. Sorry.
> My test program,
> transmogrified:
>
> #! /usr/bin/perl -wan
> use strict;
> my ($sum, $count);
So this is one of those rare cases where you do NOT want lexical scoping...
our($sum, $count);
seems to give the right output...
> BEGIN { $sum = 0; my $count = 0; }
> $sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
> END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
> Data:
>
> chase .0225
> citi .0110
> uhcu .03
>
> Output:
>
> After 1 lines, sum is 2.25
> After 1 lines, sum is 1.1
> After 1 lines, sum is 3
> The average of column 2 is this percent: 2.25/1=2.25
>
> $count staying 1 on each iteration is easy to see. As "man perlrun"
> says, the program is effectively
>
> LINE:
> while (<>) {
> use strict;
> my ($sum, $count);
> BEGIN { $sum = 0; my $count = 0; }
> $sum += $F[1] * 100; ++$count; print "After $count lines, sum is $sum\n";
> END { print "The average of column 2 is this percent: $sum/$count=", ($sum / $count), "\n"; }
>
> }
>
> that is, $sum and $count are redeclared and zorched on every loop
> iteration.
>
> But I don't know why $sum keeps its value from the FIRST iteration
Isn't the END block a closure there?
> ... maybe $sum being used in BEGIN somehow squirrels away a reference
> to that iteration's value of $sum, and that's somehow what END sees,
> instead of the last iteration's value of $sum, or $::sum, or sum such?
^^^^^^^^
nyuk!
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sun, 23 Aug 2009 17:43:06 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: windows one liner to output unix line feed
Message-Id: <slrnh92osb.s28.hjp-usenet2@hrunkner.hjp.at>
On 2009-08-23 13:58, Scott Bryce <sbryce@scottbryce.com> wrote:
> Peter J. Holzer wrote:
>> On 2009-08-23 00:08, Scott Bryce <sbryce@scottbryce.com> wrote:
>>> Keith Thompson wrote:
>>>> Brian Wakem <no@email.com> writes:
>>>>> Using \x0A instead of \n should work.
>>>> That seems unlikely, since "\x0a" and "\n" have exactly the same
>>>> value.
>>>
>>> Not on a Windows system.
>>
>> Please.
>>
>> Is it really so hard to test your assumptions before posting them?
>
> Actually, I DID test this.
>
> -----------
>
> use strict;
> use warnings;
>
> open my $OUTFILE, '>', 'test.txt' or die 'Cannot open test.txt';
> print $OUTFILE "A line of text\n";
> print $OUTFILE "A line of text\n";
>
> close $OUTFILE or die 'cannot close test.txt';
>
> -----------
No, you didn't. Your script doesn't use \x0A, so it says nothing about
whether \n and \x0A are the same or not.
Change the second print into:
print $OUTFILE "A line of text\x0A";
> Both lines in test.txt end in \x0D\x0A
With the second line changed, still both lines end with 0D 0A in the
file. This is an indication (but no proof) that "\n" and "\x0A" are
indeed the same.
> But now I see my mistake. If I binmode $OUTFILE, then both lines end in
> \x0A.
>
> The assumption I was making is consistent with the documentation:
>
> The operating system, device drivers, C libraries, and Perl run-time
> system all work together to let the programmer treat a single character
> (\n ) as the line terminator, irrespective of the external representation.
>
> which suggests that "\n" does not have a specific ASCII value.
This is true (on MacOS classic "\n" was "\x0D" and on EBCDIC based
systems it's "\x15", IIRC), but on Windows "\n" is always "\x0A".
And in any case it is always a single character, regardless of the
convention for text files on the OS.
> So the bottom line is that on a Windows system, the value of "\n"
> depends on whether you are working in text mode or binary mode.
No, it doesn't. On Windows "\n" is always the single character with the
code 10 decimal (or 0x0A hexadecimal).
The difference between text mode and binary mode is that text mode
converts to the text file convention of the local OS: For Windows that
means that when you print the single character "\n", the :crlf layer
sends to bytes (0x0D 0x0A) to the file. That's a relatively simple
conversion, there are more complicated conversions. For example, some
OSs had text files with fixed length, space padded lines. On such a
system,
print $OUTFILE "A line of text\n";
causes (for example) 80 bytes to be written to the file: "A line of
text" followed by 66 spaces. But that doesn't mean that the value of
"\n" is 66 spaces.
hp
------------------------------
Date: Sun, 23 Aug 2009 12:01:45 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: windows one liner to output unix line feed
Message-Id: <lny6panyli.fsf@nuthaus.mib.org>
Scott Bryce <sbryce@scottbryce.com> writes:
> Keith Thompson wrote:
>> Brian Wakem <no@email.com> writes:
>>> Using \x0A instead of \n should work.
>>
>> That seems unlikely, since "\x0a" and "\n" have exactly the same
>> value.
>
> Not on a Windows system.
Have you tried it?
perl -e "if (qq(\x0a) eq qq(\n)) { print qq(yes\n) } else { print qq(no\n) }"
I don't have access to a Windows system at the moment; I'll try it
tomorrow.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
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 V11 Issue 2567
***************************************