[19688] in Perl-Users-Digest
Perl-Users Digest, Issue: 1883 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 6 06:05:30 2001
Date: Sat, 6 Oct 2001 03:05:06 -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: <1002362706-v10-i1883@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 6 Oct 2001 Volume: 10 Number: 1883
Today's topics:
Re: "chdir" argument as a variable... <goldbb2@earthlink.net>
activeperl, windows 98 & apache (bal)
Re: activeperl, windows 98 & apache <bart.lateur@skynet.be>
DBI test error (BorisVian)
Re: Help -- array/hash headaches <goldbb2@earthlink.net>
How to change date of a file NOSPAM@mail.ru
Re: How to change date of a file <krahnj@acm.org>
Re: How to change date of a file <comdog@panix.com>
Re: How to change date of a file NOSPAM@mail.ru
Re: How to have portable email script? (F. Xavier Noria)
Re: How to have portable email script? (Mark Taylor)
How to interpret ?? (Phanidhar)
Re: How to interpret ?? <goldbb2@earthlink.net>
Re: How to interpret ?? <comdog@panix.com>
Re: Multiplexing strings - one line only <joe+usenet@sunstarsys.com>
Re: Problem with backticks and passing spaces in quotes <dtweed@acm.org>
Re: Problem with backticks and passing spaces in quotes <goldbb2@earthlink.net>
Re: quasi xml to xml <bart.lateur@skynet.be>
Re: Reformat Chain <goldbb2@earthlink.net>
Re: strangeness (bug?) in regexp handling <goldbb2@earthlink.net>
Re: warnings and $a, $b <goldbb2@earthlink.net>
Re: what am I doing wrong ? <bart.lateur@skynet.be>
Re: Yet another fork question <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 06 Oct 2001 01:01:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: "chdir" argument as a variable...
Message-Id: <3BBE903D.224E7484@earthlink.net>
Bruce Schreiber wrote:
>
> I would like to change directory within a script where the directory
> name is taken from a list in a text file, i.e.
#!/usr/local/bin/perl -w
use strict;
> $file = 'list.txt'; # Name the file
my $file = 'list.txt';
> open(INFO, $file); # Open the file
You need to add error checking with this line.
> @lines = <INFO>; # Read it into an array
> foreach $i (@lines) { # Read each line of the array
Don't slurp, do read in a line at a time. Don't use global variables,
do use lexical variables.
while( my $i = <INFO> ) {
> #chdir $i
Do you have any directories named "dirA\n" ? Do don't think so.
You need to chomp your lines before using them.
chomp $i;
chdir $i or die "Couldn't chdir $i: $!";
> open(OUT,"> command$i"); # Open output file
> print OUT "FILE $i.unl"; # Print text to output file
> print OUT "DELIMITER '|' 20\;\n"; # Print text to output file
> print OUT "INSERT INTO sales_temp\;\n"; # Print text to output file
> close(OUT); # Close output file
You're missing a \n, unless you want to get something like:
FILE fileA.unlDELIMITER '|' 20
INSERT INTO sales_temp
Also, you don't need to backslash semicolons inside of quotes. If what
you want is a literal backslash in your output, you need to backslash
the backslash. I would write the above bit of code as:
open( OUT, ">", "command$i" ) or die "Couldn't open command$i: $!";
print OUT <<END_OF_COMMAND;
FILE $i.unl
DELIMITER '|';
INSERT INTO sales_temp;
END_OF_COMMAND;
close OUT or die "Couldn't close 'command$i': $!";
> #chdir '..';
chdir ".." or die "Couldn't chdir '..' : $!";
> }
> close (INFO); # Close array
>
> So that every line within 'list.txt' is the name of an actual
> directory below the script directory. So if 'list.txt' is:
>
> dirA
> dirB
> dirC
>
> Then each iteration of the foreach loop will create the file
> "commanddirA" (or whatever is next) in the directory "dirA" (which
> does exist) and then go back to the original script directory. I've
> tired quotes, double quotes, parentheses, nothing seems to work. (Yes,
> I know the lines are commented out)
>
> Can anybody help me with this?
By the way, if I were doing it, I would leave out the chdir()s entirely,
and create the files by doing:
open( OUT, ">", "$i/command$i" )
or die "Couldn't open $i/command$i: $!";
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: 5 Oct 2001 21:44:18 -0700
From: balsellathurai@hotmail.com (bal)
Subject: activeperl, windows 98 & apache
Message-Id: <58504532.0110052044.625c75e4@posting.google.com>
Hi
I have installed the apache server and activeperl. i have the
following sctipt:
#!C:\perl\bin\perl.exe
print("<HTML> <HEAD> <TITLE> DFS</TITLE></head>title<body>body goes
here</body>/html>");
I have put the script in the default cgi-bin directory (under
C:\apache\cgi-bin) and when I run with :
http://localhost/cgi-bin/perltest.pl
I get the following error in the browser:
Internal Server Error
According to the error log I get:
[Sat Oct 06 05:19:02 2001] [error] [client 127.0.0.1] malformed header
from script. Bad header=<HTML> <HEAD> <TITLE> title</TIT:
c:/apache/cgi-bin/perltest.pl
Any ideas?
Much appreciated.
thanks
Bal
------------------------------
Date: Sat, 06 Oct 2001 09:49:07 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: activeperl, windows 98 & apache
Message-Id: <smktrt814funoo71j092j0igekgau74qke@4ax.com>
bal wrote:
>#!C:\perl\bin\perl.exe
>print("<HTML> <HEAD> <TITLE> DFS</TITLE></head>title<body>body goes
>here</body>/html>");
>
>I have put the script in the default cgi-bin directory (under
>C:\apache\cgi-bin) and when I run with :
>
>http://localhost/cgi-bin/perltest.pl
>
>I get the following error in the browser:
>
>Internal Server Error
1) You forgot to print the content-type header, and a blank line (= 2
newlines), before you print anything else
print "Content-type: text/html\n\n";
Most CGI libraries have a shortcut for that.
2) be sure that you patrh to perl in the shebang line is exact, or
replace it with plain "perl". Apache is picky on whether it can find the
executable exactly where you specify it.
--
Bart.
------------------------------
Date: 6 Oct 2001 01:36:41 -0700
From: dmonterrubio@terra.es (BorisVian)
Subject: DBI test error
Message-Id: <fb3ce80.0110060036.6b07795a@posting.google.com>
Hi, again
I have installed DBI 1.17 with postgresql 7.1.2.
DBI test returns me :
t/basics .................... ok
t/dbidrv .................... ok
t/examp ..................... Modification of a read-only value
attempted at blib/lib/DBI.pm dubios
At blib/lib/DBI.pm, the test is trying to make a fetchrow_hashref.
Can anyone helps me?
------------------------------
Date: Sat, 06 Oct 2001 00:48:58 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Help -- array/hash headaches
Message-Id: <3BBE8D3A.D5D3B5DC@earthlink.net>
viscido@u.washington.edu wrote:
[snip]
> At the top of the program, I have the following lines to initialize
> things, since "your school mates last time step" at time 0 must be "no
> one" (or something similar).
If this is the top, then you are missing something:
#!/usr/local/bin/perl -w
use strict;
Always, yes, *always*, use -w and "use strict;" unless you know
precisely why they are there, and know the exact reasons to not have
them enabled. And even when you do want them off, you can disable them
for a particular lexical scope.
> for ($fish=1, $fish <= $n, $fish++) {
> # For each fish, set its $schoomates{$fish} to "empty"
> $schoolmates{$fish} = [];
> }
You already know that those should be semicolons not commas, but I feel
I should mention that the syntax this would be if it was right is C-ish,
not perl-ish. A more perlish idiom is:
for my $fish ( 1 .. $n ) {
$schoolmates{$fish} = [];
}
Or even:
@schoolmates{ 1 .. $n } = map [], 1 .. $n;
Though NOT:
@schoolmates{ 1 .. $n } = ([]) x $n;
Well, that last would be a perlish idiom, if it were correct. Seeing
why it's wrong is an exercise for the reader.
You could leave out this initialization loop entirely, if you change the
line which says:
@lastmates = @{ $schoolmates{$fish} };
To:
@lastmates = @{ $schoolmates{$fish} ||= [] };
Which will initialize it to [] iff it is not already initalized.
> (Note, I have tried this using a "ghost number" (-9999) and the
> problems listed below do not go away.)
>
> Now, deeper into the program, we build a list of groups, as a
> hash called %grp. Thus, $grp{1} = the members of group 1. $grp{2} =
> the members of group 2, and so on. Again, each hash element is an
> array, so if group 1 contains 1, 2, 4, and 7, then $grp{1} = [1, 2, 4,
> ]. Etc.
>
> Here is how I go about finding the current list of school mates for
> individual x, and then read in the previous list of school mates:
>
> foreach $fish ( @{ $grp{$key} } ) {
> # We can store for each fish, its school mates
> # for this frame. We "remove" the $fish element
> # -- you're not your own group mate.
> # First set the array
> my @mate_array = @{ $grp{$key} };
>
> # Remove the focal fish using a subroutine written
> # specifically for that purpose
> my @mates = remove(@mate_array, $fish);
Using grep directly is likely to be faster than some kind of "remove"
sub. Code the above line like this:
my @mates = grep { $_ != $fish } @{ $grp{$key} };
And, unless you need it elsewhere, get rid of the @mate_array variable.
The thing is, if you don't do something like this, you end up repeatedly
copying arrays, which is rather slow.
> # Figure out who his mates were last frame.
> @lastmates = $schoolmates{$fish};
It's been mentioned to you that this should be:
my @lastmates = @{ $schoolmates{$fish} };
> .....
> <then lots of other programming here that doesn't mean
> much for this question>
>
> # Now set up the schoolmates for the next cycle with
> # the current @mates list, so we can compare next time
> # step
> $schoolmates{$fish} = [ @mates ];
Since @mates is a lexical variable, whose scope is the inside of this
block, it is perfectly safe to take a reference to it for this assigment
statement:
$schoolmates{$fish} = \@mates;
This will be faster, as it does not do any array copying, nor create a
new anonymous array.
Another thing you might consider doing, though I think it might be
slower, would be:
@{$schoolmates{$fish}} = @mates;
> print LOG "$frame $fish @mates <--- @lastmates\n";
> }
Note that on each iteration inside this loop, you are reading from
%schoolmates, and then writing back to it. If, inside the ommited code,
you refer to the schoomates of any other fish than the current $fish,
then you may end up getting wierd funky results. The correct thing to
do in such cases is to make a copy before the loop, and within the loop
read from the copy and write to the target [or use the loop to create a
modified copy, then after the loop move that to the target]
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Fri, 05 Oct 2001 11:08:09 +0000
From: NOSPAM@mail.ru
Subject: How to change date of a file
Message-Id: <s25rrtkm8s6evrgqpkkgv10lael19a1l9e@4ax.com>
I need to be able to change date of a file, using a perl script. Have
no idea of the functions and params to call.
Thanks for your help :)
Igor V.P.
------------------------------
Date: Sat, 06 Oct 2001 07:35:18 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: How to change date of a file
Message-Id: <3BBEB4BA.2BA8F24D@acm.org>
NOSPAM@mail.ru wrote:
>
> I need to be able to change date of a file, using a perl script. Have
> no idea of the functions and params to call.
Which operating system and which date?
perldoc -f stat
perldoc -f lstat
perldoc -f utime
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 06 Oct 2001 03:19:50 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: How to change date of a file
Message-Id: <comdog-1BC6C0.03195006102001@news.panix.com>
In article <s25rrtkm8s6evrgqpkkgv10lael19a1l9e@4ax.com>, NOSPAM@mail.ru
wrote:
> I need to be able to change date of a file, using a perl script. Have
> no idea of the functions and params to call.
perhaps you seek utime().
http://www.perldoc.com/perl5.6.1/pod/func/utime.html
--
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html
------------------------------
Date: Fri, 05 Oct 2001 12:55:06 +0000
From: NOSPAM@mail.ru
Subject: Re: How to change date of a file
Message-Id: <5dbrrtcl2mf99nbcescnsn7eh93f605qi0@4ax.com>
On Sat, 06 Oct 2001 03:19:50 -0500, in comp.lang.perl.misc you wrote:
>In article <s25rrtkm8s6evrgqpkkgv10lael19a1l9e@4ax.com>, NOSPAM@mail.ru
>wrote:
>
>> I need to be able to change date of a file, using a perl script. Have
>> no idea of the functions and params to call.
>
>perhaps you seek utime().
>
> http://www.perldoc.com/perl5.6.1/pod/func/utime.html
Yes, but it doesn't work, the time is *not* changed :-(
#!/usr/bin/perl
$now = time;
$file = "/path/to/file.tst";
utime $now, $now, $file;
1;
IV
------------------------------
Date: 6 Oct 2001 01:06:48 GMT
From: fxn@retemail.es (F. Xavier Noria)
Subject: Re: How to have portable email script?
Message-Id: <9pllf8$5d5jq4@news1s.iddeo2.es>
On Fri, 05 Oct 2001 17:16:48 -0700, BCC <a@b.c> wrote:
: I need to write a simple cgi script that takes form elements and then
: emails them to one address.
:
: Easy! In -unix-. But how to do this in windows? The client who wants
: this needs to temporarily have the script running on an NT machine, but
: then moved to a unix machine, which is my home environment. Far as I
: know, windows doesnt have sendmail, and has some nasty windows objects
: that are normally used.
:
: Can someone offer some suggestions or docs or code on how to send email
: in perl on a windows machine?
Check
http://www.activestate.com/PPMPackages/5.6plus/
and choose a module, Mail::Sendmail is nice.
-- fxn
------------------------------
Date: 5 Oct 2001 20:53:08 -0500
From: mtaylor@lrim.com (Mark Taylor)
Subject: Re: How to have portable email script?
Message-Id: <Xns9131D72DA620Bmaintainersetifaqorg@128.242.171.114>
BCC <a@b.c> wrote in <3BBE572A.85123AE4@b.c>:
>
>Thanks Mark!
>
>Mail::Sendmail wouldnt happen to be installed by default with activeperl
>would it? :P
>
>The windows machine I have to use is on our ISP and I have no idea what
>they have installe on it. This is a problem I believe.
>
>B
>
I'm quite sure it's not, but see the post from F. Xavier Noria. Thanks F.
for that additional info.
Mark
______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net
------------------------------
Date: 5 Oct 2001 23:22:02 -0700
From: dkphani@yahoo.com (Phanidhar)
Subject: How to interpret ??
Message-Id: <605e0b10.0110052222.7bbc06e7@posting.google.com>
Hi All,
i have the following perl code.can someone help me out in
interpreting the third and fourth line of the following code.
$dirName = $ARGV[0];
$fileName = $ARGV[0];
$dirName =~ s@[^\\/]*$@@;
$fileName =~ s@.*[\\/]@@;
print $dirName;
Thanks in advance.
Phanidhar
------------------------------
Date: Sat, 06 Oct 2001 02:38:28 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: How to interpret ??
Message-Id: <3BBEA6E4.B645213E@earthlink.net>
Phanidhar wrote:
>
> Hi All,
> i have the following perl code.can someone help me out in
> interpreting the third and fourth line of the following code.
>
> $dirName = $ARGV[0];
> $fileName = $ARGV[0];
> $dirName =~ s@[^\\/]*$@@;
The expression [^\\/]* matches any number of NON forward- and back-
slashes. The expression $ matches the end of the line.
So, [^\\/]*$ matches all the NON forward- and back- slashes which occur
just before the end of the line. As this is used as the patterm in
EXPR=~s/pattern/replace/, it results in whatever's matches it in
$dirname getting replaced with the empty string.
> $fileName =~ s@.*[\\/]@@;
.* matches as much as it can. [\\/] matches a single forward- or back-
slash. So .*[\\/] matches as much as it can, up to the last slash in
the string. It's in a s/expr/replace/, with an empty replace part, so
it removes everything up to and including the last slash from $fileName.
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Sat, 06 Oct 2001 03:23:35 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: How to interpret ??
Message-Id: <comdog-848B87.03233506102001@news.panix.com>
In article <605e0b10.0110052222.7bbc06e7@posting.google.com>,
dkphani@yahoo.com (Phanidhar) wrote:
> i have the following perl code.can someone help me out in
> interpreting the third and fourth line of the following code.
> $dirName = $ARGV[0];
> $fileName = $ARGV[0];
> $dirName =~ s@[^\\/]*$@@;
> $fileName =~ s@.*[\\/]@@;
use File::Basename qw(basename dirname);
the third line means:
$dirName = dirname( $ARGV[0] );
and the fourth line means:
$fileName = basename( $ARGV[0] );
for more info see the docs:
http://www.perldoc.com/perl5.6.1/lib/File/Basename.html
good luck :)
--
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html
------------------------------
Date: 05 Oct 2001 22:35:43 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Multiplexing strings - one line only
Message-Id: <m3y9mph4w0.fsf@mumonkan.sunstarsys.com>
Benjamin Goldberg <goldbb2@earthlink.net> writes:
> Joe Schaefer wrote:
> >
> > $out=(sub{$^A="",formline"^$_"x@_."~~",@_ for"<"x(shift()-1)}
> > ->($n, map $_,@str),$^A); # map to avoid side-effects
>
> Surely @{[@str]} would be better than map $_,@str, both for speed, and
> because it's fewer letters. [At least, I think it should be faster...]
Somehow the obvious escaped me %^@
> And have you considered that this destroys $^A ? You need to localize
> it, or at least assign back to it it's original value when you're done.
> This can be done easily enough with:
> ($^A, $out) = ($^A, ...);
Saying formline() destroys $^A is to me a bit like saying system()
destroys $?, but I see your point in the context of this thread.
($out)=map{local$^A="",formline"^@{['<'x($n-1)]}"x@$_."~~",@$_}[@str];
--
Joe Schaefer "There are two times in a man's life when he should not
speculate: when he can't afford it, and when he can."
--Mark Twain
------------------------------
Date: Sat, 06 Oct 2001 04:00:48 GMT
From: Dave Tweed <dtweed@acm.org>
Subject: Re: Problem with backticks and passing spaces in quotes
Message-Id: <3BBE809A.E96A248A@acm.org>
James Moore wrote:
> Every variant of quoting I've tried so far seems to end up breaking the
> "text 20,80 asdf" bit into three seperate arguements passed to convert
> itself, and I need it to remain one argument.
I don't think this is a perl problem. Under cygwin (perl 5.6.1), I just
tried the following:
In the file showargs.pl:
print "'$_'\n" for @ARGV;
In the file backticks.pl:
$d->{string} = 'asdf';
my $pictureDataCmd = "perl showargs.pl convert -fill white -font
lucidasanstypewriter-bold-24 -pointsize 90 -draw \"text 20,80 $d->{string}\"
imagefile -";
my $pictureData = `$pictureDataCmd`;
print $pictureData;
And the results:
~/temp/perl>perl backticks.pl
'convert'
'-fill'
'white'
'-font'
'lucidasanstypewriter-bold-24'
'-pointsize'
'90'
'-draw'
'text 20,80 asdf'
'imagefile'
'-'
I think you need to check what shell actually gets used by `` on your
system, and how it's handling the quotes. Also, make sure $pictureDataCmd
is all one line, without any embedded newlines.
-- Dave Tweed
------------------------------
Date: Sat, 06 Oct 2001 01:26:42 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Problem with backticks and passing spaces in quotes
Message-Id: <3BBE9612.8F2A01B9@earthlink.net>
James Moore wrote:
>
> I'd like to use the 'convert' utility (part of ImageMagick) from inside
> some backquotes. From a shell command line I'm doing :
>
> convert -fill white -pointsize 90 -draw text\ 20,80\ asdf
> /tmp/dis.jpg - > /tmp/fo.jpg
>
> I'd like to do something similar from perl:
>
> my $pictureDataCmd = "convert -fill white -font
> lucidasanstypewriter-bold-24 -pointsize 90 -draw \"text 20,80
> $d->{string}\" imagefile -";
> my $pictureData = `$pictureDataCmd`;
>
> Every variant of quoting I've tried so far seems to end up breaking
> the "text 20,80 asdf" bit into three seperate arguements passed to
> convert itself, and I need it to remain one argument.
The quotes and escaping you have above looks like it *should* work.
> Suggestions?
Here's an [untested] variant on the above, which also *should* work:
my $pictureData = readpipe(join(" ", qw(
convert -fill white -font lucidasanstypewriter-bold-24
-pointsize 90 -draw), qq["text 20,80 $d->{string}"],
qw(imagefile -)));
if( $! or $? ) {
die "couldn't fork: $!" if $!;
my ($sig, $ret) = ($?&255, %?>>8);
die "convert died from signal $sig" if $sig;
die "convert exited with code $ret" if $ret;
}
readpipe is the perl builtin which `` and qx// both call.
A slightly better solution would be to avoid having the shell interpret
anything at all... to do this, you would need to use either system or
exec, or something which uses one of those:
use IPC::Open2;
my $pid = open2(my($rd), "&<STDIN", qw(
convert -fill white -font lucidasanstypewriter-bold-24
-pointsize 90 -draw), qq[text 20,80 $d->{string}],
qw(imagefile -));
my $pictureData = do { local $/; <$rd> };
close $rd or die $!;
if( waitpid $pid, 0 ) {
my ($sig, $ret) = ($?&255, %?>>8);
die "convert died from signal $sig" if $sig;
die "convert exited with code $ret" if $ret;
} else {
die "Error in waitpid($pid,0) : $!";
}
Note that open2 starts the process via system(@list), which is
gaurunteed not to call the shell to parse it's arguments.
It's too bad that readpipe isn't written to take either a list or a
string [like system and exec are], and avoid calling the shell in such
situations.
If that were the case, we could do:
my $pictureData = readpipe(qw(
convert -fill white -font lucidasanstypewriter-bold-24
-pointsize 90 -draw), qq[text 20,80 $d->{string}],
qw(imagefile -));
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Sat, 06 Oct 2001 09:44:52 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: quasi xml to xml
Message-Id: <8gjtrt4d221ek6i8l71gcgijd6o6099dph@4ax.com>
Blossom Coryat wrote:
>I have some files in a quasi xml format that's looks like somthing like
>this:
>
><start>
><publication>
>AD
><issue>
>March 15, 2001
><volume>
>Vol. 343, No. 51
><copyright>
>Copyright 2001
><end>
>
>I would like to store tags and and its associted content into an array so
>that later I can use this array with XML Generator to create a well
>formed xml accoring to my dtd. How should i approach this prblem? Any
>insight would be appreciated.
I'd expect that HTML::Parser, or (my preference) HTML::TokeParser, can
process it.
Personally, I'd rather put them in a hash. Like this:
use HTML::TokeParser;
my $p = new HTML::TokeParser(\*DATA);
use Data::Dumper;
my($record, $ref);
while(my $t = $p->get_token) {
if((my $start = $t->[0] eq 'S' && $t->[1] eq 'start') ..
(my $end = $t->[0] eq 'S' && $t->[1] eq 'end')) {
if($start) {
$record = {};
} elsif($end) {
print Dumper $record;
} elsif($t->[0] eq 'S') {
$ref = \($record->{$t->[1]});
} elsif($t->[0] eq 'T') {
$$ref .= $t->[1];
}
}
}
__DATA__
<start>
<publication>
AD
<issue>
March 15, 2001
<volume>
Vol. 343, No. 51
<copyright>
Copyright 2001
<end>
##CUT HERE##
You might want to get rid of the extra newlines at the start and end of
the texts. And, I don't know, perhaps you do want an array instead of a
hash?
my @record = map { [ $_ => $record->{$_} ] } keys %$record;
--
Bart.
------------------------------
Date: Fri, 05 Oct 2001 23:16:44 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Reformat Chain
Message-Id: <3BBE779C.7F83F28F@earthlink.net>
Thomas Bätzler wrote:
>
> On 5 Oct 2001, houda.araj@cogmedia.com (Houda Araj) wrote:
>
> >I would like to reformat an (input chain) to an output chain. The
> >input chain has two lines. The first line is plain english words and
> >the second is grammatical categories. What I want to do is to put
> >grammatical label in capital letter after each word.
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my @words = split /\s+/, <DATA>;
>
> my @notes = split /\s+/, <DATA>;
>
> print join( ' ', map { $_ . "_" . uc( splice @notes,0,1 ) } @words );
splice(@something, 0, 1) is another name for shift(@something).
print "@{[map { $_ . "_" . uc shift @notes } @words]}\n";
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Fri, 05 Oct 2001 23:30:40 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: strangeness (bug?) in regexp handling
Message-Id: <3BBE7AE0.DC744088@earthlink.net>
Vladimir Volovich wrote:
>
> Thomas, David,
[snip]
> TB> Maybe you could explain what you're trying to achieve first, next
> TB> time.
>
> i was removing everything from a string after a semicolon, except
> preserving entities like é
$a = "foo é bar ; this part removed";
1 while $a =~ /&(?:\w+|#?\d+);?/gc;
substr($a, pos($a)) =~ s/;.*//;
# $a should now be "foo é bar "
Remember that & almost always is interpreted as the beginning of an
entity, even if there isn't a closing ; to mark the end of it. Also, a
pound mark, #, is only allowed at the beginning of an entity, and only
for numeric entities.
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Fri, 05 Oct 2001 22:46:34 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: warnings and $a, $b
Message-Id: <3BBE708A.75935FC4@earthlink.net>
Mr. Sunblade wrote:
>
> Hi all,
>
> I recently wrote and published an OO module that has it's own "sort"
> method.
> I've recently been toying with the sort method to make it more
> flexible, allowing coderefs to be passed in.
>
> It would look something like this:
>
> #/usr/local/bin/perl -w
> use strict;
> my $obj = Set::Array->new(qw(1 4 2));
> $obj->sort(sub{$a <=> $b});
sub { $a <=> $b } is a coderef.
[snip]
> my @sorted = sort{ $a <=> $b } @list;
{ $a <=> $b } is a block, not a coderef.
[snip]
> First, if $a and $b are recognized as special variables and don't
> generate the usual warnings, why am I getting warnings in my version?
They're probably recognized as special variables only when in a block
which is the first argument to sort, *not* when they're in arbitrary
code.
> Second, if I'm getting warnings in my version because I redefined
> 'sort()', why am I only getting the 'once' warning and not the
> 'uninitialized' warning or the fatal 'explicit package name required'
> error message?
Why would you get an 'uninitialized' warning? Remember that an
anonymous sub is deferred code, not executed where its declared, but
executed when it's specifically told to execute. I'm sure that inside
your sort sub, you *do* initialize them.
And $a and $b are specifically excluded from needing explicit package
names.
> PS - Yes, I know I can ignore this with -- no warnings 'once' -- I was
> just hoping to avoid it altogether
A better way would be to do as you would for passing a sub made in one
package to a call to CORE::sort in another... that is, give the sub a
prototype of ($$) and pass the arguments in as normal sub arguments.
This would make your code be:
$obj->sort( sub ($$) { $_[0] <=> $_[1] } );
And instead of doing:
local *a = \${caller . "::a"};
local *b = \${caller . "::b"};
$a = $foo; $b = $bar; $comparison = $thesub->();
you would do:
$comparison = $thesub->($foo, $bar);
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Sat, 06 Oct 2001 02:04:37 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: what am I doing wrong ?
Message-Id: <0apsrt0d0f7vkovur47bjq8nlvugir499p@4ax.com>
Tana wrote:
>I have a simple perl script like this:
>#!/usr/bin/perl
>use CGI;
>$cgi=new CGI;
>print $cgi->header();
>print "User ID: $user_id";
>
>And I call the script from internet browser like this:
>http://hepek.com/cgi-bin/test.pl?user_id=100
>
>I would expect to see a value for user_id (100) as a result, but value is emty.
>All I see is "User ID: ".
This isn't PHP. In Perl, you have to explicitely ask for the form
variables. You can ask for them one by one, using the param method;
fetch them all into a hash, with the Vars method, or make them plain
variables into a separate package with import_names, default package Q::
although the examples in the docs use "R" (R::). Actually, I can't see
any good reason to do that, $form->{user_id} isn't that much harder to
type than $Q::user_id.
--
Bart.
------------------------------
Date: Fri, 05 Oct 2001 21:07:00 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Yet another fork question
Message-Id: <3BBE5934.79E5305E@earthlink.net>
Randal L. Schwartz wrote:
>
> >>>>> "Anno" == Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> writes:
>
> Anno> That makes sense. There had to be a disadvantage in letting
> Anno> many processes accept connections on a single socket.
>
> And it's illegal on at least one place I was testing it, when
> I was building the code for:
>
> <http://www.stonehenge.com/merlyn/WebTechniques/col34.html>
>
> I don't recall where now, but I think it was sunos4. Linux handled it
> just fine.
>
> >> Apache avoids this by having some kind of semaphor so at most one
> >> of the children gets to sleep on the accept, and the other is
> >> sleeping waiting for the semaphor. As soon as one connection is
> >> accepted the semaphor is released for the next child process.
>
> Anno> ...so the idle children in effect queue up at the socket. Makes
> Anno> sense again, and a semaphore is perfectly suited for the
> Anno> purpose.
>
> And of course I needed a filehandle to flock that all the kids shared.
> What better one to flock than the socket itself! Worked great.
Don't some systems flock()s associate the lock with the filehandle, not
the process, even if you've forked? eg, if you do:
flock( FH, LOCK_EX );
fork or do{ flock(FH, LOCK_UN); exit };
# FH is now unlocked [in parent] !
So, while you're solution *sounds* like a good idea, it can go bad on
some systems, in an flock-dependent way.
I'm not sure if this occurs with perl's flock(), or only when using
fcntl to get locks, but it is something to watch out for.
Perhaps by using something like IPC::Semaphore, one might have higher
hopes of portability. [Whether they are valid hopes, I don't know].
use IPC::Semaphore;
my $sem = IPC::Semaphore->new(IPC_PRIVATE, 1, 0600)
# $sem->op(0, +1) is this needed?
... forking ...
... while(1) died = wait(), fork a new child ...
sub child_does {
{
$sem->op(0, -1, 0)
my $slave = $master->accept
$sem->op(0, +1, 0)
.... other stuff ....
} continue { redo if ++$did < max }
}
Obviously you need to have the SysV functions semop and semget, which
may be problematic, but if you *do* have them, I would expect them to
behave in a more consistant manner than flock.
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
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 1883
***************************************