[33043] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4319 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Nov 30 09:09:18 2014

Date: Sun, 30 Nov 2014 06:09:05 -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           Sun, 30 Nov 2014     Volume: 11 Number: 4319

Today's topics:
    Re: A hash of references to arrays of references to has (Seymour J.)
    Re: A hash of references to arrays of references to has <gravitalsun@hotmail.foo>
    Re: half-blocking sockets <luca_remove@alice.it>
    Re: half-blocking sockets <gamo@telecable.es>
    Re: half-blocking sockets <hjp-usenet3@hjp.at>
    Re: How do I compare two files byte-by-byte? <see.my.sig@for.my.address>
    Re: How do I compare two files byte-by-byte? <see.my.sig@for.my.address>
    Re: How do I compare two files byte-by-byte? <rweikusat@mobileactivedefense.com>
    Re: How do I compare two files byte-by-byte? <m@rtij.nl.invlalid>
    Re: How do I compare two files byte-by-byte? <hjp-usenet3@hjp.at>
        How to write an open(GLOB, ...) wrapper? <h.b.furuseth@usit.uio.no>
    Re: How to write an open(GLOB, ...) wrapper? <gravitalsun@hotmail.foo>
    Re: How to write an open(GLOB, ...) wrapper? <h.b.furuseth@usit.uio.no>
    Re: How to write an open(GLOB, ...) wrapper? <gravitalsun@hotmail.foo>
    Re: How to write an open(GLOB, ...) wrapper? <whynot@pozharski.name>
    Re: How to write an open(GLOB, ...) wrapper? <whynot@pozharski.name>
    Re: How to write an open(GLOB, ...) wrapper? <gravitalsun@hotmail.foo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 29 Nov 2014 18:17:41 -0500
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: A hash of references to arrays of references to hashes... is there a better way?
Message-Id: <547a5415$7$fuzhry+tra$mr2ice@news.patriot.net>

In <cq-dnYfdW8zH_-_JnZ2dnUVZ57ydnZ2d@giganews.com>, on 11/23/2014
   at 02:40 PM, Robbie Hatley <see.my.sig@for.my.address> said:

>As for two files with the same name in the same directory, that's 
>not possible in any file system I know of.

VMS had the concept of version, and you could have multiple versions
of the same file in a directory. There was syntax for referring to a
specific version.

-- 
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



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

Date: Sun, 30 Nov 2014 14:00:25 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: A hash of references to arrays of references to hashes... is there a better way?
Message-Id: <m5f0su$123s$1@news.ntua.gr>

On 30/11/2014 01:17, Shmuel (Seymour J.) Metz wrote:
> In <cq-dnYfdW8zH_-_JnZ2dnUVZ57ydnZ2d@giganews.com>, on 11/23/2014
>     at 02:40 PM, Robbie Hatley <see.my.sig@for.my.address> said:
>
>> As for two files with the same name in the same directory, that's
>> not possible in any file system I know of.
>
> VMS had the concept of version, and you could have multiple versions
> of the same file in a directory. There was syntax for referring to a
> specific version.
>

at ext3/ext4/btrfs you can have many identical "files" storing their 
contents as extended attributes bind to a normal files. Can be useful 
under special requirements


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

Date: Sat, 29 Nov 2014 10:17:44 -0500
From: Luca <luca_remove@alice.it>
Subject: Re: half-blocking sockets
Message-Id: <tslew.812064$FX2.166909@fx18.iad>

On 11/27/2014 02:28 AM, Eric Pozharski wrote:
> with <Qcrdw.718130$Ub6.714083@fx20.iad> Luca wrote:
>
>> Is there a way for me to look at the $clientdata object and understand
>> if a non-blocking socket has produced some value or not?
>
> Yes.  Its name is select(2).  Perl has IO::Select readily available.
>
> p.s.  Also:
>
> 	perldoc perlipc
>
> You might appreciate it.

Thank you, Eric (and thanks to everyone else who replied). Man, is this 
stuff complicated!

So, here's a follow up question:

$socket = new IO::Socket::INET (
     LocalHost => '127.0.0.1',
     LocalPort => '1055',
     Proto => 'tcp',
     Listen => 1,
     Reuse => 1,
     Blocking => 0
) or die "Oops: $@ \n";
  :

$clientsocket = $socket->accept();
my $handle = IO::Select->new($clientsocket);

   :

