[25031] in Perl-Users-Digest
Perl-Users Digest, Issue: 7281 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 21 11:13:04 2004
Date: Thu, 21 Oct 2004 08:10:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 21 Oct 2004 Volume: 10 Number: 7281
Today's topics:
Re: perl to english <ioneabu@yahoo.com>
Re: perl to english (Peter Scott)
Re: perl to english <uri@stemsystems.com>
Re: Perl Whirl IV from Dubrovnik <dwall@fastmail.fm>
Regex, how do I replace quotation pairs into <LI> & </L <thefatcat28@hotmail.com>
Re: Regex, how do I replace quotation pairs into <LI> & <noreply@gunnar.cc>
Re: Regex, how do I replace quotation pairs into <LI> & <toreau@gmail.com>
Re: Regex, how do I replace quotation pairs into <LI> & <tadmc@augustmail.com>
Re: Regular Expression for HTML Tags and Special Charac <tadmc@augustmail.com>
Re: Regular Expression for HTML Tags and Special Charac <tadmc@augustmail.com>
Re: Regular Expression for HTML Tags and Special Charac <bart.lateur@pandora.be>
Re: source a config file carloschoenberg@yahoo.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 21 Oct 2004 10:20:23 -0400
From: wana <ioneabu@yahoo.com>
Subject: Re: perl to english
Message-Id: <10nfhjdrg3r5342@news.supernews.com>
Tintin wrote:
>
> "wana" <ioneabu@yahoo.com> wrote in message
> news:10ndlt9dh9og462@news.supernews.com...
>> buildmorelines wrote:
>>
>>> Is there any script or pragma or something that will translate perl
>>> code to pure english, like that perl latin module, just in english. I
>>> want to show a person who cant read perl code or any computer
>>> language, some perl code, so they have remotly a clue what the code
>>> does or how it flows. It doesnt need to perfectly make sense or be
>>> proper english sentences. Just soemthing that will translate perl code
>>> and/or syntax to english.
>>
>> It is possible! I have written and tested a preliminary version of the
>> program that performs the task you are requesting. Here it is:
>>
>> #! /usr/bin/perl
>>
>> use STD; #my home made module
>
> Oooohhh, please share this module!!
>
> I'm surprised it's not on CPAN yet.
>
>> use strict;
>> use warnings;
>>
>> my $file = $ARGV[0];
>> ReplaceInFile('=', 'is equal to', $file);
>>
>> I tested it on several perl scripts and it worked beautifully.
>
> I bet I could give you some scripts that it wouldn't work beautifully on.
I was only partly joking. Of course you would want to replace things like
==, >=, =>, <=> first with their appropriate meanings and then =. The
ReplaceInFile sub is the only thing in the above code that comes from my
module STD.pm which is just a set of commonly used functions for me and a
place to practice and improve my Perl as a beginner and amateur. It turns
out that much of what I put in STD duplicates what is already available in
File::Slurp which is available on cpan and does a much better job. I would
have used a standard function for ReplaceInFile, but, as simple as it is, I
was not aware of a version of it on cpan, although I'm sure that one must
exist. Since you asked, here is my module so far (mostly but not
thoroughly tested and probably far from adequate in some spots):
package STD;
use strict;
use Exporter;
use vars qw /$VERSION @ISA @EXPORT @EXPORT_OK/;
@ISA = qw /Exporter/;
@EXPORT = qw /LoadFromFile SaveToFile
Trim ReplaceInFile read_dir/;
@EXPORT_OK = qw //;
($VERSION) = '$first attempt: 1.0 $' =~ /(\d+\.\d+)/;
#subs start here
sub LoadFromFile
#takes array reference and file name as argument
#and clears array and fills array with file contents
{
my ($array, $filename) = @_;
open my $file, '<', $filename or die "Couldn't open $filename: $!";
@$array = <$file>;
close $file or die "Error closing $filename: $!";
}
sub SaveToFile
#takes scalar, scalar ref or array ref and a file name as arguments
#and saves contents to file. Third, optional, arg: the word
#'append' will put in append mode.
{
my ($data, $filename, $append) = @_;
$append = $append ? '>>':'>';
open my $file, $append, $filename or die "Couldn't open $filename: $!";
print $file @$data if ref($data) eq "ARRAY";
print $file $$data if ref($data) eq "SCALAR";
print $file $data if not ref $data;
close $file or die "Error closing $filename: $!";
}
sub Trim
#takes reference to scalar and removes leading
#and trailing white space.
{
my $string = shift;
${$string} =~ s/^\s+//;
${$string} =~ s/\s+$//;
}
sub ReplaceInFile
#takes 4 strings as arguments. the first is the string to replace.
#the second is the string to replace with. the third is the file
#to search through. the fourth is the file to write to (can be the
#same file of course. Leave out 4th arg to use same file.
{
my ($target, $source, $infile, $outfile) = @_;
$outfile = $infile if not defined $outfile;
my @workspace;
LoadFromFile(\@workspace,$infile);
s/$target/$source/g for(@workspace);
SaveToFile(\@workspace,$outfile);
}
sub read_dir
#arguments: directory path and array reference
#action: fill array with file names of directory
{
my ($dir, $return) = @_;
@{$return} = ();
print "dir = $dir\n";
opendir DIR, "$dir" or die "Couldn't open $dir: $!";
my @contents = readdir(DIR);
closedir DIR or die "Error closing $dir: $!";
foreach(@contents)
{
if (/^\.+/) {next;}
else
{
push @{$return}, $_;
}
}
}
1;
------------------------------
Date: Thu, 21 Oct 2004 14:25:35 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: perl to english
Message-Id: <zFPdd.173388$a41.120743@pd7tw2no>
In article <cl6pgu$poo$2@mamenchi.zrz.TU-Berlin.DE>,
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>Uri Guttman <uguttman@athenahealth.com> wrote in comp.lang.perl.misc:
>> logical flow descriptions and only perl is good for program poetry!
>
>Oh, come on. There must be some Cobol poetry. Something long
>and impenetrable, in hexameter...
There is the classic COBOL version of Goldilocks and the
Three Bears, which I was introduced to over 20 years ago...
--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
------------------------------
Date: Thu, 21 Oct 2004 14:57:19 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: perl to english
Message-Id: <x7mzyg15oq.fsf@mail.sysarch.com>
>>>>> "w" == wana <ioneabu@yahoo.com> writes:
w> use strict;
w> use Exporter;
use base 'Exporter' ;
w> use vars qw /$VERSION @ISA @EXPORT @EXPORT_OK/;
no need for @ISA with use base.
no need for @EXPORT_OK as you don't use it.
w> @ISA = qw /Exporter/;
handled by use base
w> @EXPORT = qw /LoadFromFile SaveToFile
w> Trim ReplaceInFile read_dir/;
our @EXPORT
w> @EXPORT_OK = qw //;
why have that if it is empty?
w> ($VERSION) = '$first attempt: 1.0 $' =~ /(\d+\.\d+)/;
w> #subs start here
really? i couldn't have figured that out without that comment.
w> sub LoadFromFile
don't use studly caps for subnames. in perl that is only used for
package names.
w> #takes array reference and file name as argument
w> #and clears array and fills array with file contents
w> {
w> my ($array, $filename) = @_;
the filename is usually the more important arg and so it should come
first. that way you can also take an optional hash of args
afterwards. see File::Slurp for that style of API.
w> sub Trim
w> #takes reference to scalar and removes leading
w> #and trailing white space.
w> {
w> my $string = shift;
w> ${$string} =~ s/^\s+//;
w> ${$string} =~ s/\s+$//;
w> }
do you realize that @_ is really aliases to the original arguments? you
could simplify that by passing in the real scalars and directly
operating on them.
sub trim {
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
}
w> sub ReplaceInFile
w> #takes 4 strings as arguments. the first is the string to replace.
w> #the second is the string to replace with. the third is the file
w> #to search through. the fourth is the file to write to (can be the
w> #same file of course. Leave out 4th arg to use same file.
w> {
w> my ($target, $source, $infile, $outfile) = @_;
w> $outfile = $infile if not defined $outfile;
w> my @workspace;
w> LoadFromFile(\@workspace,$infile);
w> s/$target/$source/g for(@workspace);
w> SaveToFile(\@workspace,$outfile);
w> }
if you slurp, why not slurp in as a single string and then do the
s///. it is much faster than looping over the lines (also scalar
slurping is much faster then line slurping). here is what i would do:
(this is a very simple sub and it could use work)
use File::Slurp ;
sub replace_in_file {
my( $file, $from, $to ) = @_ ;
my $text = read_file( $file ) ;
$text =~ s/$from/$to/g ;
write_file( $file, $text ) ;
}
w> sub read_dir
w> #arguments: directory path and array reference
w> #action: fill array with file names of directory
w> {
w> my ($dir, $return) = @_;
w> @{$return} = ();
why that? just build up an array and return its reference.
w> print "dir = $dir\n";
w> opendir DIR, "$dir" or die "Couldn't open $dir: $!";
w> my @contents = readdir(DIR);
w> closedir DIR or die "Error closing $dir: $!";
w> foreach(@contents)
w> {
w> if (/^\.+/) {next;}
w> else
w> {
w> push @{$return}, $_;
w> }
w> }
blech. perldoc -f grep.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 21 Oct 2004 13:55:56 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Perl Whirl IV from Dubrovnik
Message-Id: <Xns9589650993601dkwwashere@216.168.3.30>
Randal L. Schwartz <merlyn@stonehenge.com> wrote:
> >>>>> "David" == David K Wall <dwall@fastmail.fm> writes:
>
> David> Matija Papec <perl@my-header.org> wrote:
> >> http://misc.linux.hr/geekcruises2004/?lang=en
> >>
> >> it's Larry on most of the pictures for everybody wanted to take
> >> picture with him.
>
> David> No names with the pictures? I recognize Larry and Randal,
> David> but if there are other names I might know, it would be
> David> pleasant (although by no means necessary) to be able to
> David> associate a face with them.
>
[snip names]
>
> That's all I spotted for now. I'm horrible with names, sorry.
Thanks! I was actually hoping to see some c.l.p.m faces, too, but I
suppose the travel expenses are prohibitive for many people. (They
would be for me.)
------------------------------
Date: Thu, 21 Oct 2004 18:27:05 +0800
From: "Kelvin" <thefatcat28@hotmail.com>
Subject: Regex, how do I replace quotation pairs into <LI> & </LI>?
Message-Id: <41778d70$1@news.starhub.net.sg>
Basically, my texts consists of normal text stream and some quotations.
This is my text stream, and inside "this streams" there are some "quotation
pairs"
which are to be replaced like this: <LI>this streams</LI> for formatting in
HTML.
Tried ___s/\".*?\"/<li>.*?<\/li>/g;___ but not working.
Thanks.
Kelvin
------------------------------
Date: Thu, 21 Oct 2004 13:13:19 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Regex, how do I replace quotation pairs into <LI> & </LI>?
Message-Id: <2tpkikF226fmaU1@uni-berlin.de>
Kelvin wrote:
> Basically, my texts consists of normal text stream and some
> quotations.
>
> This is my text stream, and inside "this streams" there are some
> "quotation pairs" which are to be replaced like this:
> <LI>this streams</LI> for formatting in HTML.
>
> Tried ___s/\".*?\"/<li>.*?<\/li>/g;___ but not working.
-------------------------^^^
Please study the description of the s/// operator in "perldoc perlop" -
Don't just guess!
Maybe this is what you want:
s/\"(.*?)\"/<li>$1<\/li>/gs;
If not, please post a short, self-contained but *complete* program
including sample data that illustrates what it is you are trying to do.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Thu, 21 Oct 2004 14:45:05 +0200
From: Tore Aursand <toreau@gmail.com>
Subject: Re: Regex, how do I replace quotation pairs into <LI> & </LI>?
Message-Id: <pan.2004.10.21.12.44.55.342677@gmail.com>
On Thu, 21 Oct 2004 18:27:05 +0800, Kelvin wrote:
> s/\".*?\"/<li>.*?<\/li>/g;
No need to escape those "-characters, AFAIK. And you don't want to
replace .*? above with - uhm - the regular expression .*?, do you?
Untested, but I think something like this should do it;
s,"(.*?)",<li>$1</li>,g;
Please read these:
perldoc perlretut
perldoc perlre
--
Tore Aursand <toreau@gmail.com>
"Time only seems to matter when it's running out." (Peter Strup)
------------------------------
Date: Thu, 21 Oct 2004 07:54:01 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Regex, how do I replace quotation pairs into <LI> & </LI>?
Message-Id: <slrncnfcb8.76v.tadmc@magna.augustmail.com>
[ There IS NO comp.lang.perl newsgroup, it was removed MANY YEARS ago.
If your news server carries it, then your server is poorly maintained.
There is nothing about modules in your post, please do not make
off-topic posts!
Newsgroups trimmed, Followups set.
]
Kelvin <thefatcat28@hotmail.com> wrote:
> This is my text stream, and inside "this streams" there are some "quotation
> pairs"
> which are to be replaced like this: <LI>this streams</LI> for formatting in
> HTML.
>
> Tried ___s/\".*?\"/<li>.*?<\/li>/g;___
^^ ^^
^^ ^^
I thought you said <LI></LI>? That isn't what you have there...
> but not working.
You need to capture the element's content so that you can put
it back in. You use parenthesis to capture in regexes.
Double quotes are not meta in regexes, so you don't need to backslash them.
If you choose an alternate delimiter, then you won't need to
backslash the / either.
s#"(.*?)"#<LI>$1</LI>#g;
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Oct 2004 07:22:46 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Regular Expression for HTML Tags and Special Characters
Message-Id: <slrncnfagm.76v.tadmc@magna.augustmail.com>
Vijayaraghavan Kalyanapasupathy <vijai.lists@gmail.com> wrote:
> In article <slrncndl9u.5fu.tadmc@magna.augustmail.com>,
> tadmc@augustmail.com says...
>> > m/<\s*(\w+)\s+.*?>.*?(\/\1)/
>> ^^^
>>
>> It is invalid HTML if it has whitespace there.
>
> I didn't know that.
Yet another reason to use an HTML module.
The module authors know these things. :-)
> In this case we would modify it to
>
> m/<(\w+)\s+.*?>.*?(\/\1)/
Let's put the endtag angle brackets in there too:
m/<(\w+)\s+.*?>.*?<(\/\1)>/
>> Will that work on the below (after taking out the \s*)?
>>
>> <foo>bar</foo>
>
> (I am trying to answer without running the code.
That's what I was hoping you would do...
> So if there's a
> mistake, you know whom you have to blame me.)
>
> You are right it wouldn't. I would have to do this instead:
>
> m/<(\w+)*?\s+.*?>.*?<\/\1>/
>
> This will catch the "foo" minimally.
... but then you should check yourself by trying it in actual code. :-)
It will fail to match at all.
Your pattern requires at least one whitespace and <foo> does
not contain a whitespace.
>> here are some more complications to try and match correctly:
>>
>> <!-- there are no <tags></tags> on this line at all! -->
>
> But if we had a regular expression that matched a <!-- . --> wouldn't
> that gobble up the <tags></tags> inbetween?
Yes, but matching "comment declarations" is not that easy, yet another
reason to use an HTML module. Here is a valid comment declaration:
<!-- comment -- -- some more comment -- >
> Of course, I am thinking more along the lines of a Lex input
> specification where you would typically do a:
[snip lex patterns]
The grammar for SGML comment declarations is a good bit more complex
than that.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Oct 2004 07:26:47 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Regular Expression for HTML Tags and Special Characters
Message-Id: <slrncnfao7.76v.tadmc@magna.augustmail.com>
Vijayaraghavan Kalyanapasupathy <vijai.lists@gmail.com> wrote:
> It does get exceedingly complicated. But, then I am not sure if a
> regular expression can really match all types of input. Isn't that what
> the Chomsky hierarchy is about?
>
> Correct me if I am wrong.
No corrections required.
Regular Expressions do not have the power required to parse a
Context Free grammar, such as HTML.
Note:
Perl's regular expressions are no longer Regular, they are called
that for historical reasons rather than for mathematically-correct
reasons.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 21 Oct 2004 15:02:00 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: Regular Expression for HTML Tags and Special Characters
Message-Id: <upjfn01f6mjc4fnomt7bfg1a59vspcgd2m@4ax.com>
Tad McClellan wrote:
>Regular Expressions do not have the power required to parse a
>Context Free grammar, such as HTML.
I doubt if HTML actually is a context free grammar. There are no
recursive rules, AFAIK.
--
Bart.
------------------------------
Date: 21 Oct 2004 07:01:45 -0700
From: carloschoenberg@yahoo.com
Subject: Re: source a config file
Message-Id: <8c526b62.0410210601.408583f@posting.google.com>
"A. Sinan Unur" <usa1@llenroc.ude.invalid> wrote in message news:<Xns9588BD6C3B50Casu1cornelledu@132.236.56.8>...
> If all you want is to be able to refer to variables etc in the MyConfig
> namespace, you why not just use a proper module (with no exporter):
Is there any trickery that will make this work if I don't know the
name of the config file at compile time? For example if it's being
given as a command-line argument.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7281
***************************************