[28278] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 9642 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 24 06:05:41 2006

Date: Thu, 24 Aug 2006 03:05:09 -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           Thu, 24 Aug 2006     Volume: 10 Number: 9642

Today's topics:
    Re: Calling another cgi program using CGI.pm <youknows@gmail.com>
    Re: Calling another cgi program using CGI.pm <youknows@gmail.com>
    Re: how can I find the driver list in perl <bik.mido@tiscalinet.it>
    Re: how can I find the driver list in perl <odperry@gmail.com>
    Re: IO::Select::select() says no readable data even if  jari.eskelinen@iki.fi
    Re: IO::Select::select() says no readable data even if  vparseval@gmail.com
    Re: IO::Select::select() says no readable data even if  <benmorrow@tiscali.co.uk>
    Re: IO::Select::select() says no readable data even if  anno4000@radom.zrz.tu-berlin.de
    Re: IO::Select::select() says no readable data even if  <David.Squire@no.spam.from.here.au>
    Re: IO::Select::select() says no readable data even if  jari.eskelinen@iki.fi
    Re: IO::Select::select() says no readable data even if  <benmorrow@tiscali.co.uk>
    Re: IO::Select::select() says no readable data even if  <David.Squire@no.spam.from.here.au>
    Re: IO::Select::select() says no readable data even if  <benmorrow@tiscali.co.uk>
    Re: Permission denied trying to execute shell commands  <youknows@gmail.com>
        readdir conflict problem? <zhushenli@gmail.com>
    Re: readdir conflict problem? <David.Squire@no.spam.from.here.au>
    Re: readdir conflict problem? anno4000@radom.zrz.tu-berlin.de
        regular expression variables under debugger <wlcna@nospam.com>
    Re: substr taking time <youknows@gmail.com>
    Re: substr taking time <someone@example.com>
    Re: variable with empty space <benmorrow@tiscali.co.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 24 Aug 2006 01:02:23 -0700
From: "alpha_beta_release" <youknows@gmail.com>
Subject: Re: Calling another cgi program using CGI.pm
Message-Id: <1156406543.245226.239280@i42g2000cwa.googlegroups.com>

or use CGI.pm,

-----------------------------
use CGI;

my $page = new CGI;

