[19135] in Perl-Users-Digest
Perl-Users Digest, Issue: 1330 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 18 21:05:44 2001
Date: Wed, 18 Jul 2001 18:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <995504711-v10-i1330@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 18 Jul 2001 Volume: 10 Number: 1330
Today's topics:
Re: All computers in world MUST sync with ATOMIC clock <luke@webber.com.au>
Re: All computers in world MUST sync with ATOMIC clock <wyzelli@yahoo.com>
Ann: Zeus Programmers Editor V3.60 <jussij@zeusedit.com>
Re: Check for Dups! <millettNOSPAM@lblueyonder.co.uk>
FAQ: How can I free an array or hash so my program shri <faq@denver.pm.org>
GnuPG encrypting from STDIN in CGI <bootsy52@gmx.net>
Re: GnuPG encrypting from STDIN in CGI <krahnj@acm.org>
Help: seems like a bug in 5.6.1 <mike_tchk@yahoo.com>
Re: How to timeout a socket recv under win32? <dbe@wgn.net>
Re: Installing DBD on AIX 4.3.3 (Jens-Uwe Mager)
map and split combination slow <rpdaly@bigfoot.com>
Re: my <ekulis@apple.com>
Re: New to PERL/CGI question <EvR@compuserve.com>
Re: Pattern Matching Questions (Tad McClellan)
Re: Pattern Matching Questions <dbe@wgn.net>
Re: perl regular expression grammar (Randal L. Schwartz)
Re: perl regular expression grammar (Abigail)
Re: perl regular expression grammar (Mark Jason Dominus)
Re: Printing results of a subroutine to a file <racso83@bellatlantic.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 18 Jul 2001 23:17:38 GMT
From: "Luke Webber" <luke@webber.com.au>
Subject: Re: All computers in world MUST sync with ATOMIC clock before 12:00 AM 21July 2001
Message-Id: <mOo57.13943$Xr6.115149@news-server.bigpond.net.au>
"Joe Cosby" <joecosby@SPAMBLOCKmindspring.com> wrote in message
news:3b55f01a.2170914@news.mindspring.com...
> kkal@javakk.com (Ken Kalish) hunched over a computer, typing
> feverishly;
> thunder crashed, kkal@javakk.com (Ken Kalish) laughed madly, then
> wrote:
>
> >Thank you. Please continue to relay to us all orders from the mother
ship.
>
> "Double cheeseburger, cheddar curly fries, and a large diet coke"
<accent>
No Coke. Pepsi. <g>
</accent>
Luke
------------------------------
Date: Thu, 19 Jul 2001 09:50:38 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: All computers in world MUST sync with ATOMIC clock before 12:00 AM 21July 2001
Message-Id: <RDp57.12$_m5.783@vic.nntp.telstra.net>
"Adam Meikle" <adam@dedekind.com> wrote in message
news:tlbvdcj67gi61f@corp.supernews.com...
> How do we know when that is, exactly?
Simple - you pre-synchronise, so that you know that you can synchronise at
the right time! Course then you don't need to synchronise, so you may as
well not pre-synchronise, but if you don't you can't and if you do you don't
need to .... er... ummmm...
Never mind :)
Wyzelli
--
push@x,$_ for(a..z);push@x,' ';
@z='092018192600131419070417261504171126070002100417'=~/(..)/g;
foreach $y(@z){$_.=$x[$y]}y/jp/JP/;print;
------------------------------
Date: Thu, 19 Jul 2001 09:37:55 +1000
From: Jussi Jumppanen <jussij@zeusedit.com>
Subject: Ann: Zeus Programmers Editor V3.60
Message-Id: <3B561DD3.7971@zeusedit.com>
The latest release of the Zeus for Windows Version 3.60 programmer's
editor is now available.
The features new to this release include:
+ support for Java, GCC, and Perl debugging from within Zeus
+ support for CVS verison control using MS-SCC interface.
+ all new visual file differencing feature.
+ enhanced project workspace support to allow for easier
configuration of compilers and linkers on a project
by project basis.
+ new scripting functions.
+ other minor enhancements and bug fixes.
For more details regarding all these changes see the history.txt
found in the Zeus install directory.
If you wish to find out more about this latest release you can
download the shareware version from the following web page:
http://www.zeusedit.com/download.html
Jussi Jumppanen
xidicone@iname.com
------------------------------
Date: Thu, 19 Jul 2001 00:18:43 GMT
From: "millside" <millettNOSPAM@lblueyonder.co.uk>
Subject: Re: Check for Dups!
Message-Id: <DHp57.2104$Ii1.406853@news1.cableinet.net>
> >I would like to protect against multiple entries being written to my flat
> >file in future and was wanting a sub routine to do it.
>
> #!/usr/bin/perl -w
> use strict;
> use Fcntl ':flock';
Whats the significance here for the colon before flock?
>
> my $email = shift; # for testing purposes
>
> open LIST, "+>> address.txt" or die $!;
I would have thought R/W with >> would be the order here. Is it necessary to
include "+" too?
> flock LIST, LOCK_EX;
> seek LIST, 0, 0; # start at the beginning..
>
> my $found = 0;
> while (<LIST>) {
Presumably "LIST" being any named file handle such as <FILE_HANDLE> all
capitals?
> chomp;
> $found++, last if $_ eq $email;
> }
>
> if ($found) {
> print "Sorry, $email is already in the database.\n";
> } else {
> seek LIST, 0, 2; # some systems may need this
> print LIST "$email\n";
> print "$email has been successfully added to the database.\n";
> }
>
> close LIST; # unlocks the file
>
>
> Some notes:
>
> * Opening with mode "+<" would work too, and would eliminate the need
> for the first seek. But it would fail if the file didn't exist.
>
> * Locking _is_ important. And you do need to obtain an exclusive lock
> for the whole process. That's because as soon as you start looking
> for an address, you effectively assert that the _next_ process that
> will look for the same address _will_ find it already in the file.
>
> * Using grep may in fact often be faster than manually looping over a
> list, at least unless you expect an early match. But the real reason
> why this code is better is that it doesn't need to read the whole
> list of addresses into the memory.
I was working some code with grep, but couldn't get it to work correctly and
I have since abandoned it anyway. Basically I was looking for any working
solution in the short term and grep was one suggested.
>
> * See "perldoc -f seek" for details on why the second seek may be
> necessary even if the file has been opened in append mode. In any
> case, it will do no harm there.
Been there! I fully understand the reason why a second seek is necessary
although initially, it seemed impractical to do it twice.. "to be sure to be
sure" as my Pa would've said!
>
> * Don't use LOCK_UN. Simply closing the filehandle will do the Right
> Thing -- flush the output buffer, unlock the file and close it.
With a simple "close" at the end? One other method which seems to have gone
out of favour is to use traditionally valued constants flock 1, 2, 8 and 4
example:
flock(FILE, 2);
print FILE "$scalar\n";
flock(FILE, 8);
Is this now supposedly redundant or is it the preferred method just to use
symbolic names?
Also is it possible to use LOCK_SH to request a shared lock, thereby saving
having to open twice?
>
> This code isn't all that slow -- running it on a 400kb word list takes
> about half a second, and you're unlikely to have hundreds of thousands
> of addresses in your list.
Hell that's pretty fast! and anyway who said I'm unlikely to have hundreds
of thousands of addresses? pessimism acheives nothing! realism on the other
hand... OK, you win!
>
> However, if you prefer the DBM approach, you could use a DBM file just
> for detecting duplicate addresses, and keep the rest of the data still
> in flat files. You'd still need to lock the DBM file just as above.
Thanks, I'll keep your posting as a referrence for my future endeavours.
--
millside
_____________
------------------------------
Date: Thu, 19 Jul 2001 00:17:02 GMT
From: PerlFAQ Server <faq@denver.pm.org>
Subject: FAQ: How can I free an array or hash so my program shrinks?
Message-Id: <2Gp57.8$EW5.210139136@news.frii.net>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.
+
How can I free an array or hash so my program shrinks?
You can't. On most operating systems, memory allocated to a program can
never be returned to the system. That's why long-running programs
sometimes re-exec themselves. Some operating systems (notably, FreeBSD
and Linux) allegedly reclaim large chunks of memory that is no longer
used, but it doesn't appear to happen with Perl (yet). The Mac appears
to be the only platform that will reliably (albeit, slowly) return
memory to the OS.
We've had reports that on Linux (Redhat 5.1) on Intel, "undef $scalar"
will return memory to the system, while on Solaris 2.6 it won't. In
general, try it yourself and see.
However, judicious use of my() on your variables will help make sure
that they go out of scope so that Perl can free up that space for use in
other parts of your program. A global variable, of course, never goes
out of scope, so you can't get its space automatically reclaimed,
although undef()ing and/or delete()ing it will achieve the same effect.
In general, memory allocation and de-allocation isn't something you can
or should be worrying about much in Perl, but even this capability
(preallocation of data types) is in the works.
-
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to
news:news.answers
or to the many thousands of other useful Usenet news groups.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-1999 Tom Christiansen and Nathan
Torkington. All rights reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
03.19
--
This space intentionally left blank
------------------------------
Date: Thu, 19 Jul 2001 01:39:21 +0200
From: "Carsten Menke" <bootsy52@gmx.net>
Subject: GnuPG encrypting from STDIN in CGI
Message-Id: <20010719.013920.993967637.1765@gmx.net>
Hi all,
I have a wired problem here, I know how to get GnuPG getting to encrypt
data from stdin this is done after executing /usr/bin/gpg --homedir.....
followed by newline to tell gpg that the data is coming from stdin not
from a file. But how to realize that in perl?
I wrote the following code, but this simply doesn't work no matter how
you change ($command, $options, $input) or ("$command $options $input")
Also I find everytime in the error log of the Webserver.
sh: That : Command not found
I think the
problem is that gpg needs a \n to realize that the data is to read from stdin.
Here is the Code (which doesn't work)
##################### BEGIN CODE ###################################
$command='/usr/bin/gpg';
$pubkey='bootsy52@gmx.net';
$gpgdir='/home/chantal/.gnupg';
$input="Dast ist ein Test";
$options=" \-\-homedir $gpgdir \-\-armor \-\-encrypt \-\-recipient $pubkey\n";
die "Could not execute fork: $!"
unless defined ($pid = open(CHILD, "-|"));
if ($pid)
{
@gpg=<CHILD>;
close(CHILD);
}
else
{
exec("$command $options $input");
}
################ END CODE ###################################
Does anybody now an solution? perldoc -f exec() didn't help :-(
Cheers
Carsten
------------------------------
Date: Wed, 18 Jul 2001 23:58:00 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: GnuPG encrypting from STDIN in CGI
Message-Id: <3B5622D3.B6F03768@acm.org>
Carsten Menke wrote:
>
> Hi all,
>
> I have a wired problem here, I know how to get GnuPG getting to encrypt
> data from stdin this is done after executing /usr/bin/gpg --homedir.....
> followed by newline to tell gpg that the data is coming from stdin not
> from a file. But how to realize that in perl?
>
> I wrote the following code, but this simply doesn't work no matter how
> you change ($command, $options, $input) or ("$command $options $input")
> Also I find everytime in the error log of the Webserver.
>
> sh: That : Command not found
>
> I think the
> problem is that gpg needs a \n to realize that the data is to read from stdin.
>
> [snip Code (which doesn't work)]
Perhaps the GnuPG module is what you need.
http://search.cpan.org/search?mode=module&query=GnuPG
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 19 Jul 2001 00:43:45 GMT
From: Mishka <mike_tchk@yahoo.com>
Subject: Help: seems like a bug in 5.6.1
Message-Id: <53q57.6261$XT4.4103107@typhoon.ne.mediaone.net>
Hi All:
does anyone know why this happens:
-------- file pq.c ------------------------
#include <stdio.h>
#include "EXTERN.h"
#include "XSUB.h"
#include "perl.h"
extern "C" XS(foo) {
printf("foo() == qp::AUTOLOAD\n");
}
extern "C" XS(boot_qp){
newXS("qp::AUTOLOAD", foo, (char *)__FILE__);
}
-------- file pq.pm ------------------------
package qp;
require DynaLoader;
require AutoLoader;
@ISA = qw(AutoLoader DynaLoader);
$VERSION = '1';
bootstrap qp $VERSION;
1;
-------- test.pl ------------------------
use qp;
qp::AUTOLOAD;
qp::bar();
-------------------------------------
when I run, here's what happens:
$ /usr/local/perl5.005/bin/perl t.pl
foo() == qp::AUTOLOAD
foo() == qp::AUTOLOAD
and
$ /usr/local/perl5.6.1/bin/perl t.pl
foo() == qp::AUTOLOAD
Undefined subroutine &qp::bar called at t.pl line 6.
$ ls
qp.bs qp.c qp.o qp.pm qp.so* t.pl*
I looked thru the changelogs but nothing seems relevant. Any idea what's
going on?
PLEASE, cc your replies to my email: I don't browse this group routinely.
Thanx,
Mishka
------------------------------
Date: Wed, 18 Jul 2001 15:46:09 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: How to timeout a socket recv under win32?
Message-Id: <3B5611B1.27D93924@wgn.net>
Bart Lateur wrote:
>
> Benjamin Goldberg wrote:
>
> >> So you mean recv().
> >
> >Umm, what do you mean by that comment?
>
> Oops, I have ben mixing threads. I cconfused this with another current
> thread, where the OP asked how you can know how many bytes there are
> currently available for reading.
>
> It all boils down to this: how do you receive all the data that the
> other side is sending to you, without waiting for more that won't come?
> How do you know that, for the moment, the transmission is complete?
>
> <S>, read, sysread, recv are similar yet different. I guess that only
> read() is not usable. select() or IO::Select can tell you that something
> is available, but not how much. I have the feeling that there isn't a
> real generic solution, that the sender somehow HAS to indicate how long
> the transmission will be. Otherwise, the receiver can stop receiving too
> soon, or wait for something that won't come.
On Win32, the way to handle reads is:
1) Don't read until IO::Select can_read tells you there is data available.
2) Read until you get less data than you asked for (or an error/eof) condition.
3) After 2) go back to 1).
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/
------------------------------
Date: 18 Jul 2001 22:12:48 GMT
From: jum@anubis.han.de (Jens-Uwe Mager)
Subject: Re: Installing DBD on AIX 4.3.3
Message-Id: <slrn9lc2et.31o.jum@anubis.han.de>
On Mon, 16 Jul 2001 12:30:12 +0200, H. Merijn Brand <h.m.brand@hccnet.nl> wrote:
>q_mac@hotmail.com (Y) wrote in
>news:e7bb86d8.0107141014.30515bb@posting.google.com:
>
>> After I installed DBI 1.15, I get this message when I try to make
>> DBD-DB2-0.75..
>
>1. Why use 1.15 when 1.18 is out since begin july 2001?
>
>> ld: 0711-851 SEVERE ERROR: Output file: blib/arch/auto/DBD/DB2/DB2.so
>> The file is in use and cannot be overwritten.
>> make: The error code from the last command is 12.
>
>2. .../DBI-1.18 > find . -name \*.so | xargs rm -f
> .../DBI-1.18 > make distclean
> .../DBI-1.18 > perl Makefile.PL
> .../DBI-1.18 > make
> .../DBI-1.18 > make test
> .../DBI-1.18 > make install UNINST=1
>3. .../DBD-DB2 > find . -name \*.so | xargs rm -f
> .../DBD-DB2 > make distclean
> .../DBD-DB2 > perl Makefile.PL
> .../DBD-DB2 > make
> .../DBD-DB2 > make test
> .../DBD-DB2 > make install UNINST=1
>
>It's somehow AIX specific that .so libs, once used, won't be removed when
>tried so on the first go. The rm will force it so.
>
>> I have been running a script using the DBI functions but have it has
>> long been stopped. I have closed and re opened my session and it still
>> give this error.
>> HELP.
Running slibclean will also help, this unloads shared objects from the
internal cache and releases the busy status on the files.
--
"I just opitimzed that algorithm 10x, it gets to the segfault much
faster now!" -- anonymous
Jens-Uwe Mager <pgp-mailto:62CFDB25>
------------------------------
Date: Thu, 19 Jul 2001 00:31:13 GMT
From: Randy Da.y <rpdaly@bigfoot.com>
Subject: map and split combination slow
Message-Id: <p6acltgdld9i55via8j4klg713143t1r01@4ax.com>
I'm processing a DB extraction file. This is a flat text file with
fixed length fields. I'm using the following segment within a
format_date function:
if ($date =~ m/\s/) {
map{ $len += $hlen{$_} } split //, $fmt2;
return($datestr = " " x $len);
}
This is running extremely slow. When left in the function, 10K records
are processed in approximately 3 minutes 20 seconds, without it, it is
25 seconds.
Suggestions, comments?
------------------------------
Date: Wed, 18 Jul 2001 16:57:38 -0700
From: Ed Kulis <ekulis@apple.com>
Subject: Re: my
Message-Id: <3B562272.45FF22D7@apple.com>
The Namespaces reference is quite a lot to read and comprehend.
Simply put:
"my" before a variable makes it local to a block. A block is a a bunch of text
between curly braces "{" and "}". (There's also a "local" qualifier but don't
use it.)
The value of the variable won't carry outside the block so you needn't worry
about a variable having a value that was set some where earlier in the
execution.
Here's a script that demonstrates "my" and some other perl features:
#!/usr/local/bin/perl -w
#
# the -w option provides warnings about variable names only used once
# which are probably typo's, numeric use of a non-number,
# redefined subroutines
# and many other things.
# The intent of this code is to show some perl features to a newbie
# it's not intended to be even the zero-th word in style.
# -----------------------------------------------------------------------------
# Usage: nona.pl Miska Mooska Mouseketeer
#
# Output:
#
# nona.pl
# =======
#
# argv=ARRAY(0x2002c828)
# argv[0]=Miska
# argv[1]=Mooska
# argv[2]=Mouseketeer
# PWD=/home/ekulis/Perl/Work/wrkbin
# scriptname=nona.pl
# PAGER=more
#
# -----------------------------------------------------------------------------
package main ; # main is the default name of the
# but might as well make it explicit
use strict; # requires module name before non-my variables
# e.g. $main::input_file ;
use vars '%p'; # exempts hash p from the need to prefix $main::
# all global parms will be in this hash
# This makes it easy to print out all of them.
&main();
sub get_parms {
my @argv = @_ ; # @argv local to this sub
my $argv = [ @argv ]; # $argv is anonymous list
# the square brackets [ and ]
# provide pointer to the array
# do block isolates "my" variables within itself
#
do {
my $scriptname = $0 ;
$p{'argv'} = $argv ;
$p{'PWD'} = $ENV{'PWD'} ; # Unix variables are in @ENV
$p{'PAGER'} = $ENV{'PAGER'} ;
$p{'scriptname'} = $scriptname ;
};
$p{'scriptname'}; # this is passed to caller
}
sub main {
# Run get_parms
# without "my" need package name for the variable that gets
# the return value which is the last value set in the sub
# @ARGV is global and it contains the command line arguments.
#
$main::scriptname = &get_parms(@ARGV);
# print newline, the script name, underline it; print 2 newlines
#
print "\n", $main::scriptname, "\n",
"=" x length $main::scriptname, "\n" x 2;
&do_main();
}
sub do_main {
my %parms = %p ; # %parms is local hash
# $k and $v are local to the while block
# If $v is an array reference then call the
# array printer sub and pass the hash key as the array name
# and the hash value as the reference to the array list.
#
while ((my $k, my $v) = each(%parms)) {
print "\t$k=$v" , "\n" ;
&print_arr($k,$v) if ref $v eq "ARRAY";
}
print "\n";
}
sub print_arr {
my $name = shift ; # sets $name
my $arr = shift ; # sets $arr to the array pointer
# use @$arr to use the list in a list context
# Print array list with name and subscripts
# e.g
# argv[0]=abc
# argv[1]=def
#
do {
my $i=0 ;
foreach my $e (@$arr) {
print "\t\t", $name, "[$i]=$e\n";
$i++;
}
}
}
Mark Jason Dominus wrote:
> In article <9ilps5$gfj$1@josh.sovintel.ru>, Alex <alex82p@svs.ru> wrote:
> >What the statement "my" means?
>
> See
>
> http://perl.plover.com/~mjd/FAQs/Namespaces.html
>
> --
> @P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
> @p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
> ($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
> close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Wed, 18 Jul 2001 17:17:21 -0600
From: "Richard A. Evans" <EvR@compuserve.com>
Subject: Re: New to PERL/CGI question
Message-Id: <9j555o$74i$1@suaar1ac.prod.compuserve.com>
> however the only output I get on my pages is: [an error occurred
> while processing this directive]
>
> If I run the script from Telnet I get this
>
> Content-type: text/html
>
> 2
>
Without seeing your perl code, I'd guess that you're not printing a valid
header.
If you're using cgi.pm, you'd use
$q = new CGI;
print $q->header();
And if you're coding it my hand, you'd use
print "Content-type: text/html\n\n";
------------------------------
Date: Wed, 18 Jul 2001 19:06:49 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Pattern Matching Questions
Message-Id: <slrn9lc5k9.eu6.tadmc@tadmc26.august.net>
Mark Murray <mark@cowcud.demon.co.uk> wrote:
>"Mark Riehl" <mark.riehl@agilecommunications.com> wrote:
>
>>One more question - I've got a directory full of files that all have the
>>format of filename: foo 1.txt, foo 2.txt, etc. I can get all the names of
>>all the text files into an array using the following:
>>
>>$dirname = ".";
>>opendir(DIR, $dirname);
>>@txt_files = grep { /\.txt$/} readdir (DIR);
>>
>>Question - I'd like to be able to keep other .txt files in the same
>>directory, that is, I'd like to only match on the foo*.txt files. I tried a
>>few variations of the grep, but couldn't seem to get it to work. Any
>>suggestions?
@txt_files = grep { /foo.*\.txt$/} readdir (DIR);
>Why not use the glob function? Then you can replace all of that code
>with:
>
>@txt_files = glob("./foo*.txt");
Because opendir/grep is more general-purpose than glob().
At least you used the named version instead of <./foo*.txt> :-)
(and @txt_files does not get the same contents with your code either)
There are 5 (2 if you're using 5.6) reasons why I never use globbing:
1) sloooow (forks another process)
2) non-portable (csh not all the same)
3) limited (csh has limits)
4) wimpy pattern matching (can't glob "starts and ends with same letter")
5) can only work on filenames (can't glob based on file's size/age)
opendir/grep gives you more power than globbing. I like power.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 18 Jul 2001 17:26:07 -0700
From: "$Bill Luebkert" <dbe@wgn.net>
Subject: Re: Pattern Matching Questions
Message-Id: <3B56291F.BDDC8A0B@wgn.net>
Mark Riehl wrote:
>
> All - I've got a input file that has the following format:
>
> # Comments go here
> key1=val1
> key2=val2
> ...
>
> After opening the file, I'm parsing the file using the following block (I
> want to skip over the comments):
>
> while (<INPUT>) {
> chomp;
> if ( !m/^\#/ ) {
> ($flag,$val) = split('=', $_);
> print "\$flag = $flag, \$val = $val\n";
> }
> }
>
> I run into problems when there is a blank line in between key/value pairs.
> I tried adding (!m/^\\n/) and (!m/^\n) to skip over the newlines, but it
> didn't seem to work. Any suggestions?
next if /^\s*$/; # skip empty lines
next if /^#/; # skip comments
# if you get here it's good
> One more question - I've got a directory full of files that all have the
> format of filename: foo 1.txt, foo 2.txt, etc. I can get all the names of
> all the text files into an array using the following:
>
> $dirname = ".";
> opendir(DIR, $dirname);
> @txt_files = grep { /\.txt$/} readdir (DIR);
>
> Question - I'd like to be able to keep other .txt files in the same
> directory, that is, I'd like to only match on the foo*.txt files. I tried a
> few variations of the grep, but couldn't seem to get it to work. Any
> suggestions?
One of these should help:
@txt_files = grep { /^foo.*\.txt$/} readdir (DIR);
@txt_files = grep { /^foo \d+\.txt$/} readdir (DIR);
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles Mailto:dbe@todbe.com
/ ) /--< o // // http://dbecoll.webjump.com/ (Free site for Perl)
-/-' /___/_<_</_</_ Castle of Medieval Myth & Magic http://www.todbe.com/
------------------------------
Date: 18 Jul 2001 19:59:00 -0400
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: perl regular expression grammar
Message-Id: <01-07-093@comp.compilers>
Keywords: parse
>>>>> "Alan" == Alan Oursland <alan@oursland.net> writes:
Alan> I've been looking for a complete perl 5 regular expression grammar
Alan> and, having been unsuccessful in my search, have attempted to write
Alan> one myself.
You might look at YAPE::Regex (et. seq.) in the CPAN. Jeff Pinyan has
done a fairly comprehensive study of regex.
print "Just another Perl hacker,"
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: 18 Jul 2001 20:02:27 -0400
From: abigail@foad.org (Abigail)
Subject: Re: perl regular expression grammar
Message-Id: <01-07-101@comp.compilers>
Keywords: syntax
Alan Oursland (alan@oursland.net) wrote on MMDCCCLXXVIII September
MCMXCIII
## I've been looking for a complete perl 5 regular expression grammar
## and, having been unsuccessful in my search, have attempted to write
## one myself. I was wondering if anyone could help me find any errors in
## it (excluding grammar syntax errors). I've left out embedded modifiers
## from the grammar -- I'm not sure how they fit into the grammar. I've
## also skimmed over the non-meta character production. One area I am
## confused is the "\c[" control character (described at
## http://www.perldoc.com/perl5.6/pod/perlre.html). How does this work?
##
## Alan Oursland
##
## Here is the grammar:
## <re> ::= <union>
## <union> ::= <concat>"|"<union> | <concat>
## <concat> ::= <quant><concat> | <quant>
## <quant> ::= <group>"*" | <group>"+" | <group>"?" | <group>"{"<bound>"}" | <group>
## <group> ::= "("<re>")" | <term>
## <term> ::= "." | "$" | "^" | <char> | <set>
## <bound> ::= <num> | <num>"," | <num>","<num>
## <char> ::= <non-meta> | "\"<escaped>
## <non-meta> ::= any non-meta char
## <escaped> ::= <meta>|<control>|<special>|<assert>
## <meta> ::= "."|"^"|"$"|"?"|"*"|"+"|"|"|"["|"("|")"|"\"|"{"
## <control> ::= "t"|"n"|"r"|"f"|"a"|"e"|"l"|"u"|"L"|"U"|"E"|"Q"
## <special> ::= <backoctal>|<hexchar>|<controlchar>|<class>
## <assert> ::= "b"|"B"|"A"|"z"|"Z"|"G"
## <backoctal> ::= <digit> | <digit><digit> | "0"<oct><oct> | "+" | "&" | "`" | "'"
## <hexchar> ::= "x"<hex><hex> | "x{"<hex><hex><hex><hex>"}"
## <controlchar> ::= "c["
## <namedchar> ::= "N{"<name>"}"
## <class> ::= "w"|"W"|"s"|"S"|"d"|"D"|"X"|"C" |"p"<name>|"P"<name>|"[:"<posixclass>":]"|"[:^"<posixclass>":]"
## <posixclass> ::= "alpha"|"alnum"|"ascii"|"cntrl"|"digit"|"graph"|"lower"|"print"|"punct"|"space"|"upper"|"word"|"xdigit"
## <name> ::= <unicodeclass>
## <unicodeclass> ::= "IsAlpha"|"IsAlnum"|"IsASCII"|"IsCntrl"|"IsDigit"|"IsGraph"|"IsLower"|"IsPrint"|"IsPunct"|"IsSpace"|"IsUpper"|"IsWord"|"IsXDigit"
## <set> ::= "[" <set-items> "]" | "[^" <set-items> "]"
## <set-items> ::= <set-item> | <set-item> <set-items>
## <set-item> ::= <range> | <char>
## <range> ::= <char> "-" <char>
## <num> ::= <digit><num> | <digit>
## <oct> ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"
## <digit> ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
## <hex> ::= "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"a"|"b"|"c"|"d"|"e"|"f"|"A"|"B"|"C"|"D"|"E"|"F"
## <mod> ::= "\i"|"\m"|"\s"|"\x"
Some regexes that I cannot parse using the above grammar:
/3*?/
/g{,1}/
/\cM/
/aa/
/[*]/
/(?!!)/
Some regexes can be parsed ambigiously with the above grammar:
/\+/
(why are "+", "&", "`" and "'" mentioned in <backoctal>?)
Here are some modifications of the grammar that fixes some of the
issues:
Fixes /3*?/:
<quant> ::= <group><quantifier><greedy> | <group>
<quantifier> ::= "*" | "?" | "+" | "{" <bound> "}"
<greedy> ::= "?" | ""
Fixes /aa/:
<term> ::= "." | "$" | "^" | <chars> | <set>
<char> ::= <non-meta> | "\"<escaped>
Fixes /\cM/:
<controlchar> ::= "c" <any-char>
<any-char> ::= Any possible character.
(but that doesn't fix /\c/ and would allow /\c\/ which doesn't parse in Perl).
Abigail
--
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"
__END__
A bee crawling in // the branches of a hazel. A // pair of bears. Bankei.
------------------------------
Date: 18 Jul 2001 20:01:28 -0400
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: perl regular expression grammar
Message-Id: <01-07-099@comp.compilers>
Keywords: syntax
One comments: Your notation seems unnecessarily cumbersome. Why not
say
<set> ::= "[" "^"? <set-item>+ "]"
as an abbreviation for:
><set> ::= "[" <set-items> "]" | "[^" <set-items> "]"
><set-items> ::= <set-item> | <set-item> <set-items>
?? It seems like not much is gained your way.
You have a few omissions:
In article <01-07-080@comp.compilers>, Alan Oursland <alan@oursland.net> wrote:
><quant> ::= <group>"*" | <group>"+" | <group>"?" | <group>"{"<bound>"}" | <group>
This should be:
<quant>::= <group>"*" | <group>"+" | <group>"?" | <group>"{"<bound>"}"
| <group>"*?" | <group>"+?" | <group>"??" | <group>"{"<bound>"}?"
| <group>
><group> ::= "("<re>")" | <term>
I think this should look more like this:
<group> ::= "("<qmod><re>")" | <term>
<qmod> ::= "" | "?:" | "?=" | "?>" | "?!" | "?<=" | "?<!"
| <qexpr>
| "?(" <num-or-expr> ")" <re-or-two>
| "?"<modifs> | "?-"<modifs> | "?"<modifs>"-"<modifs>
<num-or-expr> ::= <num> | <qexpr>
<qexpr> ::= "?{" <expr> "}"
<re-or-two> ::= <concat> | <concat> "|" <concat>
<modifs> ::= <modif> | <modif><modifs>
<modif> ::= "s" | "m" | "i" | "x"
I'll leave it up to you to figure out how to represent <expr>. The
rule is that Perl looks for the matching close brace, and backslashed
close-braces don't count on the way.
><char> ::= <non-meta> | "\"<escaped>
><non-meta> ::= any non-meta char
><escaped> ::= <meta>|<control>|<special>|<assert>
This isn't right, because "\d" is legal. It means the same as 'd'.
><hexchar> ::= "x"<hex><hex> | "x{"<hex><hex><hex><hex>"}"
\x3 is also legal.
Hope this helps, although I'm not sure what you're trying to accomplish.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Wed, 18 Jul 2001 23:06:11 GMT
From: "Racso" <racso83@bellatlantic.net>
Subject: Re: Printing results of a subroutine to a file
Message-Id: <DDo57.17207$l%.6113952@typhoon2.gnilink.net>
"Ed Kulis" <ekulis@apple.com> wrote in message news:3B55F814.E99E5C5@apple.com...
> Hi,
>
> You can also
>
> open(STDOUT,">out.tmp"); # redirect STDOUT to a file
>
> print "xxxxxxx","\n"; # goes into "out.tmp"
>
> &print_sub();
>
> sub print_sub {
> print "SSSSSSSSSSSSS","\n"; # STDOUT global so still goes into
> "out.tmp"
> }
>
> -ed
Ahh! That's it! Thank you!
Jay Tilten's explanation of STDOUT got me thinking about using <STDOUT>
somehow, but wasn't sure how. But this is it, this is exactly what I needed.
Thank you!! ^_^
Thanks to all who put in your two cents, I really appreciate the help.
Best,
--Racso
Henry D. Archut
------------------------------
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 1330
***************************************