[18819] in Perl-Users-Digest
Perl-Users Digest, Issue: 987 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 25 14:05:55 2001
Date: Fri, 25 May 2001 11:05:14 -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: <990813914-v10-i987@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 25 May 2001 Volume: 10 Number: 987
Today's topics:
Re: a newbie question: how to represent binary trees ?? (Mark Jason Dominus)
Re: Array slice: how about the remainder? <ren@tivoli.com>
Re: Calling function reference nobull@mail.com
Re: Calling function reference nobull@mail.com
Re: Comparing objects nobull@mail.com
Re: Constant -vs- Variable File Handles? nobull@mail.com
Re: Converting spaces to plus signs <mischief@velma.motion.net>
Re: Converting spaces to plus signs <jesse@uchicago.edu>
Re: dbi, fork and ipc-shareable - having problems <uri@sysarch.com>
Emacs modules for Perl programming (Jari Aalto+mail.perl)
GD::Graph, CGI <jlien@friedwire.com>
Re: generates random text file (with structural framewo <buggs@geekmail.de>
Re: generates random text file (with structural framewo (Randal L. Schwartz)
Re: getting perl internal calculated hash value? (Mark Jason Dominus)
Re: getting perl internal calculated hash value? (Mark Jason Dominus)
hash array (Andrey)
Re: hash array (Mark Jason Dominus)
Re: hash array (Steven M. O'Neill)
Re: hash array (Mark Jason Dominus)
Re: hash array nobull@mail.com
Re: Help slim down Perl code (Sweth Chandramouli)
Re: Help slim down Perl code <uri@sysarch.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 25 May 2001 17:03:31 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: a newbie question: how to represent binary trees ??
Message-Id: <3b0e9054.1ec8$212@news.op.net>
In article <EElP6.8890$ce.6922809@newsrump.sjc.telocity.net>,
mark <hamzay@telocity.nospam.com> wrote:
>Hi all,
>
>I have one newbie question: How can I represent binary trees in Perl.
The best answer will depend on what you are planning to do with the
binary trees afterwards. Usually, in Perl, there is an answer that
will be more efficient and simpler to program than if you used a
binary tree.
Can you tell us a little about what you are planning to do with the
binary trees?
--
@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: 25 May 2001 10:33:05 -0500
From: Ren Maddox <ren@tivoli.com>
Subject: Re: Array slice: how about the remainder?
Message-Id: <m3ofsha1fy.fsf@dhcp9-172.support.tivoli.com>
On Fri, 25 May 2001, krahnj@acm.org wrote:
> Chris Stith wrote:
>
> Ok, here's the code that was copied from usenet and tested. Some
> changes where made for code that modified the original @array and/or
> @chosen. Following the code is the results I got.
>
>
> my @array = ( 'AA' .. 'DV' );
> my @chosen = (0,3,5,7,21,35,63,66,68);
>
> print 'Size of @array = ', scalar @array, "\n\@array = @array\n";
> print 'Size of @chosen = ', scalar @chosen, "\n\@chosen =
> @chosen\n\n";
>
> sub Lin {
> # original post by John Lin
> my @go_picnic = @array[ @chosen ]; # those who are chosen go picnic
> my @not_chosen = grep { !grep( $_, @chosen ) } 0 .. $#array;
The problem here is the inner grep. It wants to search @chosen for
non-matches from 0 .. $#array, but has two problems. First, $_ is
meant to be the number from the outer array but in actuality is the
number from @chosen. Second, it doesn't compare to anything, but just
tests the value. Here is a fix (untested):
my @not_chosen = grep { my $x = $_; !grep $x eq $_, @chosen } 0
.. $#array;
> my @stay_home = @array[ @not_chosen ];
> return "@stay_home\n@go_picnic";
> }
>
> sub nobull1 {
[snipped, worked]
> sub nobull2 {
[snipped, worked]
> sub Siegel {
> # Anno Siegel's reply to nobull's first post
> my @go_picnic = @array[ @chosen ]; # those who are chosen go picnic
> my $aux = '';
The problem here is that $aux is only as long as the maximum value
from @chosen. I'm not certain what the best way to deal with this is,
but one solution is to pre-extend $aux:
my $aux = pack "b" x @array;
> vec( $aux, $_, 1 ) = 1 for @chosen;
> my @stay_home = @array[ grep vec( ~ $aux, $_, 1 ), 0 .. $#array ];
> return "@stay_home\n@go_picnic";
> }
>
> sub Godzilla {
> my @go_picnic = @array[ @chosen ]; # those who are chosen go picnic
> my (@stay_home);
> my (@array2) = @array;
> my (@chosen2) = @chosen;
> foreach $chosen (@chosen2)
> {undef ($array2[$chosen2]); }
Oops... that should be:
{undef ($array2[$chosen]); }
Too bad you didn't run under strictures (and declare your loop
variables), as that would have caught this typo. I haven't checked,
but I'm confident that Godzilla's original code did not have this
error.
> foreach $stay_home (@array2)
> {
> if (defined ($stay_home))
> { push (@stay_home, $stay_home); }
> }
> return "@stay_home\n@go_picnic";
> }
>
> sub Dominus {
[snipped, worked]
> sub Stith {
> # Chris Stith's reply to MJD
> my @go_picnic = @array[ @chosen ]; # those who are chosen go picnic
> my @stay_home = ();
> my %chosen = ();
> keys( %chosen ) = @chosen;
The first problem is that assigning to keys doesn't work this way.
It just preallocates hash buckets. This line can be changed to:
@chosen{@chosen} = ();
> for( @array ) {
The second problem is that this loop is looping over the values but
then checking them as if they were indices. Change this line to:
for( 0 .. $#array ) {
> unless( exists( $chosen{$_} ) ) {
> push @stay_home, $_;
and this line to:
push @stay_home, $array[$_];
> }
> }
> return "@stay_home\n@go_picnic";
> }
>
> sub Krahn {
[snipped, worked]
[snipped rest of script and output]
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 25 May 2001 16:24:02 +0100
From: nobull@mail.com
Subject: Re: Calling function reference
Message-Id: <u9wv754fl9.fsf@wcl-l.bham.ac.uk>
Kris Verbeeck <kris.verbeeck@chello.be> writes:
> With a dispatch table, you mean a mapping between a string and a
> hard ref, is that correct? If so, then I would have to know which
> functions are going to be called when I write the script and not
> when I run the script.
Surely you need to know what functions are needed in order to write
the functions in the first place?
> What I'm working on only needs a text file that describes the
> behaviour of the script through a set of rules. The functions being
> called here can be anything.
So, you are saying your configuration file can somehow instruct your
script to import additional functions from other sources.
> Am I missing something here?
Yes, I think you are missing something here, and it is something huge
way out in left-field.
> I don't need this script for something mission critical, just a small
> tool that is general enough to be used in a variety of situations.
I can't be sure because I've not got all the facts but you have used
the magic trigger phrase "small tool that is general". It sounds to
me very like you should be writing your tool as a module and writing
the "configuration file" as a Perl script that uses that module.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 25 May 2001 18:05:57 +0100
From: nobull@mail.com
Subject: Re: Calling function reference
Message-Id: <u9d78x4ave.fsf@wcl-l.bham.ac.uk>
Uri Guttman <uri@sysarch.com> writes:
> >>>>> "n" == nobull <nobull@mail.com> writes:
>
> n> If it is possible to do so the best solution is simply to switch to
> n> using hard references:
>
> n> my $func = 'doit'; # Sybolic ref
> n> my $func = \&doit; # Hard ref
>
> so if that is the best solution, why did you even clear up his symref
> problem?
Because I said "if it is possible to do so". Sometimes the beniefits
of symbolic references and eval outweigh the costs.
> n> If you really want to stick with symbolic references then read on...
>
> the problems with synrefs are well documented.
Indeed, also finite.
> read MJD's varvar pages for some good reasons why they are
> dangerous.
Being careful does not mean trying to understand the possible
consequences while behaving dangerously; it means avoiding dangerous
behavior in the first place. Don't say ``I know it's dangerous, so
I'll be really careful.'' Say ``I know it's dangerous, so I won't do
it.''
Sometime being careful does mean trying to understand the possible
consequences while behaving dangerously. It's all cost-beniefit.
> with subs it is even more dangerous. how can you vet what sub will
> be called if you are passing in any string? in a cgi, that is almost
> certain death.
When writing a script that is not suid and is executed from the
command line then that's an irrelevance. If the user wanted to make
your script behave in a way it was not designed to do then he could
simply make a modified copy.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 25 May 2001 16:31:02 +0100
From: nobull@mail.com
Subject: Re: Comparing objects
Message-Id: <u9u2294f9l.fsf@wcl-l.bham.ac.uk>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> According to Graq <graq@bigfoot.com>:
> > I would like to know if comparing two objects is a valid thing to do.
>
> Since Perl objects are references, the rules for references apply.
It is worth adding that this applies to objects of most but not all
classes. If the author of the class decides that comparison via ==
should have some other semantics for objects of his class then (s)he
can choose just about any semantics (s)he likes using the overload
mechanism.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 25 May 2001 16:08:00 +0100
From: nobull@mail.com
Subject: Re: Constant -vs- Variable File Handles?
Message-Id: <u9y9rl4gbz.fsf@wcl-l.bham.ac.uk>
"Robert" <pettyr_no_spam@hotmail.com> writes:
> my ($FH) = "";
> open ($FH, "< /etc/passwd");
Loose the ="" bit. You want to be using filehandle reference
autovivification[1], not a symbolic reference to the symbol with a
null name.
"use strict" would have caught this mistake for you.
BTW: You can combine the two statements into one:
open (my $FH, '<', '/etc/passwd')
or die "Could not open /etc/passwd: $!";
Since you'd put a space after the < it is clear that you wanted to
make clear that the the < was not part of the filename. You can do
this more elegantly with a 3-arguent open[1].
[1] New in 5.6
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 25 May 2001 15:30:52 -0000
From: Chris Stith <mischief@velma.motion.net>
Subject: Re: Converting spaces to plus signs
Message-Id: <tgsulci57038dc@corp.supernews.com>
AvA <a.v.a@home.nl> wrote:
> <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
Why? We're in a plaintext newsgroup. I don't mind "reading between
the lines", but reading between markup tags should be left for when
I'm maintaining a document.
[snip]
> ______________
> <p>#!/usr/bin/perl -w
> <p>use strict;
> <p>my $string = "This is a line with some
> spaces";
> <p>$string = join"+",(split/\s/,$string);
> <br>
> <br>print "$string\n";
> <p>______________
What's wrong with Abigail's answer of q{y}?
Or, perhaps URI::Escape would do the trick and much more. Or perhaps
the OP should be using CGI.pm in the first place, so there's no need
to escape anything. We can't tell from the OP's post for sure, but
it's a good bet this is a CGI-by-hand question.
If CGI.pm really offends, there are other, smaller modules which
will grab form results or write a URI. CPAN has most of them. I
have a small somewhat tested form results parser that I often
use, but I still recommend CGI for large projects. In fact, CGI.pm
is so good, I don't bother to post mine to PAUSE. CGI.pm is even
in any modern perl installation already.
Chris
--
The purpose of a language is not to help you learn the
language, but to help you learn other things by using the
language. --Larry Wall, The Culture of Perl, August 1997
------------------------------
Date: Fri, 25 May 2001 10:55:17 -0500
From: Jesse James Jensen <jesse@uchicago.edu>
Subject: Re: Converting spaces to plus signs
Message-Id: <3B0E8065.375B3C49@uchicago.edu>
Abigail wrote:
>
> Ken Philbrick (kenphilbrick@mindspring.com) wrote on MMDCCCXXIV September
> MCMXCIII in <URL:news:3B0DA1AC.D0B73D85@mindspring.com>:
> !! I have some strings that contain spaces, but I'd like to convert all the
> !! spaces into plus signs. I've tried various methods using split and
> !! join, but nothing has worked. The FAQ on perl.com doesn't seem to
> !! mention anything about it either. Any ideas?
>
> y
>
You are a character.
------------------------------
Date: Fri, 25 May 2001 17:13:36 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: dbi, fork and ipc-shareable - having problems
Message-Id: <x71ypd2vy8.fsf@home.sysarch.com>
>>>>> "S" == SunRay <nospam@hotmail.com> writes:
S> my $if = IO::File->new(SQLFILE) or die "Couldn't open file: $!\n";
>>
>> SQLFILE is a bareword. it should be quoted.
S> SQLFILE is constant. Perl has yet to complain about me doing this,
S> though perhaps it's bad practice. But I digress...
i must have missed that.
>> think about this. you are trying to share a reference from one program
>> to another. think again. you can't do it. refs are based on pointers to
>> a given programs' address space. that is not something that is
>> guaranteed to be the same in another program space. also who knows what
>> IPC::Sharable is doing to that ref such as stringifying it.
S> That makes sense. Yet strangely, it seems to work *sometimes* - the
S> worst case for a programmer trying to solve a problem. I was
S> getting 4 array refs (later 8, then 12, etc) during some test runs.
S> Was this luck?
the reason it may work sometimes is that you are forking and that makes
a perfect copy of the process image including the memory addresses. so
you may actually have the same addresses for the same thing in two forks
and it would work. but it could change depending on many things and then
it breaks. so it is not a proper solution.
S> That requires storing 4 different arrays (or one huge array), as
S> opposed to one nested array since I've got 4 sql queries returning
S> more than 1 row. I'd create an "array of arrays", but as even the
S> perl cookbook points out (p.91 to be exact), multidimensional
S> arrays can only be implemented as an array of references so I can't
S> even use that.
you could have 4 different shared areas, each dedicated to one
array/fork. the parent can manage them using a single structure. each
area is given a unique name and that is passed as an arg to the sub/loop
that is doing the fork and tells it which area to store its data
in. this needs some work but it seems a reasonable approach. but again,
i think shared memory is a pain.
>> another solution is to use pipes and multiplexes i/o such as Event.pm,
>> IO::Select, Stem or POE. shared memory is a pain.
S> I've looked at other options, but none clearly explain to me (or at
S> all) how to pass *variables* across a filehandle, socket, pipe or
S> whatever.
stem can pass anything over via a message and over a pipe including
complex data structures. all you have to do is serialize the data using
Data::Dumper, storable, or the new Denter.pm module. one goal is for
stem to have a DBI module which would run as a separate process. it
would take requests and send back results via messages. you could have
multiple ones running all the time giving you the parallel queries you
desire. before that gets built you could hack up a simpler designthat
just does what you want. it should be very easy to do.
S> Now I can't help but think that *somebody* out there must have come
S> up with a faster way of running sql queries using DBI. Should I be
S> looking at threads?
i would stay away from threads. they have issues with stability and i
would think that would get worse with dbi in the picture.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: 25 May 2001 15:49:43 GMT
From: <jari.aalto@poboxes.com> (Jari Aalto+mail.perl)
Subject: Emacs modules for Perl programming
Message-Id: <perl-faq/emacs-lisp-modules_990805509@rtfm.mit.edu>
Archive-name: perl-faq/emacs-lisp-modules
Posting-Frequency: 2 times a month
URL: http://tiny-tools.sourceforge.net/
Maintainer: Jari Aalto <jari.aalto@poboxes.com>
Announcement: "What Emacs lisp modules can help with programming Perl"
Preface
Emacs is your friend if you have to do anything comcerning software
development: It offers plug-in modules, written in Emacs lisp
(elisp) language, that makes all your programmings wishes come
true. Please introduce yourself to Emacs and your programming era
will get a new light.
Where to find Emacs/XEmacs
o Unix:
http://www.gnu.org/software/emacs/emacs.html
http://www.xemacs.org/
o Windows
http://www.gnu.org/software/emacs/windows/ntemacs.html
ftp://ftp.xemacs.org/pub/xemacs/windows/setup.exe
o More Emacs resources at
http://tiny-tools.sourceforge.net/emacs-elisp.html
Emacs Perl Modules
Cperl -- Perl programming mode
.ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
.<olson@mcs.anl.gov> Bob Olson (started 1991)
.<ilya@math.ohio-state.edu> Ilya Zakharevich
Major mode for editing perl files. Forget the default
`perl-mode' that comes with Emacs, this is much better. Comes
standard in newest Emacs.
TinyPerl -- Perl related utilities
.http://tiny-tools.sourceforge.net/
If you ever wonder how to deal with Perl POD pages or how to find
documentation from all perl manpages, this package is for you.
Couple of keystrokes and all the documentaion is in your hands.
o Instant function help: See documentation of `shift', `pop'...
o Show Perl manual pages in *pod* buffer
o Load source code into Emacs, like Devel::DProf.pm
o Grep through all Perl manpages (.pod)
o Follow POD manpage references to next pod page with TinyUrl
o Coloured pod pages with `font-lock'
o Separate `tiperl-pod-view-mode' for jumping topics and pages
forward and backward in *pod* buffer.
o TinyUrl is used to jump to URLs (other pod pages, man pages etc)
mentioned in POD pages. (It's a general URL minor mode)
TinyIgrep -- Perl Code browsing and easy grepping
[TinyIgrep is included in the Kit]
To grep from all installed Perl modules, define database to
TinyIgrep. There is example file emacs-rc-tinyigrep.el that shows
how to set up datatbases for Perl5, Perl4 whatever you have
installed
TinyIgrep calls Igrep.el to run the find for you, You can adjust
recursive grep options, ignored case, add user grep options.
You can get `igrep.el' module from <kevinr@ihs.com>. Ask for copy.
Check also ftp://ftp.ihs.com/pub/kevinr/
TinyCompile -- Browsing grep results in Emacs *compile* buffer
TinyCompile is minor mode for *compile* buffer from where
you can collapse unwanted lines, shorten the file URLs
/asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file1:NNN: MATCHED TEXT
/asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file2:NNN: MATCHED TEXT
-->
cd /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/
file1:NNN: MATCHED TEXT
file1:NNN: MATCHED TEXT
End
------------------------------
Date: Fri, 25 May 2001 11:23:42 -0600
From: John Lien <jlien@friedwire.com>
Subject: GD::Graph, CGI
Message-Id: <3B0E951E.A2F4FD05@friedwire.com>
Hi.
I'm trying to use GD::Graph in a CGI script. I've used both modules
separately successfully, but I've had no luck using them together. I
know I'm very close to having this working. The script and versions are
below.
The goal is to call the script in an IMG tag like this:
<img src="cgi-bin/test.pl">
The problem is I just get a broken or not found image symbol.
o If I comment out the header line and run the script like this "perl
test.pl > bob.jpeg" at the prompt, I get a jpeg file that I can view in
my browser. So the script does generate a valid jpeg.
o The script has the same permissions and is in the same location of
other "non GD::Graph" scripts that work.
o I've tried every path I can think of in the SRC tag. I've tested the
absolute url in the browser itself (http://mysite.com/cgi-bin/test.pl)
and the script executes by nothing is retured.
I'd appreciate help you can offer.
Thanks,
John
# Versions
Perl 5.6.0 for sun4-solaris
GD 1.33
GD::Graph 1.33
gd 1.8.4
#
#-- HTML
#
<HTML><HEAD></HEAD><BODY BGCOLOR="#FFFFFF">
Heres the image: <P>
<img src="cgi-bin/test.pl"><P>
</BODY>
</HTML>
#
#-- Script
#
use strict;
use CGI qw(:standard);
use GD::Graph::bars;
use GD::Graph::colour;
use GD::Graph::Data;
my $data = GD::Graph::Data->new([
["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
[ 1, 2, 5, 6, 3, 1.5, 1, 3, 4],
]) or die GD::Graph::Data->error;
my $my_graph = GD::Graph::bars->new();
$my_graph->set(
x_label => 'X Label',
y_label => 'Y label',
title => 'A Simple Bar Chart',
y_max_value => 8,
y_tick_number => 8,
y_label_skip => 2,
# shadows
bar_spacing => 8,
shadow_depth => 4,
shadowclr => 'dred',
transparent => 0,
)
or warn $my_graph->error;
print STDOUT header("image/jpeg");
binmode STDOUT;
print STDOUT $my_graph->plot($data)->jpeg();
------------------------------
Date: Fri, 25 May 2001 17:39:19 +0200
From: buggs <buggs@geekmail.de>
Subject: Re: generates random text file (with structural framework)?
Message-Id: <9eluaq$5k0$05$1@news.t-online.com>
Wenjie ZHAO wrote:
> Hello perl people,
Hoi,
>
> I right now got a task to prototyping some
> XML merging,compressing and exporting taskes which the
> performance is highly depending on the XML
> file contents. I want to simulate the real
> world more accurately and decided to use perl
> program to generate those bulk files. I think
> I select the correct tool, but then
> I found I have little idea for random "plain
> text" generation:? Some inputs from a Dictionary?
Dump a database or Spreadsheet ( Excel ...) as XML.
Format your E-Mail as XML.
Use XHTML gathered from the web
(e.g. cpan.org got the neat little sticker ).
> some Markov chain program?
Assuming, you think of using it for tag generation,
since you don't need the data of the XML
to be structured.
Imagine:
<xmlfoo attribute1="bla" attribute2="foo">DATA</xmlfoo>
You'd take the DATA from the above ( DB, web ...)
and let the Markov chain only do the attributes.
> Thanks a lot!
> Best Regards,
> wenjie
>
No prob' ( but be aware that your question is not really Perl related).
Buggs
------------------------------
Date: 25 May 2001 08:53:19 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: generates random text file (with structural framework)?
Message-Id: <m1r8xd1l3k.fsf@halfdome.holdit.com>
>>>>> "Wenjie" == Wenjie ZHAO <gokkog@yahoo.com> writes:
Wenjie> Hello perl people,
Wenjie> I right now got a task to prototyping some
Wenjie> XML merging,compressing and exporting taskes which the
Wenjie> performance is highly depending on the XML
Wenjie> file contents. I want to simulate the real
Wenjie> world more accurately and decided to use perl
Wenjie> program to generate those bulk files. I think
Wenjie> I select the correct tool, but then
Wenjie> I found I have little idea for random "plain
Wenjie> text" generation:? Some inputs from a Dictionary?
Wenjie> some Markov chain program?
Wenjie> I hope there is some simple solution somewhere.
Wenjie> And you could tell me.
If you want a BNF-driven random text generator, check out my
"spew" program, found in the examples in the Parse::RecDescent
distribution (which you'll need to run it anyway). You can
read a Linux Magazine article about the program at my site
at <http://www.stonehenge.com/merlyn/LinuxMag/col04.html>.
"spew" is ideal for this, because you can essentially mirror your XML
DTD almost line for line as spew input, and then add weights to bias
certain constructions over others. One of my to-do tasks is to parse
a DTD to generate spew input, but I've not gotten that many tuits. :)
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: Fri, 25 May 2001 15:30:52 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: getting perl internal calculated hash value?
Message-Id: <3b0e7a9d.1cc2$125@news.op.net>
Keywords: Tulane, lame, maintenance, peep
In article <slrn9gqsoc.dae.mikee@kensho.eggtech.com>,
Mike Eggleston <mikee@kensho.eggtech.com> wrote:
>I have a desire to look at the hash value that perl has generated
>for the entries in a given hash. I know how to manipulate the
>hash, but I have never before thought of just what perl call
>a given hash.
The "FakeHash" module may interest you.
http://perl.plover.com/FakeHash/
Also, the code in Perl that computes the hash value is the PERL_HASH
macro in the file hv.h; you may want to go look at it.
--
@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: Fri, 25 May 2001 15:32:37 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: getting perl internal calculated hash value?
Message-Id: <3b0e7b06.1cc9$396@news.op.net>
In article <9elcfq$sdm$1@mamenchi.zrz.TU-Berlin.DE>,
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>If by "hash value" you mean the hash key generated out of a string,
>that's documented in perlguts (p. 8 in my printout).
Note that Perlguts is wrong in 5.7.0 and 5.7.1.
--
@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: 25 May 2001 09:58:26 -0700
From: andrey@cs.wayne.edu (Andrey)
Subject: hash array
Message-Id: <fba48be1.0105250858.238e98a8@posting.google.com>
Is there a way to remove all elements from a hash array? I DID search
the faq's and the only reference I could find was
What happens if I add or remove keys from a hash while iterating over
it?
Don't do that.
That's why I'm posting to this group.
And a related question: how does one create an empty hash array?
Thanks in advance.
------------------------------
Date: Fri, 25 May 2001 17:01:34 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: hash array
Message-Id: <3b0e8fdf.1ec1$389@news.op.net>
In article <fba48be1.0105250858.238e98a8@posting.google.com>,
Andrey <andrey@cs.wayne.edu> wrote:
>Is there a way to remove all elements from a hash array?
%hash = ();
>And a related question: how does one create an empty hash array?
They are empty by default. You don't need to 'create' them.
--
@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: 25 May 2001 17:21:05 GMT
From: steveo@panix.com (Steven M. O'Neill)
Subject: Re: hash array
Message-Id: <9em4a1$4e9$1@news.panix.com>
Mark Jason Dominus <mjd@plover.com> wrote:
>In article <fba48be1.0105250858.238e98a8@posting.google.com>,
>Andrey <andrey@cs.wayne.edu> wrote:
>>Is there a way to remove all elements from a hash array?
>
> %hash = ();
Better, worse or same as
undef (%hash);
?
--
Steven O'Neill steveo@panix.com
www.cars-suck.org
------------------------------
Date: Fri, 25 May 2001 17:28:38 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: hash array
Message-Id: <3b0e9645.1f48$356@news.op.net>
In article <9em4a1$4e9$1@news.panix.com>,
Steven M. O'Neill <steveo@panix.com> wrote:
>Mark Jason Dominus <mjd@plover.com> wrote:
>>In article <fba48be1.0105250858.238e98a8@posting.google.com>,
>>Andrey <andrey@cs.wayne.edu> wrote:
>>>Is there a way to remove all elements from a hash array?
>>
>> %hash = ();
>
>Better, worse or same as
>
> undef (%hash);
>
Pretty much the same.
Close enough so that someone who has just learned to empty a hash
doesn't need to be concerned with the differences.
--
@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: 25 May 2001 18:32:50 +0100
From: nobull@mail.com
Subject: Re: hash array
Message-Id: <u97kz549ml.fsf@wcl-l.bham.ac.uk>
steveo@panix.com (Steven M. O'Neill) writes:
> Mark Jason Dominus <mjd@plover.com> wrote:
> >In article <fba48be1.0105250858.238e98a8@posting.google.com>,
> >Andrey <andrey@cs.wayne.edu> wrote:
> >>Is there a way to remove all elements from a hash array?
> >
> > %hash = ();
>
> Better, worse or same as
>
> undef (%hash);
That depends if you want %hash to rember how many buckets it grew to
last time. Usually how big a has grew last time you used it is a good
predictor of how big it will grow next time so remembering this is the
"right thing" so using undef() is the wrong thing.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 25 May 2001 15:24:32 GMT
From: sweth+perl@gwu.edu (Sweth Chandramouli)
Subject: Re: Help slim down Perl code
Message-Id: <QOuP6.26932$G5.5213773@news1.rdc1.md.home.com>
In article <x74rua4h0c.fsf@home.sysarch.com>,
Uri Guttman <uri@sysarch.com> wrote:
>you can define a closure that has the debug flag as a value and call
>that instead
Ooh. I like that. I'll have to try it...
>you can manage the namespace with the Exporter which is safer than you
>doing it yourself.
??? I'm not sure what you mean here. Could you give an
example?
-- Sweth.
--
Sweth Chandramouli ; <sweth+perl@gwu.edu>
------------------------------
Date: Fri, 25 May 2001 16:09:30 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Help slim down Perl code
Message-Id: <x77kz52yx2.fsf@home.sysarch.com>
>>>>> "SC" == Sweth Chandramouli <sweth> writes:
SC> In article <x74rua4h0c.fsf@home.sysarch.com>,
SC> Uri Guttman <uri@sysarch.com> wrote:
>> you can define a closure that has the debug flag as a value and call
>> that instead
SC> Ooh. I like that. I'll have to try it...
it makes for very clean code. here is the import sub from Stem::Debug
which does this very thing. you can specify the name of the sub that
will be exported into the user's namespace. also several other params
can be specified as debug info.
you create a debug/trace sub with calls like this:
use Debug ; # vanilla sub named Debug with all defaults
use Debug 'sub' => 'my_trace', 'label' => 'trace', 'log' => 'call' ;
you can create multiple debug subs in one module this way, each with
different behaviour and defaults.
sub import {
my( $class, %dbg_args ) = @_ ;
my $use_pkg = caller ;
my $sub = $dbg_args{ 'sub' } || 'Debug' ;
my $level = $dbg_args{ 'level' } || 10 ;
my $label = $dbg_args{ 'label' } || 'debug' ;
my $log = $dbg_args{ 'log' } || 'debug' ;
my $env = $dbg_args{ 'env' } ;
my $env_level = $dbg_args{ 'env_level' } || 0 ;
my $prefix = $dbg_args{ 'prefix' } ;
no strict 'refs';
*{ "${use_pkg}::$sub" } = sub (@) {
return if $env && $Env{ $env } < $env_level ;
unless( defined( $prefix ) ) {
my( $call_pkg, $line_num ) = (caller)[0,2] ;
$prefix = "$call_pkg:$line_num" ;
}
# if only 1 arg, it is text.
# if 2 args, it is level, text
# if 3 args, it is label, level, text
my $text = pop ;
my $log_level = pop || $level ;
my $log_label = pop || $label ;
Stem::Log::entry(
'log' => $log,
'level' => $log_level,
'label' => $log_label,
'text' => "$prefix$text\n"
) ;
}
}
>> you can manage the namespace with the Exporter which is safer than you
>> doing it yourself.
SC> ??? I'm not sure what you mean here. Could you give an
SC> example?
well, put the code to export entries in into a separate module. then you
write a similar import sub to the above that aliases a hash entry to the
var name you want in the caller's package. this is rough and untested
code for it:
sub import {
my( $class, %dbg_args ) = @_ ;
my $use_pkg = caller ;
my $name = $dbg_args{ 'name' } || 'foo' ;
my $opt = $dbg_args{ 'opt' } ;
no strict 'refs';
*{ "${use_pkg}::$name" } = \$options{$opt} ;
}
now, you have isolated the symref to one place and have a simple use
call to make to create a package name for your debug flag globals.
as i said, munging the symbol table is one of the very few legit uses
for symrefs and it should be isolated as much as possible.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
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 987
**************************************