if (validate_ok($page->param('user'), $page->param('password')) {
    $page->redirect( 'http://tosomewhere.cgi?user='$page->param('user')
);
}

# else, print login form

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

Tad McClellan wrote:
> [ Please do not top-post! }
>
>
> dmedhora@gmail.com <dmedhora@gmail.com> wrote:
>
> > Lets say if the index.pl program above validates the user/password
> > successfully then  how do I continue to another html page?
> > say, continue.pl
>
>
> If continue.pl accepts GET requests, you can issue a "redirect":
>
>    print "Location: http://www.yoursite.com/cgi-bin/continue.pl\n\n";
>
> or some such.
>
>
>
> [ snip TOFU]
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas



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

Date: 24 Aug 2006 02:17:24 -0700
From: "alpha_beta_release" <youknows@gmail.com>
Subject: Re: Calling another cgi program using CGI.pm
Message-Id: <1156411044.478109.299370@75g2000cwc.googlegroups.com>

or use CGI.pm,

-----------------------------
use CGI;

my $page = new CGI;

# assuming validate_ok() is the function to validate your user

if (validate_ok($page->param('user'), $page->param('password')) {
   print $page->redirect(
'http://tosomewhere.cgi?user='.$page->param('user'));
}

# else, print login form

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

Tad McClellan wrote:
> [ Please do not top-post! }
>
>
> dmedhora@gmail.com <dmedhora@gmail.com> wrote:
>
> > Lets say if the index.pl program above validates the user/password
> > successfully then  how do I continue to another html page?
> > say, continue.pl
>
>
> If continue.pl accepts GET requests, you can issue a "redirect":
>
>    print "Location: http://www.yoursite.com/cgi-bin/continue.pl\n\n";
>
> or some such.
>
>
>
> [ snip TOFU]
>
> --
>     Tad McClellan                          SGML consulting
>     tadmc@augustmail.com                   Perl programming
>     Fort Worth, Texas



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

Date: 24 Aug 2006 09:32:52 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: how can I find the driver list in perl
Message-Id: <9ukqe290i21id902norfs6pvai8f4sfuch@4ax.com>

On 23 Aug 2006 17:38:05 -0700, "Matt Garrish" <mgarrish@gmail.com>
wrote:

>> > > What command should I use IN THE PERL LANGUAGE in order  to get the
>> > > list of available HD in my computer.
>> >
>> > Simple.  There is no such command in the Perl language.
[snip]
>> if you lack the knowledge that is required to answer such a question,
>> means that you don't know enough perl , please don't answer or save us
>> your low enligsh that belong to the red neck.

Thank you for quoting this. Since I've killfiled the OP since the
first offensive post, I would have risked to miss this hilarious piece
of prose. Haha: "you don't know enough perl" (Anno!!), "*your* low
enligsh"...


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 24 Aug 2006 00:54:06 -0700
From: "Over G" <odperry@gmail.com>
Subject: Re: how can I find the driver list in perl
Message-Id: <1156406046.627368.253020@i3g2000cwc.googlegroups.com>

Its amazing how much time you people spend  to answer this long and
lengthy answers, this all discussion could have ended, if one would
have just answer it simple and clear, instead, people like you prefer
to go line by line on my paragraph, tell me how much I am wrong using
this line instead of that line,
Is it Perl group?
Or English literature group>?

thanks
O



Matt Garrish wrote:
> Over G wrote:
>
> > anno4000@radom.zrz.tu-berlin.de wrote:
> > > Over G <odperry@gmail.com> wrote in comp.lang.perl.misc:
> > > >
> > > > Tad McClellan wrote:
> > > > > Over G <odperry@gmail.com> wrote:
> > >
> > > > > You asked a question that has nothing to do with Perl programming.
> > > > >
> > > > > This is the Perl programming newsgroup you know...
> > > > I will rephrase :
> > > >
> > > > What command should I use IN THE PERL LANGUAGE in order  to get the
> > > > list of available HD in my computer.
> > > > thanks.
> > >
> > > Simple.  There is no such command in the Perl language.
> > >
> > > Now piss off.
> >
> > excuse me bu t who are you to tell me to piss off. ?
> >
>
> He is what we here call a regular poster. You are not, and you're not
> fooling anyone.
>
> > if you lack the knowledge that is required to answer such a question,
> > means that you don't know enough perl , please don't answer or save us
> > your low enligsh that belong to the red neck.
> >
>
> There are so many grammatical problems with that "paragraph" it's not
> even funny. If your English is poor, which yours is, you should try
> using a spellcheck program before posting. If you could write English
> well, your paragraph above might make more sense as follows: (my
> comments in brackets)
>
> If you lack the knowledge to answer this question, it must mean you
> don't know very much about the language [think what that says about
> you!]. If that's the case, please don't answer, or at least spare the
> world your red-neck English.
>
> What you wrote, to someone who has spoken English his whole life, is
> painful to read. What you wrote in your original post was equally
> unclear. Although open to some interpretation, most people would guess
> you were asking for what you were asking for: a list of drivers. A list
> of drivers is not and never will be the same as a list of drives.
> That's your problem for being unclear, not anyone else's.
> 
> Matt



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

Date: 24 Aug 2006 00:20:03 -0700
From: jari.eskelinen@iki.fi
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <1156404003.430511.84130@i3g2000cwc.googlegroups.com>

> Etc. It is cleary seen that select() (or in this case can_read())
> indicates that there are nothing more to read, even when there are.

Found solution. When using $socket->recv() instead of <$socket>,
it won't mess up with select(). So I wrote subroutine which reads
socket
until newline:

sub recvSocket {
    my ($socket) = @_;

    my $buffer;
    my $c;
    while ($socket->recv($c, 1) && $c ne "\n") {
        $buffer .= $c;
    }
    $buffer .= "\n";

    return $buffer;
}

Wham, everything works. Still, I cannot but wonder if there is bug in
IO::Socket / IO::Select under Win32, or is this supposed to work like
this... Cygwin's perl did also the same thing when I tried.

Best regards,
  Jari Eskelinen



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

Date: 24 Aug 2006 00:49:03 -0700
From: vparseval@gmail.com
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <1156405743.428797.226870@i3g2000cwc.googlegroups.com>

jari.eskelinen@iki.fi wrote:

> > Etc. It is cleary seen that select() (or in this case can_read())
> > indicates that there are nothing more to read, even when there are.
>
> Found solution. When using $socket->recv() instead of <$socket>,
> it won't mess up with select(). So I wrote subroutine which reads
> socket
> until newline:
>
> sub recvSocket {
>     my ($socket) = @_;
>
>     my $buffer;
>     my $c;
>     while ($socket->recv($c, 1) && $c ne "\n") {
>         $buffer .= $c;
>     }
>     $buffer .= "\n";
>
>     return $buffer;
> }
>
> Wham, everything works. Still, I cannot but wonder if there is bug in
> IO::Socket / IO::Select under Win32, or is this supposed to work like
> this... Cygwin's perl did also the same thing when I tried.

This is not surprising. You said you were originally using line-based
reading in order to avoid buffering-issues. However, line-based
communication is the very thing that will expose your program to
these issues.

When you read line-wise, select() may not know that there is more
data to read. On the first line-read, perl's IO-layer probably reads a
bigger chunk than just the first line and keeps it in an internal
buffer.

This is what happens when you are using fread(3) in C on a FILE
struct which contains such a buffer in order to avoid doing too many
read(2) system-calls.

select(2) on the other hand acts on the underlying file-descriptor and
does not take  into account what is left in the buffer.

Even though the perldocs explicitely state that select() should not be
mixed with line-wise processing, there is a dim chance that you could
still make it work by slurping into an array instead of reading just
one line. Instead of:

>   my $buffer = <$rsock>;
>   print "Client said: $buffer\n";

    my @buffer = <$rsock>;
    print "Client said: ", join '', @buffer;

This may still fail when there were, say, only one and a half lines
sent from the client. The half line will then remain in the buffer
waiting for the other half (with the terminating newline) to arrive.

The correct solution is to use sysread() or -- as you did -- recv()
which may even end up doing the same system-calls depending
on the platform.

Cheers,
Tassilo

--
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);



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

