[22367] in Perl-Users-Digest
Perl-Users Digest, Issue: 4588 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 19 21:05:53 2003
Date: Wed, 19 Feb 2003 18:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 19 Feb 2003 Volume: 10 Number: 4588
Today's topics:
Re: A simple problem befuddling my simple head <tore@aursand.no>
Re: Also religious: brackets <shondell@cis.ohio-state.edu>
Re: Also religious: brackets <abigail@abigail.nl>
Re: Also religious: brackets <goldbb2@earthlink.net>
Re: Also religious: brackets <tore@aursand.no>
Re: Check for occurrence of element in Array <shanem@nospam.ll.mit.edu>
Re: Extracting patterns <goldbb2@earthlink.net>
Re: Extracting patterns <noreply@gunnar.cc>
Re: Having problems writing a tar file. Correction <justinagraham@hotmail.com>
Re: Having problems writing a tar file. Correction <rereidy@indra.com>
Having problems writing a tar file. <justinagraham@hotmail.com>
Re: How do I get the current date? <abigail@abigail.nl>
Re: Need Help with Pattern Matching <PleaseDontThrowSpam@Me.com>
Re: Perl, Large Memory, and autofork <goldbb2@earthlink.net>
Re: pipe() + fork() + exit() == problem? (Anno Siegel)
Re: pipe() + fork() + exit() == problem? <goldbb2@earthlink.net>
Re: Problem with buffering on non-blocking socket under <goldbb2@earthlink.net>
Re: reading excel spreadsheet with perl (John McNamara)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 20 Feb 2003 01:23:49 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: A simple problem befuddling my simple head
Message-Id: <pan.2003.02.20.00.14.57.664404@aursand.no>
On Wed, 19 Feb 2003 11:33:45 -0800, Liam James wrote:
> $email1=$cgiobject->param("email1");
> $email2=$cgiobject->param("email2");
Actually, you can give them the same name;
my @emails = $cgiobject->param('email') || ();
> print $cgiobject->header;
Let the web-server know what type of data it should expect;
print $cgiobject->header( -type => 'text/html' );
> If ($email1 eq $email2)
Seems like a typo; Try 'if' instead of 'If'.
--
Tore Aursand - tore@aursand.no - http://www.aursand.no/
------------------------------
Date: 19 Feb 2003 18:05:14 -0500
From: Ryan Shondell <shondell@cis.ohio-state.edu>
Subject: Re: Also religious: brackets
Message-Id: <xcwznor98id.fsf@psi.cis.ohio-state.edu>
Sandman <mr@sandman.net> writes:
> In article <xcw65rfanxi.fsf@psi.cis.ohio-state.edu>,
> Ryan Shondell <shondell@cis.ohio-state.edu> wrote:
>
> > > # 1
> > > if (statement){
> > > do ..
> > > } else {
> > > do ...
> > > }
(snip)
> > Or #4:
> >
> > if (something) {
> > do ...
> > }
> > else {
> > do ...
> > }
>
> That's identical to #1 apart from one newline.
> You just made that one to be compliant with Larry! :-D
Some people have serious preferences with cuddled elses. I figured it
was different enough to mention.
> > You should check out perldoc perlstyle, if you haven't already. It has
> > some info on things like this.
>
> Well, this is, including perldoc perlstyle, after all a matter of personal
> taste rather than right/wrong :)
Sure, sure. Well, until you inherit that piece of garbage code with no
indentation or spacing or any of the things that keep our collective
sanity. Then, it's wrong. :)
--
Ryan Shondell <shondell@cis.ohio-state.edu>
------------------------------
Date: 19 Feb 2003 23:26:39 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Also religious: brackets
Message-Id: <slrnb584lf.1ea.abigail@alexandra.abigail.nl>
Sandman (mr@sandman.net) wrote on MMMCDLIX September MCMXCIII in
<URL:news:mr-7A29A9.23535019022003@news.fu-berlin.de>:
|| In article <xcw65rfanxi.fsf@psi.cis.ohio-state.edu>,
|| Ryan Shondell <shondell@cis.ohio-state.edu> wrote:
||
|| > > # 1
|| > > if (statement){
|| > > do ..
|| > > } else {
|| > > do ...
|| > > }
|| > >
|| > > # 2
|| > > if (statement){ do ... }
|| > > else { do ... }
|| > >
|| > > # 3
|| > > if (statement)
|| > > {
|| > > do ...
|| > > }
|| > > else
|| > > {
|| > > do ...
|| > > }
|| >
|| > Or #4:
|| >
|| > if (something) {
|| > do ...
|| > }
|| > else {
|| > do ...
|| > }
||
|| That's identical to #1 apart from one newline.
Well, apart from newlines, your #1 and #2 are identical. And apart
from whitespace, #1, #2, #3 and #4 are identical. This religious
war is about nothing *BUT* whitespace.
However, the difference between #1 and #4 is more than a newline.
There's also an extra space present in #4, making #4 far more
pleasing to the eye than #1. I hadn't noticed the newline of
Ryan until you pointed it out. But the missing space in #1 and #2
pokes at me like a sore thumb.
Abigail
--
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
print } sub __PACKAGE__ { &
print ( __PACKAGE__)} &
__PACKAGE__
( )
------------------------------
Date: Wed, 19 Feb 2003 19:06:42 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Also religious: brackets
Message-Id: <3E541C12.91543E0B@earthlink.net>
Sandman wrote:
>
> How do you use brackets in your code in loops or if statements? Here
> are some versions I can come up with:
[snip]
> Any other variations you prefer? I am a #1 guy myself, but use #2 if
> the do's are one-liners.
If the two branches are single expressions, then I sometimes prefer to
use ?: instead of if(){}else{}.
Also, while I normally use something like your #1, I put the spaces a
bit differently:
if( statement ) {
... do ...
} else {
... do ...
}
[Note the lack of a space between the 'if' and the '(', the space after
the '(', the space before the ')', and the space between the ')' and
the '{'.]
Sometimes, 'statement' is really lengthy... I'll sometimes write code
as:
if( statement1 &&
statement2 &&
statement3
) {
... do ...
} else {
... do ...
}
--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print
------------------------------
Date: Thu, 20 Feb 2003 01:23:50 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Also religious: brackets
Message-Id: <pan.2003.02.20.00.23.09.809670@aursand.no>
On Wed, 19 Feb 2003 23:34:56 +0100, Sandman wrote:
> # 1
> if (statement){
> do ..
> } else {
> do ...
> }
Almost. For other than one-liners, I use this one;
if ( statement ) {
do
}
else {
do
}
One-liners goes like this;
do if ( statement );
Matching several statements on one line (example);
my $name = ( defined ... ) ? ... : 'Default';
Many variations of the last one. Can have plenty of fun with it. :)
--
Tore Aursand - tore@aursand.no - http://www.aursand.no/
------------------------------
Date: Wed, 19 Feb 2003 18:22:52 -0500
From: Shane McDaniel <shanem@nospam.ll.mit.edu>
Subject: Re: Check for occurrence of element in Array
Message-Id: <3E5411CC.D21A3B2D@nospam.ll.mit.edu>
Tad McClellan wrote:
>
> upro <upro@gmx.net> wrote:
>
> > E. g. my array contains the valuaes "de", "en", and "fr". I have
> > anther list with iso codes, and I want to check for their presence.
>
> > I have heard that it is better tpo do this with a hash but I don't
> > know how to do that.
>
> my %tld;
> $tld{de} = 1;
> $tld{en} = 1;
> $tld{fr} = 1;
>
> if ( $tld{$isocode} )
> { print "found $isocode\n" }
> else
> { print "$isocode not found\n" }
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
Isn't the exist() function the better way to check for existance?
-shane
------------------------------
Date: Wed, 19 Feb 2003 18:50:58 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Extracting patterns
Message-Id: <3E541862.663128B@earthlink.net>
Gunnar Hjalmarsson wrote:
>
> Ryan McCarthy wrote:
> > I am trying to extract to an array all the different
> > permutations of a string formatted like this:
> >
> > ABCD(E|F)GHI...
> >
> > Where this would produce an array with two entries
> >
> > 1. ABCDEGHI
> > 2. ABCDFGHI
>
> Before I saw Benjamin's sophisticated solution,
> I put together this code:
>
> $string = 'ABC(1|2)DEF(3|4)GHI';
> push @array, $string;
> for (1..$string =~ s/(\(\w\|\w\))/$1/g) {
> # for each occurrence of (X|Y)
> for (@array) {
> ($string = $_) =~ s/\((\w)\|\w\)/$1/;
> push @tmp, $string;
> ($string = $_) =~ s/\(\w\|(\w)\)/$1/;
> push @tmp, $string;
> }
> @array = splice @tmp;
> }
This doesn't work with nested patterns, nor for alternates which are
more than a single character, though.
Hmm, I have thought of a non-recursive solution, though, inspired by
this bit of code.
my @in = 'ABC(1|2)DEF(3|4)GHI';
my @out = ();
while( @in ) {
my $pat = pop @in;
if( $pat =~ /(.*?) \( ([^()]*) \) (.*)/xs ) {
my ($left, $mid, $right) = ($1, $2, $3);
push @in, map "$left$_$right", split /\|/, $mid;
} elsif( $pat =~ tr/()// ) {
die "Pattern '$pat' contains unbalanced parens\n";
} else {
push @out, $pat;
}
}
[Hmm, it would be interesting to see a Godzilla-ish index/substr
solution to this]
--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print
------------------------------
Date: Thu, 20 Feb 2003 00:51:57 GMT
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Extracting patterns
Message-Id: <NEV4a.871$ID6.2267@newsc.telia.net>
Benjamin Goldberg wrote:
> Gunnar Hjalmarsson wrote:
>>Before I saw Benjamin's sophisticated solution,
>>I put together this code:
>>
>> $string = 'ABC(1|2)DEF(3|4)GHI';
>> push @array, $string;
>> for (1..$string =~ s/(\(\w\|\w\))/$1/g) {
>> # for each occurrence of (X|Y)
>> for (@array) {
>> ($string = $_) =~ s/\((\w)\|\w\)/$1/;
>> push @tmp, $string;
>> ($string = $_) =~ s/\(\w\|(\w)\)/$1/;
>> push @tmp, $string;
>> }
>> @array = splice @tmp;
>> }
>
> This doesn't work with nested patterns, nor for alternates which are
> more than a single character, though.
True. The latter could be easily fixed (I don't know about Ryan's actual
need), but the nested part is trickier...
> Hmm, I have thought of a non-recursive solution, though, inspired by
> this bit of code.
I'm glad that my beginner-level code worked as an inspiration. :)
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 20 Feb 2003 00:51:12 GMT
From: "Justin A. Graham" <justinagraham@hotmail.com>
Subject: Re: Having problems writing a tar file. Correction
Message-Id: <3E54268A.2000603@hotmail.com>
Hi,
I am trying to tar a list of file with the following snippet;
# Archive files
$tar = Archive::Tar->new();
$tar->add_files(@files);
$tar->write($filename);
@files and $filename are valid...
The result is the tar file $filename filled with zeroes.
What am I doing wrong?
Thanks
Justin
------------------------------
Date: Wed, 19 Feb 2003 18:07:12 -0700
From: Ron Reidy <rereidy@indra.com>
Subject: Re: Having problems writing a tar file. Correction
Message-Id: <3E542A40.7000709@indra.com>
perldoc Archive::Tar shows the write() metjod takes 2 arguments. Did
you try it this way?
--
Ron Reidy
Oracle DBA
Justin A. Graham wrote:
> Hi,
>
> I am trying to tar a list of file with the following snippet;
>
> # Archive files
> $tar = Archive::Tar->new();
> $tar->add_files(@files);
> $tar->write($filename);
>
> @files and $filename are valid...
>
> The result is the tar file $filename filled with zeroes.
>
> What am I doing wrong?
>
> Thanks
> Justin
>
>
>
------------------------------
Date: Thu, 20 Feb 2003 00:49:34 GMT
From: "Justin A. Graham" <justinagraham@hotmail.com>
Subject: Having problems writing a tar file.
Message-Id: <3E542628.40806@hotmail.com>
Hi,
I am trying to tar a list of file with the following snippet;
# Archive files
$tar = Archive::Tar->new();
$tar->add_files(@files);
$tar->write($filename);
@files and $filename are valid...
The result is the list @files written serially to the file $filename.
I.e. all the filenames are just concatenated together and written to the
tar file.
What am I doing wrong?
Thanks
Justin
------------------------------
Date: 19 Feb 2003 23:07:17 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How do I get the current date?
Message-Id: <slrnb583h5.1ea.abigail@alexandra.abigail.nl>
Tintin (me@privacy.net) wrote on MMMCDLIX September MCMXCIII in
<URL:news:b30ur8$1fh22h$1@ID-172104.news.dfncis.de>:
""
"" "Justin A. Graham" <justinagraham@hotmail.com> wrote in message
"" news:3E53E86F.2090803@hotmail.com...
"" > I want to write a script that zip up files, and names the archive
"" > file-month-day-year.zip
""
"" Please try to use a sensible date format where ever possible. The ISO date
"" format is unambigious and sorts easily (YYYYMMDD)
Hmmm. There is no "the ISO date format". The ISO standard dealing with
dates defines a whole army of allowed formats.
Abigail
--
$"=$,;*{;qq{@{[(A..Z)[qq[0020191411140003]=~m[..]g]]}}}=*_;
sub _ {push @_ => /::(.*)/s and goto &{ shift}}
sub shift {print shift; @_ and goto &{+shift}}
Hack ("Just", "Perl ", " ano", "er\n", "ther "); # 20030220
------------------------------
Date: Thu, 20 Feb 2003 00:56:08 -0000
From: "Mikey" <PleaseDontThrowSpam@Me.com>
Subject: Re: Need Help with Pattern Matching
Message-Id: <b3190k$46s$1@wisteria.csv.warwick.ac.uk>
I'm getting a lot of:
Use of uninitialised variable in numeric comparison.....
And the list isn't sorted, it just prints out the default order of the data
file.
Do I need to initialize something somewhere?
Thanks for all your help,
Mikey
"Marc" <marc.beyer@gmx.li> wrote in message
news:b30ub8$1grhqp$1@ID-123680.news.dfncis.de...
>
> > Thanks Marc, but I asked for it to be sorted by height! (ascending)
>
> Ack. Mea culpa, that's what you get for doing stuff at three in the
morning
> ;-)
>
> This will do what your split does:
>
> sub make_sort_1 {
> ($a =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d\.\d+)/)[0]
> <=>
> ($b =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d\.\d+)/)[0];
> }
>
> Which produces the output
>
> 01.02.1983 100 Blaro Van Daronimo 4.4
> 12.01.1963 100 John Smith 5.11
> 16.03.1977 400 Johnny Barrosh 5.4
> 09.01.1987 200 Steve McHooter 5.5
> 27.02.1955 200 Jimmy Dolan Andrews 5.8
>
> However, I assume what you actually want to have is this:
>
> 01.02.1983 100 Blaro Van Daronimo 4.4
> 16.03.1977 400 Johnny Barrosh 5.4
> 09.01.1987 200 Steve McHooter 5.5
> 27.02.1955 200 Jimmy Dolan Andrews 5.8
> 12.01.1963 100 John Smith 5.11
>
> which can be done via
>
> my ($aa,$ab,$ba,$bb);
> sub make_sort_2 {
> ($aa,$ab)= $a =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d)\.(\d+)/;
> ($ba,$bb)= $b =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d)\.(\d+)/;
> $aa==$ba ? $ab <=> $bb : $aa <=> $ba;
> }
>
> As for the things you mentioned in the conversation with Benjamin, once
> you've got the sorted array you can do
>
> my @split;
> foreach (@sorted) {
> @split = $_ =~ m/([0-9.]*)\s(\d*)\s([a-zA-Z ]*\s\w*)\s(\d\.\d+)/;
> # do stuff with @split
> }
>
> to individually access the columns of data. There's probably a way to do
> this within the make_sort subroutine but I can't think of one right now.
>
> > Incidently, when I tried your idea as well as the following lines:
> > use strict;
> > use warnings;
> >
> > I was getting "use of uninitialized value in string comparison" and the
> > data wasn't sorted as expected.
>
> Hmm, my version works fine here, for the space-delimited data that is. If
> you feed it data delimited by the | you will get these warnings cause the
> regex doesn't match and the comparison operator doesn't have anything to
> work on.
>
> Have fun,
>
> Marc
------------------------------
Date: Wed, 19 Feb 2003 20:43:54 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Perl, Large Memory, and autofork
Message-Id: <3E5432DA.2748ADDD@earthlink.net>
Mike Hunter wrote:
[snip]
> The program shoves obscene amounts of data into a hash (7 byte binary
> key, listref to 7 scalar/numeric values)...is there anything I should
> be considering in trying to keep down memory usage?
Tie your hash to a disk database; it will slow your program down, but
will drastically reduce memory usage if your hash is huge.
use DB_File;
tie my(%hash), "DB_File";
tied(%hash)->filter_fetch_value( sub { $_ = [unpack "N*", $_] } );
tied(%hash)->filter_store_value( sub { $_ = pack "N*", @$_ } );
Just remember, though, that you won't be able to do:
$hash{$foo}[3] = 7;
You'll have to do:
(my $a = $hash{$foo})->[3] = 4;
$hash{$foo} = $a;
PS: Change 'N*' to 'd*' if your numeric values are real/float values,
not integers.
=======================================================================
You could work around that limitation with something like the following:
package MyTieFile;
use DB_File;
sub TIEHASH {
my $class = shift;
my $self = DB_File->TIEHASH(@_);
bless \$self, $class;
}
sub CLEAR { ${shift()}->CLEAR }
sub EXISTS { ${shift()}->EXISTS(@_) }
sub FETCH {
my ($self, $key) = @_;
tie(my(@a), 'MyTiedValue', $$self, $key);
\@a;
}
sub STORE {
my ($self, $key, $val) = @_;
my $t = tied $val;
if( $t and $t->isa("MyTiedValue") and $t->[1] == $self ) {
return;
}
${shift()}->STORE( shift(), pack 'N*', @{shift()} );
}
###############################################################
package MyTiedValue;
use constant PACKLEN => length pack 'N';
sub TIEARRAY { bless [@_[1..$#_], $_[0]] }
sub FETCHSIZE {
my ($db, $key) = @{ shift() };
my $val = $db->FETCH($key);
return 0 unless defined $val;
int(length( $val ) / PACKLEN);
}
sub FETCH {
my ($db, $key) = @{ shift() };
my $ix = shift;
my $packed = $db->FETCH($key);
return unless defined($packed)
and length($packed) > $ix * PACKLEN;
return unpack('N', substr($packed, $ix * PACKLEN, PACKLEN));
}
sub STORE {
my ($db, $key) = @{ shift() };
my ($ix, $val) = @_;
my $packed = $db->FETCH($key);
$packed = "" unless defined($packed);
$packed |= "\0" x (PACKLEN * $ix);
substr( $packed, $ix * PACKLEN, PACKLEN ) = pack("N", $val);
$db->STORE($key, $packed);
}
1;
__END__
Then you can do:
tie my(%hash), 'MyTieFile';
$hash{$foo}[2] = 12;
, just like magic.
(if you want to be able to push, pop, shift, unshift, splice the values
of %hash, then you'll have to implement those yourself.)
The two level tie will be slower than a one-level tie, but that's the
expense you pay for convenience.
--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print
------------------------------
Date: 19 Feb 2003 23:06:05 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: pipe() + fork() + exit() == problem?
Message-Id: <b312kt$pee$2@mamenchi.zrz.TU-Berlin.DE>
Gordan <gordan@_NOSPAM_bobich.net> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > <gordan@_NOSPAM_bobich.net> wrote in comp.lang.perl.misc:
[...]
> > That's one of the applications message queues were designed for.
> >
> > A socket-based solution would be more portable.
>
> Well, that's arguable. It would only be portable to systems that support
> UNIX sockets.
Not if you use internet sockets also for single-machine applications.
I think most programs do that these days. The overhead isn't that bad.
Anno
------------------------------
Date: Wed, 19 Feb 2003 19:00:27 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: pipe() + fork() + exit() == problem?
Message-Id: <3E541A9B.B7B197BB@earthlink.net>
gordan@_NOSPAM_bobich.net wrote:
>
> Hi!
>
> Got a small problem. I'm trying to write an application that regulates the
> number of forked children according to it's outstanding workload (within
> limits, obviously).
>
> I use pipes to communicate the process queue between the queue process and
> the worker processes. There is one pipe for writing to the queue, and one
> pipe for the forked worker threads to read from. This method works very
> nicely, and I'd like to keep it.
Are you *sure* that it works nicely? Are you performing locking on
the queue? What keeps more than one worker from reading at once?
Also, what perl primitive are you using to read data from the queue?
If you're using any of read(), readline(), or <>, then each worker is
performing input-buffering, and may be reading more than just one job--
it may be getting a job and a half, or somesuch (leaving half a job in
the queue, which will be seen by another worker as corrupted data).
I'd be willing to bet that if you were to add locking (either via
flock($rd, LOCK_EX) or via a second pair of pipes), and change your
reading of jobs to use sysread(), then the problem will go away.
--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print
------------------------------
Date: Wed, 19 Feb 2003 18:40:20 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Problem with buffering on non-blocking socket under win32
Message-Id: <3E5415E4.3951A743@earthlink.net>
Edward Wildgoose wrote:
>
> Benjamin, Thanks for once again, what is an exceptional and very
> detailed answer!
>
> I think I can summarise what you said as: "can_write($socket)" tells
> me if Perl is buffering extra data, however, after that can_write
> returns true even though the OS is buffering some more data?
No, when you're using syswrite (or when you're using print with
autoflush turned on) perl never buffers data.
IO::Select->new($socket)->can_write tells you whether the Operating
System's buffers are full or not. That's all.
> ie if I write 1Mb, then 32Kb, (say) is in the OS buffer, and once
> there is only 32Kb left to write, then "can_write" starts to return
> true.
I'm not sure what you're trying to say, so let me try an example.
Let's suppose that the (capacity of the) OS buffer is 3k. You've just
called $socket->sync, so all the data that was in the buffer has already
been sent over the wire, and the OS buffer is now empty (it is still 3k
large, but nothing is in it). You write 32k of data to the socket.
This will block until 29k has been sent over the wire. The remaining 3k
will be copied out of the perl process into the OS buffer, to be
asynchronously sent over the wire.
For as long as the OS buffer is full (that is, until at least one byte
is sent over the wire, and thus removed from the OS buffer),
$sel->can_write($timeout) will return false. If you were to do
$sel->can_write(undef), or $sel->can_write(), then your perl process
will be blocked until the OS buffer is not entirely full (ie, until at
least one byte is sent over the wire).
> Two questions occur. I think I saw on here a mention of extra params
> when creating the socket to control "windows size", which I assume is
> that the OS buffer is going to size itself based on? (Perhaps...)
> Can someone please remind me what these options would be (ie I would
> like to turn down the size of my send buffer on windows 2K/XP)
This is a network programming question, not a perl question -- you'll
probably need to do certain ioctl()s ... which are, of course, system
dependent.
> Secondly, why does the write function send too much data, ie from the
> perl cookbook we see:
>
> my $n = $remote->send($buf, 0);
>
> Where $n is supposed to be the number of chars written, BUT I am
> *always* seeing $n == length($buf)
This indicates that the length of $buf (minus the amount that was sent
over the wire right away) is less than or equal to the size of the OS
buffer.
> How can I just fill up just the OS buffers and then have it set
> "$!{EWOULDBLOCK}" - my options appear to be: blocking writes, or
> non-blocking writes which accept as much data as they feel fit (I
> managed to force 100MB "into" the socket without problems or notice of
> "EWOULDBLOCK")?
>
> This seems a little odd.
Hmm, are you *sure* that your socket is non-blocking? What is the
return status of $socket->blocking(0)? If it's false, then you aren't
blocking as you think you are.
Here's a test which should work regardless of whether or not you can
successfully put the socket into nonblocking mode.
my $sel = IO::Select->new($remote);
for( 1 .. 0x1000 ) {
unless( $sel->can_write(0) ) {
print "select indicates writing would block\n";
exit;
}
my $n = syswrite($remote, "\0" x 0x10000);
if( $n != 0x10000 ) {
print "only wrote $n bytes, not ".0x10000." bytes\n";
exit;
}
}
print "wrote ".0x1000_0000." bytes without blocking.\n";
--
$;=qq qJ,krleahciPhueerarsintoitq;sub __{0 &&
my$__;s ee substr$;,$,&&++$__%$,--,1,qq;;;ee;
$__>2&&&__}$,=22+$;=~y yiy y;__ while$;;print
------------------------------
Date: 19 Feb 2003 15:59:02 -0800
From: jmcnamara@cpan.org (John McNamara)
Subject: Re: reading excel spreadsheet with perl
Message-Id: <8cceb2da.0302191559.3caf9028@posting.google.com>
rory@vivid-design.com.au (Rory) wrote:
> I have an excel spreadsheet on a linux server.
> ...
> I am finding it increasingly difficult however to write back to the
> spreadsheet with updated/new values ect.
The Spreadsheet::ParseExcel::SaveParser module allows you to read and
re-write an Excel file. It is a combination of Spreadsheet::ParseExcel
and Spreadsheet::WriteExcel and it is part of the standard
Spreadsheet::ParseExcel distribution.
Here is a example that shows some of the possibilities:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel::SaveParser;
# Open the template with SaveParser
my $parser = new Spreadsheet::ParseExcel::SaveParser;
my $template = $parser->Parse('template.xls');
my $sheet = 0;
my $row = 0;
my $col = 0;
# Get the format from the cell
my $format = $template->{Worksheet}[$sheet]
->{Cells}[$row][$col]
->{FormatNo};
# Write data to some cells
$template->AddCell(0, $row, $col, 1, $format);
$template->AddCell(0, $row+1, $col, "Hello", $format);
# The SaveParser SaveAs() method returns a reference to a
# Spreadsheet::WriteExcel object. If you wish you can then
# use this to access any of the methods that aren't
# available from the SaveParser object. If you don't need
# to do this just use SaveAs().
#
my $workbook;
{
# SaveAs generates a lot of harmless warnings about unset
# Worksheet properties. You can ignore them if you wish.
local $^W = 0;
# Rewrite the file or save as a new file
$workbook = $template->SaveAs('newfile.xls');
}
# Use Spreadsheet::WriteExcel methods
my $worksheet = ($workbook->sheets())[0];
$worksheet->write($row+2, $col, "World");
__END__
John.
--
# Useless non-use of cat
perl -peruse filename
------------------------------
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.
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 4588
***************************************