[18405] in Perl-Users-Digest
Perl-Users Digest, Issue: 573 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 27 11:05:41 2001
Date: Tue, 27 Mar 2001 08:05:15 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <985709114-v10-i573@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 27 Mar 2001 Volume: 10 Number: 573
Today's topics:
a funny question <cutmaster@fearlesss.com>
Re: acceccing files on localhost ? <kellyboy@nospanner>
Re: acceccing files on localhost ? <bart.lateur@skynet.be>
Re: beginner's perl question (Abigail)
Email gate on W2K server pgaw@interact.net.au
gd.pm under HP-UX <andreas.zschunke1@infineon.com>
Re: grep(EXPR,LIST) behaves strangely <ronald.fischer.gp@icn.siemens.de>
Re: grep(EXPR,LIST) behaves strangely (Logan Shaw)
Re: grep(EXPR,LIST) behaves strangely (Anno Siegel)
Re: grep(EXPR,LIST) behaves strangely <bart.lateur@skynet.be>
Re: grep(EXPR,LIST) behaves strangely (Tad McClellan)
Help <n-arturs@dsv.su.se>
Re: Help <gus@black.hole-in-the.net>
Re: How to post to a newsgroup from a script? <iltzu@sci.invalid>
Perl::mySQL <Richard_member@newsranger.com>
Re: Q: Active Perl redistributable MINIMUM? <beable@my-deja.com>
Re: require question; data structure for rules (Logan Shaw)
Re: require question; data structure for rules <andrew@erlenstar.demon.co.uk>
Re: sorting a list of files containing a numeric (Tad McClellan)
Re: String::Approx buggy ? <iltzu@sci.invalid>
Re: system() return value (Anno Siegel)
Re: system() return value (Martien Verbruggen)
Re: system() return value (Tad McClellan)
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 27 Mar 2001 11:21:30 +0200
From: "CutMaster" <cutmaster@fearlesss.com>
Subject: a funny question
Message-Id: <99plu2$dmp$1@wanadoo.fr>
Hi ;)
I'm not a good developer in Perl, but I'm working with a guy that says he's
the best ;))
I want you to help me to give him a challenge : decrypt a big line.
So the line would be to add on a var named 'toto' the string : 'perl is
good'...
But in a very obscur way (so not something like toto .= 'perl is good')...
Can you help me ?
Thanks ;))
------------------------------
Date: Tue, 27 Mar 2001 02:28:04 -0600
From: "kellyboy" <kellyboy@nospanner>
Subject: Re: acceccing files on localhost ?
Message-Id: <tc0jje3jl885f0@corp.supernews.com>
ok...Im going to go at it from different approach..
my apache is on /usr/local/apache
cgi-bin is /usr/local/apache/cgi-bin
I have directory /home/kellyboy/image
Using cgi script locating in /usr/local/apache/cgi-bin, how would you write
a script to retreive images from /home/kellyboy/image
my script I got is this:
==================
#!/usr/local/bin/perl -w
use CGI qw(:standard);
print header,start_html("Graphic Explorer");
@files = glob("/home/kellyboy/image/*.jpg");
foreach $pix (@files) {
print img(src=>'$pix');
print p("$pix\n");
print hr();
}
print end_html;
==================
that doesnt work: All I got is this source as a result:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML><HEAD><TITLE>Graphic Explorer</TITLE>
</HEAD><BODY><HR></BODY></HTML>
seems it didnt fill in img tag between the body tag...
I've been at it for couple of hours now..and I need to go to bed...but I
wont be able to sleep with that unsolved problem hanging over my head!!
kellyboy
------------------------------
Date: Tue, 27 Mar 2001 08:37:24 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: acceccing files on localhost ?
Message-Id: <07k0ct05osgq7n3cqpgvjfcc1oh2pgn9n6@4ax.com>
OT, but anyway...
kellyboy wrote:
>my apache is on /usr/local/apache
>cgi-bin is /usr/local/apache/cgi-bin
>
>I have directory /home/kellyboy/image
>
>Using cgi script locating in /usr/local/apache/cgi-bin, how would you write
>a script to retreive images from /home/kellyboy/image
It's not the file location that matters, it's the URL. Note that it's
not the CGI script that sends the image file, but it's the browser that
fetches it. So what's the URL for the /home/kellyboy/image directory?
Could it be "http://www.your.server/image/"? Then all you need is
"/image/filename.jpg".
--
Bart.
------------------------------
Date: Tue, 27 Mar 2001 15:59:55 +0000 (UTC)
From: abigail@foad.org (Abigail)
Subject: Re: beginner's perl question
Message-Id: <slrn9c1e7r.alk.abigail@tsathoggua.rlyeh.net>
David Wang (dwwang@uiuc.edu) wrote on MMDCCLXV September MCMXCIII in
<URL:news:Pine.GSO.4.10.10103262311270.9405-100000@ux13.cso.uiuc.edu>:
|| I have a question about how to remove a block of code from a particular
|| file, the block of code would start with some string x and end with some
|| string y.
||
|| so the pseudocode would be open the file for update, then read each line
|| until x is found, then delete that line and lines that follow until y is
|| read, then delete that last line and then repeat the process. I know this
|| is very simple but I just can't figure it out despite having read the
|| lhama book and the cookbook.
perl -ni -wle 'print unless $_ eq "x" .. $_ eq "y"' file
Abigail
--
@;=split//=>"Joel, Preach sartre knuth\n";$;=chr 65;%;=map{$;++=>$_}
0,22,13,16,5,14,21,1,23,11,2,7,12,6,8,15,3,19,24,14,10,20,18,17,4,25
;print@;[@;{A..Z}];
------------------------------
Date: Tue, 27 Mar 2001 22:47:46 +1000
From: pgaw@interact.net.au
Subject: Email gate on W2K server
Message-Id: <3AC08BF1.326E9EE3@interact.net.au>
Being a self taught newbie I am still struggling my way through. I am
about to use a perl script on a w2000 server and need to generate an
email (no problem on a Unix server). Can anyone help me with the code
required to print to email on W2K server.
------------------------------
Date: Tue, 27 Mar 2001 14:47:18 GMT
From: "Andreas Zschunke" <andreas.zschunke1@infineon.com>
Subject: gd.pm under HP-UX
Message-Id: <WJ1w6.96$jS.1663@dca1-nnrp2.news.digex.net>
Hi guys!
I tried to install gd.pm under HP-UX 11.0 and didn't work because the
original GD library doesn't support dynamically shared libraries, it only
supports static libraries.
Does anyone have an idea how to resolve this problem?
-Andreas
------------------------------
Date: Tue, 27 Mar 2001 11:43:56 +0200
From: Ronald Fischer <ronald.fischer.gp@icn.siemens.de>
Subject: Re: grep(EXPR,LIST) behaves strangely
Message-Id: <3AC060DC.B30D9069@icn.siemens.de>
Tad McClellan wrote:
>
> Ronald Fischer <ronald.fischer.gp@icn.siemens.de> wrote:
>
> >Who can help me with this? I have
> >
> >@ids=(3,5,6);
> >
> >When I write
> >
> >$count = grep { $_ == 5 } @ids;
> >
> >I get, as expected, $count==1. But if I write the statement as
> >
> >$count = grep('$_ == 5',@ids);
> ^ ^
> ^ ^
>
> That is a string, not Perl code. Since it is not one of the two
> "false" strings ( '' or '0') it is always "true", so _all_ list
> elements are selected by grep().
Ooops ... I know it is a string, but I thought grep would eval it for
each element in the list. Thank you for pointing this out.
I reread the description of grep carefully, and indeed, nothing says
about eval'ing the string.
But now I wonder what good is it to pass an arbitrary expression as a
first parameter of grep? OK, a regexp makes sense, and a code block too
... but can someone think about some other type of expression which
would make sense at this point?
Ronald
--
Ronald Otto Valentin Fischer <rovf@earthling.net>
[now at: Siemens ICM N MR UR DE 6, phone: +49(Germany)+89-722-23368]
http://profiles.yahoo.com/ronny_fischer/
http://fusshuhn.ourfamily.com/cppincomp.html
------------------------------
Date: 27 Mar 2001 04:11:49 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: grep(EXPR,LIST) behaves strangely
Message-Id: <99pp15$pqn$1@boomer.cs.utexas.edu>
In article <3AC060DC.B30D9069@icn.siemens.de>,
Ronald Fischer <ronald.fischer.gp@icn.siemens.de> wrote:
>But now I wonder what good is it to pass an arbitrary expression as a
>first parameter of grep? OK, a regexp makes sense, and a code block too
>... but can someone think about some other type of expression which
>would make sense at this point?
Any expression that looks at $_ and returns true or false would make
sense to me. For instance:
sub quicksort
{
if (@_ < 2)
{
return @_;
}
my $pivot = shift;
return
(
quicksort (grep ($_ < $pivot, @_)),
$pivot,
quicksort (grep ($_ >= $pivot, @_))
);
}
Does that help?
- Logan
--
whose? my your his her our their _its_
who's? I'm you're he's she's we're they're _it's_
------------------------------
Date: 27 Mar 2001 10:34:44 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: grep(EXPR,LIST) behaves strangely
Message-Id: <99pqc4$kn1$1@mamenchi.zrz.TU-Berlin.DE>
According to Logan Shaw <logan@cs.utexas.edu>:
[...]
> sub quicksort
> {
> if (@_ < 2)
> {
> return @_;
> }
>
> my $pivot = shift;
>
> return
> (
> quicksort (grep ($_ < $pivot, @_)),
> $pivot,
> quicksort (grep ($_ >= $pivot, @_))
> );
> }
Very pretty, very clear. Just looking at the code, you can watch it
getting slow when the list is already sorted.
Anno
------------------------------
Date: Tue, 27 Mar 2001 15:25:06 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: grep(EXPR,LIST) behaves strangely
Message-Id: <hva1ctsb9sc11102p476avlqm33sod020p@4ax.com>
Ronald Fischer wrote:
>Ooops ... I know it is a string, but I thought grep would eval it for
>each element in the list. Thank you for pointing this out.
This might seem confusing at first... but the expression is not
calculated immediately. Instead, a reference to the code consisting of
this expression is passed to grep. This code is in turn executed once
for every item in the LIST.
I think the official term is "higher order function".
--
Bart.
------------------------------
Date: Tue, 27 Mar 2001 09:10:45 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: grep(EXPR,LIST) behaves strangely
Message-Id: <slrn9c17r5.cqp.tadmc@tadmc26.august.net>
Ronald Fischer <ronald.fischer.gp@icn.siemens.de> wrote:
>Tad McClellan wrote:
>> Ronald Fischer <ronald.fischer.gp@icn.siemens.de> wrote:
>> >$count = grep('$_ == 5',@ids);
>> ^ ^
>> ^ ^
>>
>> That is a string, not Perl code. Since it is not one of the two
>> "false" strings ( '' or '0') it is always "true", so _all_ list
>> elements are selected by grep().
>
>Ooops ... I know it is a string, but I thought grep would eval it for
>each element in the list.
Gak! That would be a "string eval", and using string eval should
always cause you to pause for a moment and see if you really really
need to take on the security concerns that go with string eval.
It would be annoying to have to pause before using grep(), I'm
glad we don't have to :-)
If you leave the quotes off, then perl _does_ eventually "eval" the EXPR.
But it parses the code at compile time instead of at run time, that's
good too.
>Thank you for pointing this out.
Thank Larry for not making it a string eval :-)
>I reread the description of grep carefully, and indeed, nothing says
>about eval'ing the string.
A case of the programmer's "What I Mean" not lining up with perl's DWIM.
Happens occasionally.
>But now I wonder what good is it to pass an arbitrary expression as a
>first parameter of grep?
See your code above, heh.
>OK, a regexp makes sense,
That is another case of using the EXPR form.
>and a code block too
>... but can someone think about some other type of expression which
>would make sense at this point?
A bunch. Here's a few:
my @not_empty = grep -s, @filenames;
my @oddnums = grep $_ % 2, @numbers;
my @reachable = grep follow_URL($_), @urls;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 27 Mar 2001 15:27:35 +0200
From: Niklas Herder <n-arturs@dsv.su.se>
Subject: Help
Message-Id: <Pine.LNX.4.21.0103271520050.957-100000@l454.dsv.su.se>
I'm having problem with a small cgi script I've done. It's supposed to
generate a gif image with text, but all I get is an empty square.
What am I doing wrong? I'm getting pretty frustrated here...
Here's the code:
#!/usr/local/bin/perl -wT
use CGI::Carp qw{fatalsToBrowser};
alarm (30);
use CGI ':standard';
use GD;
use strict;
print header("image/gif");
my $time = localtime;
my $img = new GD::Image;
my $textcol;
$textcol = $img->colorAllocate(150,150,0);
my $bg;
$bg = $img->colorAllocate(0,0,0);
$img->string(gdLargeFont, 1,1, $time, $textcol);
my $gif;
$gif = $img->gif;
$img->transparent($bg);
binmode STDOUT;
print "$gif\n";
------------------------------
Date: Tue, 27 Mar 2001 15:35:48 GMT
From: Gus <gus@black.hole-in-the.net>
Subject: Re: Help
Message-Id: <985707348.25615.2.nnrp-07.c29f015a@news.demon.co.uk>
Niklas Herder <n-arturs@dsv.su.se> wrote:
> I'm having problem with a small cgi script I've done. It's supposed to
> generate a gif image with text, but all I get is an empty square.
> What am I doing wrong?
You're allocating the text colour first, which means it becomes the background
colour of the image. Your text is there, but it's the same colour as the
background.
Try this.
use GD;
use strict;
my ($img,$time,$bg,$textcol);
$img = new GD::Image;
$time = scalar localtime;
$bg = $img->colorAllocate(0,0,0);
$textcol = $img->colorAllocate(150,150,0);
$img->string(gdLargeFont, 1,1, $time, $textcol);
print
"Content-type: image/gif\n\n",
$img->gif;
--
gus@black.hole-in-the.net
0x58E18C6D
82 AA 4D 7F D8 45 58 05 6D 1B 1A 72 1E DB 31 B5
http://black.hole-in-the.net/gus/
------------------------------
Date: 27 Mar 2001 15:08:30 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: How to post to a newsgroup from a script?
Message-Id: <985705108.4417@itz.pp.sci.fi>
In article <985627951.7553.0.nnrp-08.c1ed3545@news.demon.co.uk>, Chris Thompson wrote:
>foreach $newsgroup_to_send_to (@newsgroups_to_send_to) {
>
> # select newsgroup
> $nntp->group($newsgroup_to_send_to);
You don't need to do that. The group your post goes is determined by
the "Newsgroups" header in the post itself, not by the group() method.
And, as the name says, you can put multiple group names on that header
line, separated by commas. Doing it that way saves Usenet resources,
and incidentally makes it less likely for your account to be closed by
your ISP for excessive multiposting, something very much frowned upon.
Still, the people who said you shouldn't be doing this at all are most
likely right. Not just because it isn't acceptable in most newsgroups,
but also because the groups where it *is* allowed are often wastelands
full of job ads and with few if any actual readers.
--
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla / Kira -- do not feed the troll.
------------------------------
Date: Tue, 27 Mar 2001 12:12:15 GMT
From: Richard Dobson <Richard_member@newsranger.com>
Subject: Perl::mySQL
Message-Id: <zs%v6.3398$fy.5442@www.newsranger.com>
Hi,
I have been set a task that I would like a bit of guidance with if possible.
The idea is that someone wants to track files i.e when they were created,
editted, proofed etc. I was thinking of having a form on their intranet that
they filled in whenever they made alterations to the file. They could then
search for files that fitted within certain criteria. This would rely on them
going and filling in the form.
Would it be possible to make this nicer by having the form automatically open
whenever a file was closed?
I am planning it with Perl DBI and mySQL. I would like the files to be within
the database and to open them from a browser. Is there a function within perl
for storing documents as opposed to text records within mySQL.
Any guidance would be a great help. Is perl the best tool for this?
thanks in advance
richard
r.dobson@microscience.com
------------------------------
Date: Tue, 27 Mar 2001 10:52:52 GMT
From: Beable van Polasm <beable@my-deja.com>
Subject: Re: Q: Active Perl redistributable MINIMUM?
Message-Id: <lv66gvscso.fsf@beable.van.polasm.bigpond.net.au>
Jeremy <jeremyk.remove-this@privacy.nu> wrote:
> Does anyone know what the absolutely minimal Active Perl installation
> consists of? Assume I use no packages, only the very core perl
> functionality, what do I need to install on a machine in order to be
> able to run it? (The old perl was easy in that respect, while the
> "Active" perl is too Window'ish, takes huge amount of space on disk,
> installs fifty bazillion files, and probably puts something in the
> Registry.) I need to move it quickly though, and nonintrusively in terms
> of machine configuration.
I don't run ActiveState Perl, but wouldn't the smallest possible
installation be just perl.exe? Why don't you try using just the
exe and see if that works?
cheers
Beable van Polasm
--
This isn't right!! -- WCW Nitro Commentator #1
This is a travesty! -- WCW Nitro Commentator #2
IQC 78189333
http://members.nbci.com/_______/index.html
------------------------------
Date: 27 Mar 2001 02:47:14 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: require question; data structure for rules
Message-Id: <99pk2i$phi$1@boomer.cs.utexas.edu>
In article <3AC044EC.8547C84F@leland.stanford.edu>,
Jonathan Dugan <dugan@leland.stanford.edu> wrote:
>I'd like the rule set to be alterable at run time without stopping the
>script, so I've implemented the rule set as a module. In the setup()
>subroutine, I use require to load the modules and build the hashes
>containing the rules. I've put signal handlers in the main program to
>catch HUP.
>
>So the problem: how do I keep reloading a module? (or for that
>matter, any code in an external file) I want the function called on
>HUP to simply run the setup() sub again (which it does) but the
>'require' function only will load a module once.
You want "do". It's like require, but it doesn't remember that
it's already loaded something. "perldoc -f do" for more info.
Personally, I would not make the code a module. If I recall correctly,
loading it again would *add* to the definition of the package, not
replace it. This could lead to memory being wasted, or (worse) it
could lead to incorrect execution.
Instead, I would make your included file an expression that returns a
scalar value. That scalar value would be a reference to a data
structure containing your rules. So, the rules file might look
like this:
{
a => sub { 42; },
b => sub { $ARGV[0] * $ARGV[1]; }
}
Then, in the program you can do this:
$rules = do '/path/to/rules.pl';
which would be very much like doing this:
$rules =
{
"adams" => sub { 42; },
"multiply" => sub { $ARGV[0] * $ARGV[1]; }
};
One other advantage: by avoiding having a package in the file, you
don't have to worry about the package name. This could become a real
problem if you want to have more than one set of rules. In the real
world, once you started using a script that does require a package
name, I bet you'd find that all your rules files always have the same
package name in them.
Actually, I would so some error handling when it's time to re-read the
rules file. Maybe something like this:
$loaded_rules = do '/path/to/rules.pl';
if ($loaded_rules)
{
$new_rules = $loaded_rules;
}
else
{
warn "Error opening rules file ($!)\n" if defined $!;
warn "Error compiling rules file:\n$@\n" if defined $@;
}
Of course, if you're using a signal handler like you say, then you want
to make sure you only modify the list of rules when some part of the
program isn't in the middle of using them. You could probably do it
with something like this:
while (some_condition())
{
if ($new_rules)
{
print "Using new rules now!\n";
$rules = $new_rules;
$new_rules = undef;
}
use_rules();
}
>I'm envisioning each rule set as a hash, keyed on a serial number:
>
>###
>package RULES;
>
>my %rules1;
>my %rules2;
>
>%rules1 = (
>
> 1 => (blah,
> blah,
> blah),
>
>
> 2 => ( blah, etc. )
>
>);
Well, of course, that syntax isn't quite correct since you'd have to
have anonymous lists as hash values, but I think you intended that just
as an example.
Note, however, that if you do it that way, you probably don't want to
use "my". The variables are already in their own package, so using
"my" in this case just makes things harder. Actually, I believe that
using any (non-temporary) variable names in the rules file just makes
things harder, which is why I suggested that the rules file just be a
big expression.
>I'd like both conditions and effects to be BLOCKS {} with perl code in
>them.
>
>So... can you put a BLOCK of perl code as an element of an list? How
>would you run it?
You can't, but you can put a subroutine in a list or a hash. And the
good thing about that is that the subroutine can take arguments and
return a value. In Perl, this is called an anonymous subroutine. (In
other languages, it's called a lambda function. Personally, I think
Perl's name for it is MUCH better!) To get an anonymous subroutine,
you just leave off the name. The subroutine then becomes, rather than
a stand-alone definition, an expression whose value is a reference that
happens to refer to the subroutine.
For instance, you can do this:
$x = sub { print "hello, world.\n"; };
&$x;
Note the semicolon after the closing bracket for the subroutine.
That's because this anonymous subroutine is an expression just like "2
+ 2" or similar. Also note the ampersand in the second line, which
calls the routine contained in $x. (In the old days, all Perl
functions had to be called with an ampersand.)
>This way you could have a list of code segments that are the
>conditions if they all evalualte to true, then the effects are
>processed the return value is set appropriately. Finally, the return
>value is returned.
I'm not really all that sure what kind of conditions you have in mind.
You may be tempted to store your condition/action pairs in a hash,
because the hash naturally has key/value pairs in it. This is great,
except that while hashs' values can be anything, their keys must be
strings. Not to worry, though: you can store pairs in other ways.
The easiest way is to store them as an anonymous list. For example,
state capitals can be stored this way:
$capitals =
{
TX => "Austin",
CA => "Sacramento",
NY => "PLEASE don't say New York City"
};
Or, they can be stored like this:
$capitals =
[
[ "TX", "Austin" ],
[ "CA", "Sacramento" ],
[ "NY", "PLEASE don't say New York City" ]
];
The former is an anonymous hash. The latter is an anonymous list of
lists, and those lists are all two elements long. The list of lists
method is slower to search, but all its elements are in a well-defined
order, and there is no limitation to just strings. If you want,
you can make this prettier by using the fact that "=>" acts just like a
comma (at least when it's syntactically valid, it does):
$capitals =
[
[ "TX" => "Austin" ],
[ "CA" => "Sacramento" ],
[ "NY" => "PLEASE don't say New York City" ]
];
Aaaaanyway, a decent data structure to do what you want might look
like this:
$rulesets =
{
# set #0
0 =>
[
# first rule
[
# test
sub { return some_test(); },
# action
sub { whatever(); }
],
# second rule
[
# test
sub { return other_test(); },
# action
sub { whatever_else(); }
],
],
# set #1
1 =>
[
# similar to first rule set
]
};
So basically, it's an anonymous hash whose keys are ruleset numbers and
whose values are lists of pairs of anonymous subroutines. The syntax
for this can get unwieldy, so you can compose the elements and then
store them into the data structure one at a time if you want:
do
{
my $ruleset;
my ($sub_0_0_test, $sub_0_0_action,
$sub_0_1_test, $sub_0_1_action);
$sub_0_0_test = sub
{
return some_test();
};
$sub_0_0_action = sub
{
return whatever();
};
$sub_0_1_test = sub
{
return other_test();
};
$sub_0_1_action = sub
{
return whatever_else();
};
# Now, define the ruleset. Since this is the last
# part of the "do" block, it will be the value of the
# "do" expression, which will be the value of the
# file when it is run by the script's "do".
{
0 =>
[
[ $sub_0_0_test, $sub_0_0_action ],
[ $sub_0_1_test, $sub_0_1_action ]
],
1 =>
[
]
};
}
Sorry, that was a little complicated. But you've asked a complicated
question...
Hope that helps.
- Logan
--
whose? my your his her our their _its_
who's? I'm you're he's she's we're they're _it's_
------------------------------
Date: 27 Mar 2001 10:10:19 +0100
From: Andrew Gierth <andrew@erlenstar.demon.co.uk>
Subject: Re: require question; data structure for rules
Message-Id: <87snjz8tms.fsf@erlenstar.demon.co.uk>
>>>>> "Jonathan" == Jonathan Dugan <dugan@leland.stanford.edu> writes:
Jonathan> So the problem: how do I keep reloading a module? (or for
Jonathan> that matter, any code in an external file)
use 'do' rather than 'require'. i.e. do $file;
('do' can do other things too, it's a bit overloaded.)
This is reasonably straightforward as long as you just want to load
things; unloading turns out to be harder, and there are some
interesting issues with lifetimes of data values.
Jonathan> next question:
[...]
Jonathan> rules have conditions, effects, and a return value to fill
Jonathan> in the blah blah blah above (somehow).
Jonathan> I'd like both conditions and effects to be BLOCKS {} with
Jonathan> perl code in them.
Jonathan> So... can you put a BLOCK of perl code as an element of an
Jonathan> list? How would you run it?
The construct "sub { ... }" creates a code reference to an anonymous
subroutine; this is a highly convenient way of storing small blocks
of code in datastructures.
So if you have, say, a hash of this form:
my %rules = (
"rule1" => {
predicate => sub { local($_) = shift; tr/a// > tr/o// },
action => sub { process_value(@_) },
result => 1
},
"rule2" => ...and so on...
);
thus each value in %rules is a reference to an array of code
references, (the code fragments are examples only of course), then a
function of this form could be used to apply a rule to a given string:
sub apply_rule
{
my ($rulename,$value) = @_;
return undef unless $rules{$rulename}{predicate}->($value);
$rules{$rulename}{action}->($value);
return $rules{$rulename}{result};
}
note that $ref->(...) is one way to call a function by means of a
reference.
Jonathan> This way you could have a list of code segments that are
Jonathan> the conditions if they all evalualte to true, then the
Jonathan> effects are processed the return value is set
Jonathan> appropriately. Finally, the return value is returned.
in the above example you could use arrays of code references instead
of the single code references I used for clarity.
e.g.
my %rules = (
"rule1" => {
predicates => [ sub { local($_) = shift; tr/a// > tr/o// },
sub { length($_[0]) > 20 }
],
actions => [ sub { process_value(@_) },
sub { other_process(@_) },
],
result => 1
},
"rule2" => ...and so on...
);
sub apply_rule
{
my ($rulename,$value) = @_;
for my $guard (@{$rules{$rulename}{predicates}})
{
return undef unless $guard->($value);
}
for my $action (@{$rules{$rulename}{actions}})
{
$action->($value);
}
return $rules{$rulename}{result};
}
Jonathan> It doesn't appear that the eval function does what I want.
you could use eval, but you'd probably end up recompiling the code
every time you called it, which the sub {...} method avoids.
--
Andrew.
------------------------------
Date: Tue, 27 Mar 2001 09:33:03 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: sorting a list of files containing a numeric
Message-Id: <slrn9c194v.cqp.tadmc@tadmc26.august.net>
Jay Tilton <tiltonj@erols.com> wrote:
>Sounds like a good candidate for Schwartzian Transform.
>
>my @sorted = map {$_->[0]}
> sort {$a->[1] <=> $b->[1]}
> map {/^constant_string_(\d+?)\.log$/; [$_, $1]}
^ ^ ^
^ ^ ^
> @array;
Note that, since both start and end are anchored, "greedy" and
"non-greedy" must both match exactly the same strings, so the
question mark is added fluff that does not affect anything.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 27 Mar 2001 14:55:46 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: String::Approx buggy ?
Message-Id: <985704707.2821@itz.pp.sci.fi>
In article <985610051.29372.0.nnrp-08.9e98901a@news.demon.co.uk>, Stephen Collyer wrote:
>
>Ilmari Karonen <iltzu@sci.invalid> wrote in message
>news:985607334.18184@itz.pp.sci.fi...
>> In article <985603697.11132.0.nnrp-12.9e98901a@news.demon.co.uk>, Stephen
>Collyer wrote:
>> >
>> >print "match\n" if amatch('fred', [ 2, 'D2', 'I0', 'S0'], 'frooooooood');
>
>> I *think* that's not a bug, but simply counterintuitive behavior and
>> rather obscure documentation. What happens is that amatch()
>> tries to find a *substring* of 'frooooooood' that is no more than
>> two deletions away from 'fred'. One such such substring is 'fr'.
>
>I think you're almost right - only almost because it seems that
>amatch has to match multiple substrings to get a match here:
>
>frooooooood
>fr________d
Hmm.. am I misunderstanding your point, or did you miss the part in the
documentation that says the match isn't anchored. So presumably (as I
don't actually have the module installed, and have never used it)
amatch('foo', [ 0 ], 'foobar')
should be true, since 'foo' is a substring of 'foobar'. I've no idea
why it should be so, but that docs quite clearly (well, compared to the
general clarity of the String::Approx docs) indicate it is.
--
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla / Kira -- do not feed the troll.
------------------------------
Date: 27 Mar 2001 10:09:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: system() return value
Message-Id: <99pos7$eo8$2@mamenchi.zrz.TU-Berlin.DE>
According to Martien Verbruggen <mgjv@tradingpost.com.au>:
> In Perl, false is normally represented by 0, "0", "" or undef. Oh, and
> in a list context, the empty list is false.
Surely you mean "scalar (or boolean) context".
Anno
------------------------------
Date: Tue, 27 Mar 2001 20:53:37 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: system() return value
Message-Id: <slrn9c0s9h.ugg.mgjv@martien.heliotrope.home>
On 27 Mar 2001 10:09:11 GMT,
Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> According to Martien Verbruggen <mgjv@tradingpost.com.au>:
>
>> In Perl, false is normally represented by 0, "0", "" or undef. Oh, and
>> in a list context, the empty list is false.
>
> Surely you mean "scalar (or boolean) context".
Lemme think about that...
[time passes]
*nod* It would be a boolean context, otherwise there is nothing to test
the falseness against.
I got a bit confused because of what a bare return does in a list or
scalar context. This, of course, has nothing to do with something being
false or true.
My statement should simply have read something like:
In Perl, false is represented by 0, "0", "", undef or an empty list.
Martien
--
Martien Verbruggen |
Interactive Media Division | Make it idiot proof and someone will
Commercial Dynamics Pty. Ltd. | make a better idiot.
NSW, Australia |
------------------------------
Date: Tue, 27 Mar 2001 09:20:47 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: system() return value
Message-Id: <slrn9c18dv.cqp.tadmc@tadmc26.august.net>
Martien Verbruggen <mgjv@tradingpost.com.au> wrote:
>On 27 Mar 2001 10:09:11 GMT,
> Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
>> According to Martien Verbruggen <mgjv@tradingpost.com.au>:
>>
>>> In Perl, false is normally represented by 0, "0", "" or undef. Oh, and
>>> in a list context, the empty list is false.
>>
>> Surely you mean "scalar (or boolean) context".
>
>Lemme think about that...
>
>[time passes]
>
>*nod* It would be a boolean context, otherwise there is nothing to test
>the falseness against.
>
>I got a bit confused because of what a bare return does in a list or
>scalar context. This, of course, has nothing to do with something being
>false or true.
>
>My statement should simply have read something like:
>
>In Perl, false is represented by 0, "0", "", undef or an empty list.
If you preface it with "booleans are *always* in scalar context",
as they are, then it becomes the conventional:
In Perl, false is represented by 0, "0", "" or undef.
Because there is no such thing as a list in scalar context
(so "bare return" would see "scalar context" when called in
a boolean (scalar) context).
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 573
**************************************