Date: Thu, 24 Aug 2006 08:56:45 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <tm20s3-gk9.ln1@osiris.mauzo.dyndns.org>


Quoth jari.eskelinen@iki.fi:
> 
> I am using line-based communication in order to keep
> reading and writing to sockets easy and to avoid buffering
> problems. However, buffering problems I have:
> 
> When I write two lines from server to socket and do
> IO::Select::select (or can_read()) in client, select()
> returns handle that can be read. I read first line from
> socket using <$socket> and then select() again. Select should
> return again handle because there are unread second line.
> However, it wil not return anything. If I read from socket
> despite of select()'s reutrn, I get second line just like
> expected.

As you said, you have buffering problems. select(2) operates on the
low-level filedescriptor (or its analogue under Win32); when you use
<> Perl reads a whole bufferful of data and then returns only the first
line. There really is nothing to read from the raw socket: the next line
is already read into the buffer.

With 5.8 you ought to be able to use <> in an unbuffered fashion by
pushing the :unix PerlIO layer; I haven't tried it though. It may be
very inefficient: a better solution would be to use sysread/syswrite and
do the buffering yourself.

> are data

:) It's nice to see someone getting it right...

Some unrelated comments:

> --------- server.pl -----------
> #!/usr/bin/perl -w

You want
    use warnings;
nowadays, instead of -w.