while ($handle->can_read(0) ) {

   $clientdata = <$clientsocket>;

Still doesn't work. $handle->can_read(0) is undefined no matter what the 
client sends through the socket... (both client and server hang). What 
am I getting fundamentally wrong?

Thanks




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

Date: Sat, 29 Nov 2014 20:04:19 +0100
From: gamo <gamo@telecable.es>
Subject: Re: half-blocking sockets
Message-Id: <m5d5bf$jio$1@speranza.aioe.org>

El 29/11/14 a las 16:17, Luca escribió:
> On 11/27/2014 02:28 AM, Eric Pozharski wrote:
>> with <Qcrdw.718130$Ub6.714083@fx20.iad> Luca wrote:

> Still doesn't work. $handle->can_read(0) is undefined no matter what the
> client sends through the socket... (both client and server hang). What
> am I getting fundamentally wrong?

can_read could be called without parameter and returns a list. This
example works for me:

#!/usr/bin/perl -w

use IO::Select;
use IO::Socket;

$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
$sel = new IO::Select( $lsn );

while(@ready = $sel->can_read) {
     foreach $fh (@ready) {
	if($fh == $lsn) {
	    # Create a new socket
	    $new = $lsn->accept;
	    $sel->add($new);
	}
	else {
	    # Process socket
	    $data = <$fh>;
	    print $data;
	
	    # Maybe we have finished with the socket
	    if ($data=~/quit/i || $data=~/exit/i || $data=~/end/i){
		$sel->remove($fh);
		$fh->close;
	    }
	}
     }
}

__END__

-- 
http://www.telecable.es/personales/gamo/


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

Date: Sat, 29 Nov 2014 21:40:20 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: half-blocking sockets
Message-Id: <slrnm7kbpk.71k.hjp-usenet3@hrunkner.hjp.at>

On 2014-11-29 15:17, Luca <luca_remove@alice.it> wrote:
> On 11/27/2014 02:28 AM, Eric Pozharski wrote:
>> with <Qcrdw.718130$Ub6.714083@fx20.iad> Luca wrote:
>>
>>> Is there a way for me to look at the $clientdata object and understand
>>> if a non-blocking socket has produced some value or not?
>>
>> Yes.  Its name is select(2).  Perl has IO::Select readily available.
>>
>> p.s.  Also:
>>
>> 	perldoc perlipc
>>
>> You might appreciate it.
>
> Thank you, Eric (and thanks to everyone else who replied). Man, is this 
> stuff complicated!
>
> So, here's a follow up question:

It's better to post a complete script than just fragments. It is
possible that you already handled the problem I'm pointing out below in
the parts you didn't post.


> $socket = new IO::Socket::INET (
>      LocalHost => '127.0.0.1',
>      LocalPort => '1055',
>      Proto => 'tcp',
>      Listen => 1,
>      Reuse => 1,
>      Blocking => 0
> ) or die "Oops: $@ \n";

You have a non-blocking socket here ...

>  :
>
> $clientsocket = $socket->accept();

 ... and you do a single unconditional accept() here. Since the socket is
non-blocking it will not wait for a client to connect. Unless a client
happens to connect excactly between the creation of the socket and the
accept, the accept will fail and you won't have a socket to read from.

You either need to make your listen socket blocking or use select to
check whether you can call accept on it.

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Fri, 28 Nov 2014 06:44:10 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: How do I compare two files byte-by-byte?
Message-Id: <PYadnfb7z7umF-XJnZ2dnUVZ572dnZ2d@giganews.com>


On 11/24/2014 03:35 AM, Martijn Lievaart wrote:

 > > ... According to "Programming Perl", Perl's "read()" uses a buffer
 > > between itself and the operating system's "read()" ...
 >
 > True, but it is still slow as you compare byte by byte. Much better to
 > compare large chunks, waaaay faster. Perl eq operator will do this nicely
 > for you, it can compare megabytes efficiently.

I tried comparing 2 identical files of 25MB each. Since they were
identical, the comparison went all the way to the end. I estimated
the time required at about 11 seconds. But during that 11 seconds
there was almost ZERO disk activity. The IDE light on my computer
flicked briefly twice, once at the 0-second mark, and once at about
the 6-second mark. The real-time disk performance graph (this was
on Windows 8.1) also showed only two tiny blips at 0% and 50%.
This is telling me that Perl pulled in half of each file as one huge
chunk at the beginning, then the other half about 6 seconds later.

The 11 seconds, therefore, was all moving and comparing bytes.
5 million comparisons per second isn't TOO bad, but with CPU speed
at over 2 BILLION cycles per second, it's about 2% efficiency.

 > ... Normally I would say, make it correct first, optimize later.
 > But in this case...

Yep, this is an atypical case, involving handing amounts of data
ranging from 0 bytes all the way up to 4.7GB (the size of a DVD
iso file, which is about the biggest thing I'm likely to ever have
on MY computers) and potentially having to compare EVERY SINGLE
BYTE. So yep, some optimizing has to be done right now, because
with a 1-byte buffer it's way too slow.

I think I'll start by trying a 4096-byte buffer (one HD allocation
unit).  If that's still too slow, I'll try larger.

 > ... both "\r\n" and "\n" are translated in text mode to "\n" ...