> use strict;
> use IO::Socket;
> use IO::Select;
> 
> my $socket = new IO::Socket::INET(
>     LocalAddr=>'localhost',
>     LocalPort=>'1234',
>     Proto=>'TCP',
>     Listen =>1);
> binmode($socket);
> $socket->autoflush(1);
> 
> my $select = new IO::Select();
> $select->add($socket);
> 
> while (1) {
>     my @readable = $select->can_read(0);
                                       ^^
This, plus the sleep below, means you are polling for data (trying again
and again at regular intervals). This is very unfriendly to other
processes on the system: much better is to specify infinite timeout
(can_read with no argument) and let the system wake you up when
something happens.

>     foreach my $rsock (@readable) {
>         if ($rsock == $socket) {
>             my $ns = $rsock->accept();
>             binmode($ns);
>             $ns->autoflush(1);
>             $select->add($ns);
>             print {$ns} "Hello there, I am your server.\r\n";
>             print {$ns} "Ready to obey?.\r\n";
>         } elsif (eof($rsock)) {

Personally I wouldn't use eof. I have no idea if it works reliably on
sockets, and in any case I don't think it plays with sysread/syswrite/
select at all. Detect EOF by waiting for sysread to return 0.

>             $select->remove($rsock);
>             close($rsock);
>         } else {
>             my $buffer = <$rsock>;
>             print "Client said: $buffer\n";
>         }
>     }
> 
>     sleep(1);
> }
> ----------------------------------

Ben

-- 
   Razors pain you / Rivers are damp
   Acids stain you / And drugs cause cramp.                    [Dorothy Parker]
Guns aren't lawful / Nooses give
  Gas smells awful / You might as well live.            benmorrow@tiscali.co.uk


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

Date: 24 Aug 2006 08:43:59 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <4l576fFa8peU1@news.dfncis.de>

Ben Morrow  <benmorrow@tiscali.co.uk> wrote in comp.lang.perl.misc:
> Quoth jari.eskelinen@iki.fi:

> > are data
> 
> :) It's nice to see someone getting it right...

Are you saying it is always wrong to use "data" with a singular verb?

My understanding is that in English "data" can mean a collection of
individual facts, in which case it is plural.  It can also mean a body
of facts, synonymous with "information" and as such is construed singular.

In Latin "data" is of course unambiguously plural.

Anno


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

Date: Thu, 24 Aug 2006 10:06:52 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <ecjq7c$9d3$1@gemini.csx.cam.ac.uk>

anno4000@radom.zrz.tu-berlin.de wrote:
> Ben Morrow  <benmorrow@tiscali.co.uk> wrote in comp.lang.perl.misc:
>> Quoth jari.eskelinen@iki.fi:
> 
>>> are data
>> :) It's nice to see someone getting it right...
> 
> Are you saying it is always wrong to use "data" with a singular verb?
> 
> My understanding is that in English "data" can mean a collection of
> individual facts, in which case it is plural.  It can also mean a body
> of facts, synonymous with "information" and as such is construed singular.
> 
> In Latin "data" is of course unambiguously plural.
> 

Pedants will say that even in English "data" is always plural, and even 
when referring to a collection it should be "the data are". When 
referring to a single item they would have us say "the datum is".

I, however, am of the descriptive rather than prescriptive school [1], 
and in modern English usage "the data is" is perfectly acceptable.


DS


[1] Still, it's worth knowing what prescriptivists want, so that you can 
tailor your responses should one be interviewing you for a job :)


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

Date: 24 Aug 2006 02:11:54 -0700
From: jari.eskelinen@iki.fi
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <1156410714.368639.159650@m79g2000cwm.googlegroups.com>

> <> Perl reads a whole bufferful of data and then returns only the first
> line. There really is nothing to read from the raw socket: the next line
> is already read into the buffer.

Thank you very much for your information and thanks for Tassilo too.
Now I understand why this work like it does and undestanding something
is more valuable than just solving the problem.

> This, plus the sleep below, means you are polling for data (trying again
> and again at regular intervals). This is very unfriendly to other
> processes on the system: much better is to specify infinite timeout
> (can_read with no argument) and let the system wake you up when
> something happens.

Yes, I know that polling does keep system busy and that's why I am
sleep(1)'ing with every iteration of loop. My server program (example
code was just a fraction) does much event based job (polls also
filesystem, calculates some things and does various things according
situations) and socket communications is only a tiny bit. So I loop my
program infinitely, sleeping a while between iterations and doing
polling of sockets etc. Server just can't halt when waiting data from
socket - it must work all time. Same goes to client - it is Perl/GTK2
program which cannot halt when waiting data from server, otherwise user
interface will also halt.

My first idea was to do polling like described above. I am not very
familiar with this kind of programmin so I do not know if there would
be much better solutions. Perhaps using threads, event modules or
something like that. Infinite loop polling seemed to be most easiest to
implement so I went that way. If there are much better ways however,
please point me to the right direction (threading...?).

> Personally I wouldn't use eof. I have no idea if it works reliably on
> sockets, and in any case I don't think it plays with sysread/syswrite/
> select at all. Detect EOF by waiting for sysread to return 0.

Just saw that in some socket examples so I adopted habit of using eof.
I'll switch to sysread, thanks for pointing it out.

Best regards,
  Jari Eskelinen



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

Date: Thu, 24 Aug 2006 10:06:31 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <np60s3-ela.ln1@osiris.mauzo.dyndns.org>


Quoth anno4000@radom.zrz.tu-berlin.de:
> Ben Morrow  <benmorrow@tiscali.co.uk> wrote in comp.lang.perl.misc:
> > Quoth jari.eskelinen@iki.fi:
> 
> > > are data
> > 
> > :) It's nice to see someone getting it right...
> 
> Are you saying it is always wrong to use "data" with a singular verb?
> 
> My understanding is that in English "data" can mean a collection of
> individual facts, in which case it is plural.  It can also mean a body
> of facts, synonymous with "information" and as such is construed singular.

Traditionally, 'data' was always plural, a straight steal from Latin. It
has become common practice to use it as a singular (continuous) noun,
and this in turn has become one of the things people who complain about
grammar complain about :). In particular, in a statistical/whatever
context, 'The data suggests <foo>' is definitely wrong.

However, when one is using the pipe analogy of data flowing from one
place to another, it is very hard to resist using a continuous noun, so
I would say usage has changed and in some cases the singular is correct.
After all, we're dealing with rather more than just a few sets of
numbers nowadays, so a continuous noun is really more appropriate.

I have noticed in the past that Damian at least always uses 'data' as a
plural...

Ben

-- 
I have two words that are going to make all your troubles go away.
"Miniature". "Golf".
                                                  [benmorrow@tiscali.co.uk]


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

Date: Thu, 24 Aug 2006 10:17:49 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <ecjqrt$aek$1@gemini.csx.cam.ac.uk>

Ben Morrow wrote:

> I have noticed in the past that Damian at least always uses 'data' as a
> plural...

You are talking about the man who wrote Lingua::Romana::Perligata :)


DS


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

Date: Thu, 24 Aug 2006 10:41:12 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: IO::Select::select() says no readable data even if there are
Message-Id: <oq80s3-iua.ln1@osiris.mauzo.dyndns.org>

[please attribute quotations]

Quoth jari.eskelinen@iki.fi:
> <benmorrow@tiscali.co.uk> wrote:
> >
> > This, plus the sleep below, means you are polling for data (trying again
> > and again at regular intervals). This is very unfriendly to other
> > processes on the system: much better is to specify infinite timeout
> > (can_read with no argument) and let the system wake you up when
> > something happens.
> 
> Yes, I know that polling does keep system busy and that's why I am
> sleep(1)'ing with every iteration of loop. My server program (example
> code was just a fraction) does much event based job (polls also
> filesystem, calculates some things and does various things according
> situations) and socket communications is only a tiny bit. So I loop my
> program infinitely, sleeping a while between iterations and doing
> polling of sockets etc. Server just can't halt when waiting data from
> socket - it must work all time.

You should use a proper event system to handle this: perhaps POE or
Event.pm. I confess I've never used either of these. I presume you could
also use Glib.pm, if you like, to match the client.

> Same goes to client - it is Perl/GTK2
> program which cannot halt when waiting data from server, otherwise user
> interface will also halt.

GTK2 has a facility (Gtk2::Helper->add_watch) to watch a filehandle in
addition to waiting for GUI events. In general any toolkit that does its
own event management will do the same.

> Perhaps using threads,