This script is intended for use on files of all types,
so I'm opening using:

    open ($filehandle1, "< :raw", $filename1)
       or die "Can't open $filename1 for reading. $!\n";
    open ($filehandle2, "< :raw", $filename2)
       or die "Can't open $filename2 for reading. $!\n";

 > ... calculate a checksum ...

I just don't see how that's going to help in this application.
Checksums still involve running calculations on every single
byte. If you're going to compare every single byte anyway,
no point. This "dedup" script I'm writing will be used mostly in
situations where there are no pre-existing checksums, and I don't
have the inclination to create them, so it will be a "brute force
compare" script with no checksums used at all.

The hard work is already done. Now I just need to speed-up the
compare, and add the code that alerts the user that duplicates
were found as asks what to do, and the code that deletes
duplicate files. In progress.

-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'



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

Date: Fri, 28 Nov 2014 06:49:52 -0800
From: Robbie Hatley <see.my.sig@for.my.address>
Subject: Re: How do I compare two files byte-by-byte?
Message-Id: <zcidnfyF77INFuXJnZ2dnUVZ57ydnZ2d@giganews.com>


On 11/24/2014 07:08 AM, Rainer Weikusat wrote:

 > The speed difference is nevertheless dramatical. I ran the code below
 > with 8K input files filled with zeroes and the 'blocks' variant ran at
 > about 171 times the speed of the other despite the code isn't more
 > complicated...

Yep, that jibes with what I calculated, that I was operating
at about 2% efficiency. I'll try a 4096-byte buffer and see
what that does to the speed of comparing two 25MB files.
(Was taking 11 seconds using a 1-byte buffer.)

-- 
Cheers,
Robbie Hatley
Midway City, CA, USA
perl -le 'print "\154o\156e\167o\154f\100w\145ll\56c\157m"'
http://www.well.com/user/lonewolf/


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

Date: Fri, 28 Nov 2014 16:30:06 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: How do I compare two files byte-by-byte?
Message-Id: <871tonl241.fsf@doppelsaurus.mobileactivedefense.com>

Robbie Hatley <see.my.sig@for.my.address> writes:
> On 11/24/2014 07:08 AM, Rainer Weikusat wrote:
>
>> The speed difference is nevertheless dramatical. I ran the code below
>> with 8K input files filled with zeroes and the 'blocks' variant ran at
>> about 171 times the speed of the other despite the code isn't more
>> complicated...
>
> Yep, that jibes with what I calculated, that I was operating
> at about 2% efficiency. I'll try a 4096-byte buffer and see
> what that does to the speed of comparing two 25MB files.
> (Was taking 11 seconds using a 1-byte buffer.)

Below is the result for comparing identical 25M files.

rw@doppelsaurus]/tmp#perl a.pl f0 f0
Benchmark: running blocks, bytes, mmap for at least 20 CPU seconds...
    blocks: 21 wallclock secs ( 4.24 usr + 16.88 sys = 21.12 CPU) @ 118.70/s (n=2507)
     bytes: 24 wallclock secs (23.81 usr +  0.05 sys = 23.86 CPU) @  0.17/s (n=4)
      mmap: 20 wallclock secs ( 9.94 usr + 10.92 sys = 20.86 CPU) @ 80.49/s (n=1679)

The test was done using a slightly changed script as I thought it would
be interesting to have some numbers for mmap, too.

NB: The fairly bad result for mmap may be due to bad interactions
between the perl runtime and the mmap interface (eg, according to some
tests I made, the 'mmap' sub stats each filehandle thee times) and
less-than-intelligent implementation choices of the Sys::Mmap version I
was using (it seems to do an additional mmap per user-requested mmap in
order to determine 'the system page size', something it doesn't need to
do at all) but I didn't investigate this beyond looking at strace
output.