You could cook something up with threads, but I'd strongly recommend
against it. It would certainly be slower (unless your app ends up
CPU-bound and you have a multi-processor machine), and it would probably
be much harder to get right. At least with the client you're going to
have to structure your program 'inside-out' with callbacks anyway, so
there would be no gain whatever.

Ben

-- 
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit.|benmorrow@tiscali.co.uk
Musica vel ipsas arbores et horridas movet feras.     |


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

Date: 24 Aug 2006 02:14:02 -0700
From: "alpha_beta_release" <youknows@gmail.com>
Subject: Re: Permission denied trying to execute shell commands from Perl/CGI.
Message-Id: <1156410842.157872.24000@i3g2000cwc.googlegroups.com>

Use taint mode -T. The following i run with Apache on Windows. For
*NIX, linux,change 'c:/windows/system32'' to system path

---------------------------------------------
#!c:/perl/bin/perl -Tw
#                        ^

# u need to set this during Taint mode
$ENV{PATH} = 'c:/perl/bin/perl;c:/windows/system32';

use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser';  # would help, but only to debug

my $page = new CGI;

my $content;
local $/;
if (!open(PIPE1, '-|', "dir -w *")) {
     print $page->header('text/plain'),
           "ERROR : Can't list - $!";
     exit;
 }
$content = <PIPE1>;
close PIPE1;

print $page->header('text/plain'),
      $content;


Glenn Jackman wrote:
> At 2006-08-22 03:56PM, "Ben Morrow" wrote:
> > > #! /usr/bin/perl -w
> >      ^
> >  You don't want this space.
>
> Nothing wrong with spaces there.  See exec(2)
> 
> -- 
> Glenn Jackman
> Ulterior Designer



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

Date: 24 Aug 2006 01:09:56 -0700
From: "Davy" <zhushenli@gmail.com>
Subject: readdir conflict problem?
Message-Id: <1156406996.105520.269470@75g2000cwc.googlegroups.com>

Hi all,

I write a program to generate file list and directory list seperately
from a directory.
But I found the readdir run correctly only once. The second readdir
always read in a null list? why?

#-------------------------------------------------------
use strict;
use warnings;

use Data::Dumper;

my $path = "something";

opendir(DIR, $path);

#--- get directory list
my @dir_list = grep{-d "$path/$_"} readdir(DIR);
print $dir_list[1],"\n";
print Dumper(\@dir_list);

#--- get file list
my @file_list = grep{-f "$path/$_"} readdir(DIR);
print $file_list[1],"\n";
print Dumper(\@file_list);


closedir(DIR);
#------------------------------------------------------------------

Thanks!
Davy



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

Date: Thu, 24 Aug 2006 09:27:07 +0100
From: David Squire <David.Squire@no.spam.from.here.au>
Subject: Re: readdir conflict problem?
Message-Id: <ecjnsr$4o7$1@gemini.csx.cam.ac.uk>

Davy wrote:
> Hi all,
> 
> I write a program to generate file list and directory list seperately
> from a directory.
> But I found the readdir run correctly only once. The second readdir
> always read in a null list? why?

What is wrong with the documentation?

----

perldoc -f readdir:

readdir DIRHANDLE

Returns the next directory entry for a directory opened by "opendir". If 
used in list context, returns all the rest of the entries in the 
directory.  If there are no more entries, returns an undefined value in 
scalar context or a null list in list context.

 ...

----

Your first readdir (see code below) is in list context, so it returns 
everything. The second readdir thus returns a null list, as per the 
documentation.

You need to either:

a) store the list you get from the first readdir in an array and grep 
that (my choice)

or

b) close and open the directory again before the second readdir.

Please read the documentation for the functions you are using before 
posting questions here.


DS

> 
> #-------------------------------------------------------
> use strict;
> use warnings;
> 
> use Data::Dumper;
> 
> my $path = "something";
> 
> opendir(DIR, $path);
> 
> #--- get directory list
> my @dir_list = grep{-d "$path/$_"} readdir(DIR);
> print $dir_list[1],"\n";
> print Dumper(\@dir_list);
> 
> #--- get file list
> my @file_list = grep{-f "$path/$_"} readdir(DIR);
> print $file_list[1],"\n";
> print Dumper(\@file_list);
> 
> 
> closedir(DIR);
> #------------------------------------------------------------------


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