--------------
use Benchmark;
use Sys::Mmap;

timethese(-20,
	  {
	   blocks => sub {
	      my ($fh0, $fh1, $d0, $d1);

	      open($fh0, '<', $ARGV[0]);
	      open($fh1, '<', $ARGV[1]);

	      while (sysread($fh0, $d0, 65536)) {
	  	  sysread($fh1, $d1, 65536);
	  	  last unless $d0 eq $d1;
	      }
	  },

	  bytes => sub {
	      my ($fh0, $fh1, $b0, $b1);

	      open($fh0, '<', $ARGV[0]);
	      open($fh1, '<', $ARGV[1]);

	      while (read($fh0, $b0, 1)) {
	  	  read($fh1, $b1, 1);
	  	  last unless $b0 eq $b1;
	      }
	  },

	   mmap => sub {
	       my ($fh0, $fh1, $d0, $d1, $rc);

	       open($fh0, '<', $ARGV[0]);
	       mmap($d0, 0, PROT_READ, MAP_SHARED, $fh0);
	       
	       open($fh1, '<', $ARGV[1]);
	       mmap($d1, 0, PROT_READ, MAP_SHARED, $fh1);

	       $rc = $d0 eq $d1;

	       munmap($d0);
	       munmap($d1);

	       return $rc;
	  }
	  });


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

Date: Sat, 29 Nov 2014 10:13:50 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: How do I compare two files byte-by-byte?
Message-Id: <ebfokb-ia3.ln1@news.rtij.nl>

On Fri, 28 Nov 2014 06:44:10 -0800, Robbie Hatley wrote:

> On 11/24/2014 03:35 AM, Martijn Lievaart wrote:
> 
>  > ... Normally I would say, make it correct first, optimize later.
>  > But in this case...
> 
> Yep, this is an atypical case, involving handing amounts of data ranging
> from 0 bytes all the way up to 4.7GB (the size of a DVD iso file, which
> is about the biggest thing I'm likely to ever have on MY computers) and
> potentially having to compare EVERY SINGLE BYTE. So yep, some optimizing
> has to be done right now, because with a 1-byte buffer it's way too
> slow.
> 
> I think I'll start by trying a 4096-byte buffer (one HD allocation
> unit).  If that's still too slow, I'll try larger.

Go for larger directly. Start at 1MB.

> 
>  > ... both "\r\n" and "\n" are translated in text mode to "\n" ...
> 
> This script is intended for use on files of all types,
> so I'm opening using:
> 
>     open ($filehandle1, "< :raw", $filename1)
>        or die "Can't open $filename1 for reading. $!\n";
>     open ($filehandle2, "< :raw", $filename2)
>        or die "Can't open $filename2 for reading. $!\n";
> 
>  > ... calculate a checksum ...
> 
> I just don't see how that's going to help in this application. Checksums
> still involve running calculations on every single byte. If you're going
> to compare every single byte anyway,
> no point. This "dedup" script I'm writing will be used mostly in
> situations where there are no pre-existing checksums, and I don't have
> the inclination to create them, so it will be a "brute force compare"
> script with no checksums used at all.

As I said already, if multiple files have the same size, it can be much 
faster.

M4


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

Date: Sat, 29 Nov 2014 21:22:34 +0100
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: How do I compare two files byte-by-byte?
Message-Id: <slrnm7kaoa.71k.hjp-usenet3@hrunkner.hjp.at>

On 2014-11-28 14:44, Robbie Hatley <see.my.sig@for.my.address> wrote:
> On 11/24/2014 03:35 AM, Martijn Lievaart wrote:
> > > ... According to "Programming Perl", Perl's "read()" uses a buffer
> > > between itself and the operating system's "read()" ...
> >
> > True, but it is still slow as you compare byte by byte. Much better to
> > compare large chunks, waaaay faster. Perl eq operator will do this nicely
> > for you, it can compare megabytes efficiently.
>
> I tried comparing 2 identical files of 25MB each. Since they were
> identical, the comparison went all the way to the end. I estimated
> the time required at about 11 seconds. But during that 11 seconds
> there was almost ZERO disk activity. The IDE light on my computer
> flicked briefly twice, once at the 0-second mark, and once at about
> the 6-second mark. The real-time disk performance graph (this was
> on Windows 8.1) also showed only two tiny blips at 0% and 50%.
> This is telling me that Perl pulled in half of each file as one huge
> chunk at the beginning, then the other half about 6 seconds later.

More likely that was Windows. Or both files were already in cache and
the disk accesses were unrelated.


> The 11 seconds, therefore, was all moving and comparing bytes.
> 5 million comparisons per second isn't TOO bad, but with CPU speed
> at over 2 BILLION cycles per second, it's about 2% efficiency.

It's even worse. Rainer's benchmark shows a speedup of over 700 when
going from single bytes to 64k blocks. So we are talking more like 0.1%
efficiency here.


> > ... calculate a checksum ...
>
> I just don't see how that's going to help in this application.
> Checksums still involve running calculations on every single
> byte. If you're going to compare every single byte anyway,
> no point. This "dedup" script I'm writing will be used mostly in
> situations where there are no pre-existing checksums, and I don't
> have the inclination to create them, so it will be a "brute force
> compare" script with no checksums used at all.

Assume that you have 10 files of 4.7GB which are identical in the first
4 GB (so two compare 2 of them you need to read 4GB of each). 

So you compare f1 to f2 (8 GB), f1 to f3 (another 8 GB), ..., 
f1 to f10 (another 8 GB), f2 to f3 (another 8 GB), ...  and finally
f9 to f10 (another 8 GB). So you have to read and process 45 * 8 GB =
360 GB of data. 

If you compute the checksum of each file you only have to read and
process 10 * 4.7 GB = 47 GB of data. You still have to do 45 comparisons
of the checksums, but that's negligible.

(To be honest, the case that you have a lot of large files which differ
only at the end is probably rare and you shouldn't optimize for it at
the expense of the more common case where the files differ much earlier. 
The checksum technique also covers identical files, but there are other
ways to cover that.)

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


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

Date: Fri, 28 Nov 2014 15:49:15 +0100
From: Hallvard Breien Furuseth <h.b.furuseth@usit.uio.no>
Subject: How to write an open(GLOB, ...) wrapper?
Message-Id: <hbf.20141128kpjv@bombur.uio.no>

I know the program should have been using $filehandles instead
of GLOBs, but such a rewrite would not be amusing at this time.

This seems to work:
    sub myopen {
        my($fh, $name) = @_;
        open(*$fh, "<", $name);
    }
    myopen *FOO, "/etc/hosts" or die;

But it also works if open() uses $fh, \*fh, *{$fh}{IO}, and/or
if I pass myopen \*FOO, *FOO{IO}, or (without strict refs) FOO.
WTF...

So, which do I use and what's going on when I use it?  I don't
trust "seems to work".  Several myopen()ed files will be open at
the same time, maybe with the same filename and/or local()ized
glob name.  Leaves room for screwing up if I use globs wrong.

-- 
Hallvard


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

Date: Fri, 28 Nov 2014 17:12:05 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <m5a3b6$233p$1@news.ntua.gr>

On 28/11/2014 16:49, Hallvard Breien Furuseth wrote:
> I know the program should have been using $filehandles instead
> of GLOBs, but such a rewrite would not be amusing at this time.
>
> This seems to work:
>      sub myopen {
>          my($fh, $name) = @_;
>          open(*$fh, "<", $name);
>      }
>      myopen *FOO, "/etc/hosts" or die;
>
> But it also works if open() uses $fh, \*fh, *{$fh}{IO}, and/or
> if I pass myopen \*FOO, *FOO{IO}, or (without strict refs) FOO.
> WTF...
>
> So, which do I use and what's going on when I use it?  I don't
> trust "seems to work".  Several myopen()ed files will be open at
> the same time, maybe with the same filename and/or local()ized
> glob name.  Leaves room for screwing up if I use globs wrong.
>


#  all the following are equal good, select any you like !




open ( FileID, 'E:\Work\data.txt') || die "$^E\n";

print_file_with_filehandle_reference(\*file)
sub print_file_with_filehandle_reference {
local  $var = shift;
while(<$var>) { print }
close  $var || die "$^E\n" }


print_file_with_filehandle(*file)
sub print_file_with_filehandle {
local *FILEID = \$_[0];
while(<FILEID>) { print }
close  FILEID || die "$^E\n" }


print_file_with_variable(file)
sub print_file_with_variable {
local  *var = \shift;
while(<$var>) { print }
close  $var || die "$^E\n" }

# by filename

print_file('C:\Work\Sync\Readme.txt');
sub print_file {
local  *FileID = \undef;
open  ( FileID, "<$_[0]"  ) || die "Could not read file $_[0] because 
$^E\n";
while (<FileID>) { print }
close   FileID || die "Could not close file $_[0] because $^E\n" }



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