Date: 24 Aug 2006 09:03:54 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: readdir conflict problem?
Message-Id: <4l58bqFa8peU2@news.dfncis.de>

David Squire  <David.Squire@no.spam.from.here.au> wrote in comp.lang.perl.misc:
> Davy wrote:
> > Hi all,
> > 
> > I write a program to generate file list and directory list seperately
> > from a directory.
> > But I found the readdir run correctly only once. The second readdir
> > always read in a null list? why?
> 
> What is wrong with the documentation?
> 
> ----
> 
> perldoc -f readdir:
> 
> readdir DIRHANDLE
> 
> Returns the next directory entry for a directory opened by "opendir". If 
> used in list context, returns all the rest of the entries in the 
> directory.  If there are no more entries, returns an undefined value in 
> scalar context or a null list in list context.
> 
> ...
> 
> ----
> 
> Your first readdir (see code below) is in list context, so it returns 
> everything. The second readdir thus returns a null list, as per the 
> documentation.
> 
> You need to either:
> 
> a) store the list you get from the first readdir in an array and grep 
> that (my choice)
> 
> or
> 
> b) close and open the directory again before the second readdir.

c) rewinddir(), which exists explicitly for the purpose of reading
a directory handle again from the beginning.

Anno


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

Date: Thu, 24 Aug 2006 08:53:34 GMT
From: "wlcna" <wlcna@nospam.com>
Subject: regular expression variables under debugger
Message-Id: <iOdHg.17136$gY6.804@newssvr11.news.prodigy.com>

I have a program I was running through the debugger that had a usage 
like this (this is not the code but similar):

1: my $str = "hello there yes i am here";
2: $str =~ /([a-z]*)s/;
3: $yes = $1;

So, I'm running this in the debugger to check things out, make sure it's 
working as I expect, and the debugger when I get to line 3 tells me that 
$1 is "undef", and past line 3 it tells me that $yes did not receive any 
value, it's "undef" as well.

I run this same program *NOT IN THE DEBUGGER* and everything is normal, 
and I check by doing old-fashioned print statements.  $yes is "yes" as 
expected.

I've noticed problems before with viewing regular expression variables 
in teh debugger.

I now assume that regular expressions and the debugger just don't mix 
but prior to tonight I NEVER IMAGINED that something like my $yes 
variable above would also not show the correct value in the debugger.  I 
had assumed the special regex variables scope meant if I tried to print 
them directly from the debugger prompt, (i.e. p $1 or p $&) that 
wouldn't work, but of course I thought running the program from inside 
the debugger and watching its variables get set should still work 
properly.

But now I don't even think that works.  So what am I missing?

Thanks for any info! 




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

Date: 24 Aug 2006 01:26:55 -0700
From: "alpha_beta_release" <youknows@gmail.com>
Subject: Re: substr taking time
Message-Id: <1156408015.719084.42330@i42g2000cwa.googlegroups.com>

hi,

Perl script takes longer time than compiled program.

About how fast substr() is,recently, i try to test it. What i found
it's OK, and considerably fast.

i've done the following test, to compare substr() and less primitive
technique also in Perl. My objective is to find which technique is
faster. (You can extend the string if you want.)

----------------------------
use Benchmark ':all';

my $str1 = "1234567890123456789012345678901234567890";
my $str2 = "1234567890123456789012345678901234567890";
my @a = split '', $str1;
my @b = split '', $str2;

sub code1
{
       my $dumb;
       for(0..length $str1) {
              $dumb = 1 if(substr($str1, $_, 1) eq substr($str2, $_,
1));
       }
}
sub code2
{
       my $dumb;
       for(0..$#a) {
              $dumb = 1 if($a[$_] eq $b[$_]);
       }
}

timethese(100, {
       style1 => 'code1',
       style2 => 'code2'
});

----------------------------
code1() is a bit slower than code2(), but still fast enough to me.



Ted Zlatanov wrote:
> On 23 Aug 2006, beefstu350@hotmail.com wrote:
>
> > I am reading through a file that is 2,432 lines with a record length of
> > 450 bytes. I want to get the first 8 bytes from each line and stick in
> > into an array. When I excute the following piece of code it takes 7 to
> > 8 seconds.
> >
> > In comparison, when I issue a  cut -c1-8 < data this from MKS this
> > takes one second on the same data
> >
> > Does anybody know of a way on how these lines of code can be optimized.
> >
> > BTW, I am using Active Perl on a Windows 2003 platform, but that should
> > not make a difference
> >
> > print scalar ( localtime() ) . "\n";
> >
> > while ($NextLine = <INPUT>)
> > {
> > @docidarray = (@docidarray, substr($NextLine, 0, 8));
> > }
> >
> > print scalar ( localtime() ) . "\n";
>
> While the advice to use push() is valid, you should realize that a
> simple task like this does not require Perl, and you are incurring
> quite a bit of overhead when you use Perl.  You'll never be as fast as
> `cut' with Perl at doing `cut's job (well, some examples could be
> contrived).
>
> Starting up Perl, in particular, takes a while (depending on the
> machine, of course).  The modules you are using may also slow you
> down.  So the 7-8 seconds time may not be just the processing time.
>
> Then, of course, each statement is run by the Perl interpreter, unlike
> a program like `cut' which is very optimized in C to do just one task.
>
> So you have to decide - if the task requires just `cut', use just
> that.  You can produce the localtime() output with the `date'
> command.  If, however, this is part of a bigger program, you may have
> to live with the slight performance hit.
> 
> Ted



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

Date: Thu, 24 Aug 2006 09:36:46 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: substr taking time
Message-Id: <OqeHg.16091$365.480@edtnps89>

[ Please do not top-post.  TIA ]

alpha_beta_release wrote:
> 
> Perl script takes longer time than compiled program.
> 
> About how fast substr() is,recently, i try to test it. What i found
> it's OK, and considerably fast.
> 
> i've done the following test, to compare substr() and less primitive
> technique also in Perl. My objective is to find which technique is
> faster. (You can extend the string if you want.)
> 
> ----------------------------
> use Benchmark ':all';
> 
> my $str1 = "1234567890123456789012345678901234567890";
> my $str2 = "1234567890123456789012345678901234567890";
> my @a = split '', $str1;
> my @b = split '', $str2;
> 
> sub code1
> {
>        my $dumb;
>        for(0..length $str1) {
>               $dumb = 1 if(substr($str1, $_, 1) eq substr($str2, $_,
> 1));
>        }
> }
> sub code2
> {
>        my $dumb;
>        for(0..$#a) {
>               $dumb = 1 if($a[$_] eq $b[$_]);
>        }
> }
> 
> timethese(100, {
>        style1 => 'code1',
>        style2 => 'code2'
> });
> 
> ----------------------------
> code1() is a bit slower than code2(), but still fast enough to me.

Maybe because you are reading past the end of the strings:

        for(0..length $str1) {

Should be:

        for(0..length($str1) - 1) {


John
-- 
use Perl;
program
fulfillment


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

Date: Thu, 24 Aug 2006 08:44:08 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: variable with empty space
Message-Id: <8v10s3-gk9.ln1@osiris.mauzo.dyndns.org>


Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> Ben Morrow wrote:
> > Quoth Josef Moellers <josef.moellers@fujitsu-siemens.com>:
> > 
> >>	if ($username =~ /\s/)
> >>
> >>(ouch, ouch, don't hit me ... yes ... leaning toothpick ...)
> >>
> >>	if ($username =~ m|\s|)
> > 
> > 
> > Or 
> >     if ($username =~ m{\s}) {
> > 
> > , as | is meta in a regex. :)
> 
> ... and so are '{' and '}':

 ...but they nest in quoting constructs.

    m{ \s{1,10} }x;

does what you think it does.

Ben

-- 
  Joy and Woe are woven fine,
  A Clothing for the Soul divine       William Blake
  Under every grief and pine          'Auguries of Innocence'
  Runs a joy with silken twine.                         benmorrow@tiscali.co.uk


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

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


Administrivia:

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

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

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

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

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


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


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