Date: Fri, 28 Nov 2014 16:21:49 +0100
From: Hallvard Breien Furuseth <h.b.furuseth@usit.uio.no>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <hbf.20141128lanu@bombur.uio.no>

George Mpouras <gravitalsun@hotmail.foo> writes:
> #  all the following are equal good, select any you like !
> (...)

Thanks, but I see no open() wrapper there.  I guess I should
have put the question in the article body as well:

How to write an open(GLOB, ...) wrapper?

-- 
Hallvard


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

Date: Fri, 28 Nov 2014 17:37:11 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <m5a4q8$26hr$1@news.ntua.gr>

On 28/11/2014 17:21, Hallvard Breien Furuseth wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>> #  all the following are equal good, select any you like !
>> (...)
>
> Thanks, but I see no open() wrapper there.  I guess I should
> have put the question in the article body as well:
>
> How to write an open(GLOB, ...) wrapper?
>


# do somethink like that

our %DBFH;
my $fh = Give_me_a_filehandle( 'somefile.txt' );

Do_something_with_this_filenandle( $fh );


# At Give_me_a_filehandle do something like


sub Give_me_a_filehandle
{
my $file = shift;


	if (! exists $DBFH{$file})
	{
	my $fh = ..fh ..
	push @{$DBFH{$file} , $fh
	return $fh
	}
	else
	{
	using the fileno delete all keys from the
         @{$DBFH{$file}  array that the fileno is undef
	push @{$DBFH{$file} , $fh
	return $fh
	}
}


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

Date: Sat, 29 Nov 2014 15:31:38 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <slrnm7jilq.hr9.whynot@orphan.zombinet>

with <hbf.20141128kpjv@bombur.uio.no> Hallvard Breien Furuseth wrote:

> I know the program should have been using $filehandles instead
> of GLOBs, but such a rewrite would not be amusing at this time.

I think you have a slight bit of misunderstanding.  In Perl filehandles
are called GLOBs:

	% perl -wle 'print ref \*STDOUT'
	GLOB

Before lexical filehandles where made they were global only (clearly,
that's why they are called GLOBs).  But!  Being them global GLOBs or
lexical GLOBs doesn't change The Truth -- deep inside (at kernel level)
they are file descriptors.  That is, they are set of integers that
identify open files when getting down to kernel.

(N.B.  AAMOF, it's possible to operate on GLOB that doesn't have a
filedescriptor.)

> This seems to work:
>     sub myopen {
>         my($fh, $name) = @_;
>         open(*$fh, "<", $name);
>     }
>     myopen *FOO, "/etc/hosts" or die;
>
> But it also works if open() uses $fh, \*fh, *{$fh}{IO}, and/or
> if I pass myopen \*FOO, *FOO{IO}, or (without strict refs) FOO.
> WTF...

(I'm pulling out this gun for the first time.)  What is you're trying to
do?  Your global GLOBs are autovivified *outside* of myopen().  You've
achieved literally nothing.  You have to understand one thing:  global
GLOBs ain't no bad because they're GLOBs but because they are global.
Global variables are bad design -- end of story.  If you're fine with
slapping more complexity on bad design, then you're free to shoot
yourself in foot any way you want.  What George suggested is a shooter
good enough, you don't even need 'our' -- put that in a module, it will
be much easier to cheat yourself.

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Sat, 29 Nov 2014 15:34:08 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <slrnm7jiqg.hr9.whynot@orphan.zombinet>

with <m5a4q8$26hr$1@news.ntua.gr> George Mpouras wrote:

*SKIP*
That's what CLPM-people were trying to say you on multiple occasions
over a year now -- Make Your Code Readable (For Others)!  This one
doesn't compile.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Sat, 29 Nov 2014 21:33:17 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: How to write an open(GLOB, ...) wrapper?
Message-Id: <m5d721$1a9b$1@news.ntua.gr>

On 29/11/2014 15:34, Eric Pozharski wrote:
> with <m5a4q8$26hr$1@news.ntua.gr> George Mpouras wrote:
>
> *SKIP*
> That's what CLPM-people were trying to say you on multiple occasions
> over a year now -- Make Your Code Readable (For Others)!  This one
> doesn't compile.
>
its pseudocode in case you did not notice; whatever. why dont add a 
blacklist record and move on ;


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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 4319
***************************************


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