[25208] in Perl-Users-Digest
Perl-Users Digest, Issue: 7454 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 27 00:06:04 2004
Date: Fri, 26 Nov 2004 21:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 26 Nov 2004 Volume: 10 Number: 7454
Today's topics:
Re: counting words in a file ioneabu@yahoo.com
Re: counting words in a file ioneabu@yahoo.com
Re: Dat files help "Att James" hope@hope.com
Re: Dat files help "Att James" <spamtrap@dot-app.org>
Re: Dat files help "Att James" <tadmc@augustmail.com>
FAQ 9.21: How do I use MIME to make an attachment to a <comdog@panix.com>
Re: looking for a better regexp <eon@hotmail.com>
Re: looking for a better regexp <tadmc@augustmail.com>
Re: post into hash table <postmaster@castleamber.com>
Re: post into hash table <tintin@invalid.invalid>
Re: Question about implementing range operator with str <see@sig.invalid>
Re: recursive bruteforce ASCII range <wksmith@optonline.net>
Redirecting STDOUT to Scalar behaves not as expected. W <zaphod@s4r.de>
Re: Storing a substitution (similar to qr()) <see@sig.invalid>
Re: Storing a substitution (similar to qr()) <nobull@mail.com>
Re: Storing a substitution (similar to qr()) <tadmc@augustmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 26 Nov 2004 19:05:41 -0800
From: ioneabu@yahoo.com
Subject: Re: counting words in a file
Message-Id: <1101524741.137398.62800@f14g2000cwb.googlegroups.com>
>
> No warnings?
>
Sorry, I had no good reason to leave it out really. Interestingly, as
fast as the program runs on the pda (400mhz x-scale 32mb ram), any
'use' statements add a second or two to the run time. It's no big deal
while playing around, but if I were to actually use a script for real
work, I would probably comment out 'use strict' and 'use warnings' and
avoid any absolutely unnecessary modules.
> > my $fn = "/textucation/moby10b.txt";
> > open (INF, $fn) or die "error: $!";
> > my %words;
> > for (<INF>)
> > {
> > $words{lc $1}++ while /(\w+)/gi;
>
> The /i modifier does nothing and shouldn't be there.
don't you need the /i so words like 'The' and 'the' are counted as the
same word?
>
> I'd use split without arguments for that. You break "gable-ended
> Spouter-Inn" into "gable" and "ended" plus "Spouter" and "Inn", split
> keeps the hyphenated words.
>
> > }
>
> Okay so far, you have a hash of words and their count now. What
follows
> is wrong.
>
> > my @n = %words;
> > @n = reverse @n;
> > %words = @n;
>
> %words = reverse %words;
>
> That would do the same, but it would be equally wrong. You can only
> reverse a hash without loss of information when the values are
unique,
> but your word counts aren't. Looking at your output, have you
noticed
> that there appears to be only one word Melville used exactly once?
> Every other word he must have used at least twice. That is unlikely,
> and is indeed an artifact of this hash reversal.
>
> > my @k = keys %words;
> > sub num {$b <=> $a}
> > @k = sort num @k;
>
> Scratch everything after the counting loop and simply do
>
> my @k = sort { %words{ $b} <=> %words{ $a} } keys %words;
cool, thanks! Good thing I was doing this for fun and not being paid
for the results :-) I guess I should test on a smaller text file with
known results. Thank you for trying out my code on the same text. If
anyone is looking for vast stores of text to play with, the Gutenberg
Project http://promo.net/pg/ has many gigabytes of plain text files of
classic literature. I have to make sure to leave out the introduction
GP puts at the beginning of the file in my searches. Melville
certainly didn't use the word computer, but my search came up with a
couple instances because of their intro.
>
> That's your sort.
>
> > open (OUTF, ">file count.txt") or die "error: $!";
> > print OUTF "$_ $words{$_}\r\n" for (@k);
>
> Anno
Thanks again!
wana
------------------------------
Date: 26 Nov 2004 19:05:55 -0800
From: ioneabu@yahoo.com
Subject: Re: counting words in a file
Message-Id: <1101524755.035471.63410@f14g2000cwb.googlegroups.com>
>
> No warnings?
>
Sorry, I had no good reason to leave it out really. Interestingly, as
fast as the program runs on the pda (400mhz x-scale 32mb ram), any
'use' statements add a second or two to the run time. It's no big deal
while playing around, but if I were to actually use a script for real
work, I would probably comment out 'use strict' and 'use warnings' and
avoid any absolutely unnecessary modules.
> > my $fn = "/textucation/moby10b.txt";
> > open (INF, $fn) or die "error: $!";
> > my %words;
> > for (<INF>)
> > {
> > $words{lc $1}++ while /(\w+)/gi;
>
> The /i modifier does nothing and shouldn't be there.
don't you need the /i so words like 'The' and 'the' are counted as the
same word?
>
> I'd use split without arguments for that. You break "gable-ended
> Spouter-Inn" into "gable" and "ended" plus "Spouter" and "Inn", split
> keeps the hyphenated words.
>
> > }
>
> Okay so far, you have a hash of words and their count now. What
follows
> is wrong.
>
> > my @n = %words;
> > @n = reverse @n;
> > %words = @n;
>
> %words = reverse %words;
>
> That would do the same, but it would be equally wrong. You can only
> reverse a hash without loss of information when the values are
unique,
> but your word counts aren't. Looking at your output, have you
noticed
> that there appears to be only one word Melville used exactly once?
> Every other word he must have used at least twice. That is unlikely,
> and is indeed an artifact of this hash reversal.
>
> > my @k = keys %words;
> > sub num {$b <=> $a}
> > @k = sort num @k;
>
> Scratch everything after the counting loop and simply do
>
> my @k = sort { %words{ $b} <=> %words{ $a} } keys %words;
cool, thanks! Good thing I was doing this for fun and not being paid
for the results :-) I guess I should test on a smaller text file with
known results. Thank you for trying out my code on the same text. If
anyone is looking for vast stores of text to play with, the Gutenberg
Project http://promo.net/pg/ has many gigabytes of plain text files of
classic literature. I have to make sure to leave out the introduction
GP puts at the beginning of the file in my searches. Melville
certainly didn't use the word computer, but my search came up with a
couple instances because of their intro.
>
> That's your sort.
>
> > open (OUTF, ">file count.txt") or die "error: $!";
> > print OUTF "$_ $words{$_}\r\n" for (@k);
>
> Anno
Thanks again!
wana
------------------------------
Date: Fri, 26 Nov 2004 23:15:26 GMT
From: hope@hope.com
Subject: Re: Dat files help "Att James"
Message-Id: <egdfq0lg6obfgbtevl6ntb5ac5hfv1tcg2@4ax.com>
On Fri, 26 Nov 2004 16:54:16 -0500, Sherm Pendley <spamtrap@dot-app.org>
wrote:
>> Yes you are correct , It was set to 180 characters. NOW set to 72 is
>> that ok for you?
>
>Tad's not trying to be a dictator - he's just pointing out an old USENET
>tradition that dates back to 80-column paper terminals, meaning that
>it's older than a lot of the people who post here.
Ok I have no problem at all with people pointing out these things to me
but it does get a bit confusing when you get different version of it
>> BTW that gets me confused some have a " in front and others have '
>> like
>> "c:/tom/fred"
>> 'c:/tom/fred'
>
>The difference is variable interpolation. When you use double quotes,
>variables and escape sequences inside the quotes are expanded. When you
>use single quotes, they are not. So this code:
>
>my $cat = 'tom';
>print "$cat\n";
>print '$cat', "\n";
>
>Produces this output:
>
>tom
>$cat
>
>The above is *very* simplified. Have a look in "perldoc perlop" for more
>(much more) in the section titled "Quote and Quote-like Operators".
Oh thank you for that I will have a look
That is one of the things that I did not understand when People kept
saying look under "perldoc xxxx" Until I did a search on it and
found out what they meant :)) I think it is like a lot of things when
you are telling people what to do, you assume that there know what you
are talking about.
>
>> Fine now I have had a telling off
>
>I don't think Tad intended to tell you off. The group guidelines are
>here for everyone's benefit, including yours. He's not trying to be mean
>by pointing them out to you, he's trying to help.
Ha Ha Ha No I did not think Tad was telling me of , My way of joking
about it
The thing that does anoy me is when someone say's you should not do that
and then never tells you what you should be doing, so how do you know
what to do
>> If you missed my first post
>
>I replied to that, asking for clarification on some points - did your
>news provider pick up the reply? If not, I'll repost it.
No I did not get it or I missed it
Yes please if you could repost it I will then try to give you the
information that you want. Thank you
>
>And thank you, by the way, for moving this from a.c.p.freelance and into
>this group. Like Tad, I was intended to be helpful, not mean - you'll
>get more and better answers when you ask the right group.
That is no problem
Thank you for trying to help me with this
It as been going on a long time now, still I have learnt a lot from the
post's
------------------------------
Date: Fri, 26 Nov 2004 18:43:25 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Dat files help "Att James"
Message-Id: <16idnXdF2v8AIjrcRVn-3g@adelphia.com>
hope@hope.com wrote:
> No I did not get it or I missed it
> Yes please if you could repost it
It's archived on Google:
<http://tinyurl.com/4os8m>
<http://groups.google.com/groups?safe=off&as_ugroup=comp.lang.perl.misc&as_umsgid=lb-dncoF1pMKFD3cRVn-1w@adelphia.com&lr=&hl=en>
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Fri, 26 Nov 2004 19:56:53 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Dat files help "Att James"
Message-Id: <slrncqfnn5.4v7.tadmc@magna.augustmail.com>
hope@hope.com <hope@hope.com> wrote:
> On Fri, 26 Nov 2004 16:54:16 -0500, Sherm Pendley <spamtrap@dot-app.org>
> wrote:
>
>>> Yes you are correct , It was set to 180 characters. NOW set to 72 is
>>> that ok for you?
>>
>>Tad's not trying to be a dictator - he's just pointing out an old USENET
>>tradition that dates back to 80-column paper terminals, meaning that
>>it's older than a lot of the people who post here.
>
> Ok I have no problem at all with people pointing out these things to me
> but it does get a bit confusing when you get different version of it
That's why we took the trouble to discuss and agree on what we wanted
to have here, then we wrote them all down in one place.
Just go by those, they are the proven consensus here.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 27 Nov 2004 05:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 9.21: How do I use MIME to make an attachment to a mail message?
Message-Id: <co91q5$ngo$1@reader1.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
9.21: How do I use MIME to make an attachment to a mail message?
This answer is extracted directly from the MIME::Lite documentation.
Create a multipart message (i.e., one with attachments).
use MIME::Lite;
### Create a new multipart message:
$msg = MIME::Lite->new(
From =>'me@myhost.com',
To =>'you@yourhost.com',
Cc =>'some@other.com, some@more.com',
Subject =>'A message with 2 parts...',
Type =>'multipart/mixed'
);
### Add parts (each "attach" has same arguments as "new"):
$msg->attach(Type =>'TEXT',
Data =>"Here's the GIF file you wanted"
);
$msg->attach(Type =>'image/gif',
Path =>'aaa000123.gif',
Filename =>'logo.gif'
);
$text = $msg->as_string;
MIME::Lite also includes a method for sending these things.
$msg->send;
This defaults to using sendmail but can be customized to use SMTP via
Net::SMTP.
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: Fri, 26 Nov 2004 23:31:45 GMT
From: "Leon" <eon@hotmail.com>
Subject: Re: looking for a better regexp
Message-Id: <B1Ppd.1746$8K4.1471@newsfe5-win.ntli.net>
"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrncqej4u.3rp.tadmc@magna.augustmail.com...
> Leon <eon@hotmail.com> wrote:
>
> > $line =~ s/[\t ]//g;
>
>
> This will do the same thing, only faster:
>
> $line =~ tr/\t //d;
Thanks for that Tad:) (always interested in learning some new) Out of
curiousity, does that apply on all occasions when performing pattern
matching that would work using either ~s~ or ~tr~? (Is ~tr~ always gonna be
faster?)
>
>
> --
> Tad McClellan SGML consulting
> tadmc@augustmail.com Perl programming
> Fort Worth, Texas
------------------------------
Date: Fri, 26 Nov 2004 19:52:47 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: looking for a better regexp
Message-Id: <slrncqfnff.4v7.tadmc@magna.augustmail.com>
Leon <eon@hotmail.com> wrote:
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrncqej4u.3rp.tadmc@magna.augustmail.com...
>> Leon <eon@hotmail.com> wrote:
>>
>> > $line =~ s/[\t ]//g;
>>
>>
>> This will do the same thing, only faster:
>>
>> $line =~ tr/\t //d;
>
> Thanks for that Tad:) (always interested in learning some new) Out of
> curiousity, does that apply on all occasions when performing pattern
> matching that would work using either ~s~ or ~tr~?
Your question does not make sense as phrased, because there
is NO pattern matching going on with tr/// despite the fact
that it looks an awful lot like a s/// and can be used with
the binding operator.
> (Is ~tr~ always gonna be
> faster?)
Yes, without a doubt.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 27 Nov 2004 01:13:17 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: post into hash table
Message-Id: <Xns95ADC386FB350castleamber@130.133.1.4>
Gunnar Hjalmarsson wrote:
> Bob Walton wrote:
>> Bremse wrote:
>>> I'm trying to put values (from form) posted by post method into hash
>>
>> One possibility: The query string could use the ; character to
>> separate key-value pairs rather than the & character (either is
>> permitted and possibly generated by a web server).
>
> The query string is probably empty, since it's a form submission via
> the POST method, and forms always use & as the separator.
Sometimes I call POST forms using GET, sometimes I use a script. Don't rely
on what you think is on your page.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Sat, 27 Nov 2004 16:42:23 +1300
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: post into hash table
Message-Id: <30qba7F308958U1@uni-berlin.de>
"Bremse" <bremse{usun.to}@wp.pl> wrote in message
news:pegeq0lj9qjqj42a9iq7i2rqkn03rsdf5o@4ax.com...
> I'm trying to put values (from form) posted by post method into hash
> table, but all entries after code execution are empty (i.e.
> $FORM_DATA{$forename} equals ""). Why?
That's what happens when you try doing something as complex as CGI parsing
yourself.
use CGI;
my $q = new CGI;
my %FORM_DATA = $q->Vars;
------------------------------
Date: Fri, 26 Nov 2004 19:02:39 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: Question about implementing range operator with strings
Message-Id: <41a7c1a4$1_2@127.0.0.1>
bayxarea-usenet@yahoo.com wrote:
> I had a routine that worked fine with a range operator while looping
> through a filehandle... but I have since changed the data source and it
> is now already in an array... and the range operator does not appear to
> be working...
>
> I am basically extracting lines of data from a larger set.
>
>
> --------------------
> before -------------
>
> open(IN,"< $file");
>
> while (<IN>) {
> if (/$start_string/ .. /$end_string/) {
> if (($_ !~ /$start_string/) && ($_ !~ /$end_string/)) {
> ...
> ... do something with $_ ...
> ...
> } # end if
> } # end if
> } # end while
> close IN;
>
>
> --------------------
> now -------------
>
>
> @data_from_file; # lines of data retrieved elsewhere
>
> foreach (@data_from_file) {
> if (/$start_string/ .. /$end_string/) {
> if (($_ !~ /$start_string/) && ($_ !~ /$end_string/)) {
> ...
> ... do something with $_ ...
> ...
> } # end if
> } # end if
> } # end foreach
Seems to work fine for me:
use warnings;
use strict;
my @data_from_file=<DATA>;
my $start_string='aaa';
my $end_string='zzz';
foreach (@data_from_file) {
if (/$start_string/ .. /$end_string/) {
if (($_ !~ /$start_string/) && ($_ !~ /$end_string/)) {
print "line included: $_";
} # end if
} # end if
} # end foreach
__END__
blah blah blah
blah aaa blah
blah 3 blah
blah 4 blah
blah 5 blah
blah zzz blah
blah 7 blah
blah 8 blah
test aaa test
test 10 test
test 11 test
test zzz test
test 13 test
test 14 test
generates:
D:\junk>perl junk503.pl
line included: blah 3 blah
line included: blah 4 blah
line included: blah 5 blah
line included: test 10 test
line included: test 11 test
D:\junk>
Isn't the above the behavior you want? If not, put together a short
program with data (like the above) that anyone can copy/paste/run that
demonstrates the problem you are having, and clearly state exactly what
that problem is.
>
>
> Also -- there are now multiple places within the @data_from_file array
> that I want to extract the data for which I am searching.
> So want to be able to go inside (between the start and end strings)
> each time the pair occurs within the array.
>
> I thought of using something like this:
>
> while ($_ = shift @data_from_file) {
What if one of the lines has a false value? The loop will stop right
there. Also note that you will be destroying @data_from_file as you go.
> if (/$start_string/ .. /$end_string/)
> ...
> ... etc
>
> but it did not work either.
"did not work" -- precisely what did it do that you didn't expect it
would do, or didn't do that you did expect it to do? Were there errors?
If so, exactly what did they say?
>
> After reading the docs I thought perhaps a slice would work - but I am
> not sure how to tie the range to the elements of the array.
>
> @data_I_want = (/$start_string/ .. /$end_string/) # ????
Note that .. in a list context is an *entirely* different operator.
>
> Thanks for any clues as to how to change this to work from a while loop
> on the file handle to looping through each element of the array.
>
> And - as I mentioned - I need to do this multiple times as the block I
> am searching for will occur several times - so ... multiple slices on
> the same array? Is that possible?
As demonstrated in my example program above, the range operator will
switch on/off an indefinite number of times, whenever the first/second
condition transitions from false to true. If that isn't the behavior
you want, please clearly state what the behavior you want is.
...
> John
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Fri, 26 Nov 2004 23:39:59 -0500
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: recursive bruteforce ASCII range
Message-Id: <FxTpd.4132$t61.2568@fe12.lga>
"bernd" <bernd@thebc.ch> wrote in message
news:b741babc.0411260850.157063a1@posting.google.com...
> #!/usr/bin/perl -w
> #
> # hy group
> #
> # i feel really stupid. could it be that hard?
> #
> # i try to write a script to bruteforce the ASCII range. something
> # like:
> #
> # a..z
> # aa..zz
> # aaa..zzz
> # ...
> #
This specification omits many details, the most important of which is
the order of the strings within each group.
> # but there must be a smarter, solution than writing the same stuff
> # over and over again. i'd tried some recursive stuff but failed.
> #
As I have said in other threads, recursive solutions are probably never
"necessary" and seldom if ever efficient. Recursion can often be used
to compactly specify a function which would be awkward otherwise. A
perl implementation of such a spec can be almost indistinguishable from
the spec. This is certainly an advantage in program validation.
OK, I'll be honest. I never actually wrote the formal spec for this
one. Here is my recursive solution.
$ARGV[0] = 3 unless defined @ARGV;
my $N = $ARGV[0]-1;
my @SET = ('a'..'z', 0..9);
recurse(''); # Run the recursion
sub recurse{
my $prefix = $_[0];
if (length $prefix < $N){
recurse($prefix.$_) foreach @SET;
} else{
print $prefix.$_."\n" foreach @SET;
}
}
Note that the global "variable" names $N and @SET are in upper case to
emphasize that they are logically constant. The command line argument
(default 3) is the OP's "n". My $N is one less.
Bill
------------------------------
Date: Sat, 27 Nov 2004 01:24:07 +0100
From: zaphod <zaphod@s4r.de>
Subject: Redirecting STDOUT to Scalar behaves not as expected. Why?
Message-Id: <pan.2004.11.27.00.24.06.779000@s4r.de>
Hi,
to explain what I want to do, I made to demos:
This is the first version, which does exactly what I want to do except,
that it writes STDOUT to a file:
################################################################################
## demo1.pl
################################################################################
use IO::File;
# My Little Programm
my @PROGRAMM = (
"# This is a demo-Programm\n",
"print \"Hello World!\\n\";\n",
"for my \$i (1..5){\n",
" print \"\$i\\n\";\n",
"}\n"
);
# Saving STDOUT
open(OLDOUT,">&STDOUT") || die("[$PROGNAME] Couldn't dup STDOUT\n");
# Redirecting STDOUT to file
close(STDOUT);
die("Couldn't write perl.out\n")
unless open(STDOUT,">perl.out");
# Open Pipe to Perl
my $perl = new IO::File;
$perl->open("|perl");
# Run Programm
for my $code (@PROGRAMM){
print {$perl} $code;
}
# Restore STDOUT
close(STDOUT);
open(STDOUT,">&OLDOUT");
print "Hello again!\n";
################################################################################
now I try to redirect the output to a scalar variable, which works for the
simple print statement, but not for the output of the perl-interpreter
where the programm is piped to:
################################################################################
## demo2.pl
################################################################################
use IO::File;
use IO::Scalar;
# My Little Programm
my @PROGRAMM = (
"# This is a demo-Programm\n",
"print \"Hello World!\\n\";\n",
"for my \$i (1..5){\n",
" print \"\$i\\n\";\n",
"}\n"
);
# Saving STDOUT
open(OLDOUT,">&STDOUT") || die("[$PROGNAME] Couldn't dup STDOUT\n");
# Redirecting STDOUT to Scalar
my $scalar = "";
close(STDOUT);
tie *STDOUT, 'IO::Scalar', \$scalar;
# Show that STDOUT to Scalar works somehow:
print "Hi";
# Open Pipe to Perl
my $perl = new IO::File;
$perl->open("|perl");
# Run Programm
for my $code (@PROGRAMM){
print {$perl} $code;
}
# Restore STDOUT
close(STDOUT);
untie *STDOUT;
open(STDOUT,">&OLDOUT");
print "Hello again!\n";
print ">>>$scalar<<<\n";
################################################################################
What I do not understand is, why there is a different behaviour in the two
versions of the programm and what I can do to capture the result of piping
the programm to perl into $scalar.
Hope there is somebody out there, who can help.
Regards
Thomas
PS.: I know about the existance of "eval", so please no comments about it.
------------------------------
Date: Fri, 26 Nov 2004 18:13:46 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: Storing a substitution (similar to qr())
Message-Id: <41a7b62e$1_3@127.0.0.1>
Arvin Portlock wrote:
>> Tad McClellan <tadmc@augustmail.com> wrote:
>
>
>
>> >my $match = 'I saw (.*) and (.*)';
>> >my $repl = 'I saw $2 and $1';
>> >my $regexp = qr($match);
>> >$example =~ s/$regexp/$repl/;
>>
>> my $repl = '"I saw $2 and $1"';
>> ^ ^ note the quotes
>> ...
>> $example =~ s/$regexp/$repl/ee;
>
>
>
> I've never seen 'ee' at the end of a substitution before.
> It's contained in an example in perlop but without any
> explanation as far as I can find.
Each "e" causes the replacement to be evaluated as a Perl expression, as
in eval(), rather than be treated as a double-quoted string. If there
are two e's, the replacement is simply evaluated twice.
>
> Although your example worked I haven't managed to get
> it to work in my particular situation, where a list of
> these substitutions are read in from a text file. In fact,
> even if I got it to work I'm not sure I could use it be-
> cause of the 'e' there (I'm concerned about executing
> malicious code). But still I wonder how I could get it to
> work in the example below (where a small here-document
> replicates the external text file):
>
> my $example = 'I saw Bob and Alice';
>
> my $user_subs =<<'EOF';
> /I saw (.*) and (.*)/I saw $2 and $1/
> /I saw (.*) and (.*)/$1 and $2 saw me/
> EOF
>
> my @user_subs = split (/\n/, $user_subs);
>
> my @substitutions;
> foreach my $user_sub (@user_subs) {
> $user_sub =~ s/^\///;
> $user_sub =~ s/\/$//;
> my ($match, $replace) = split (/\//, $user_sub);
> my $pattern = {};
> $pattern->{match} = $match;
> $pattern->{replace} = $replace;
> push @substitutions, $pattern;
> }
>
> foreach my $pattern (@substitutions) {
> my $match = $pattern->{match};
> my $replace = $pattern->{replace};
> $example =~ s/$match/$replace/ee;
You're missing a couple of levels of quotes (as Tad mentioned, BTW).
After all, it is a Perl expression you are evaluating each time, so it
has to be valid Perl. And you want the substitutions to occur. So you
need double-quoted strings to eval each time. Try:
$example=~s/$match/"\"$replace\""/ee;
> }
>
> print "$example\n";
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Sat, 27 Nov 2004 00:56:00 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Storing a substitution (similar to qr())
Message-Id: <co8jb2$18o$1@slavica.ukpost.com>
Bob Walton wrote:
> Arvin Portlock wrote:
>
>> I've never seen 'ee' at the end of a substitution before.
>> It's contained in an example in perlop but without any
>> explanation as far as I can find.
>
>
> Each "e" causes the replacement to be evaluated as a Perl expression, as
> in eval(), rather than be treated as a double-quoted string. If there
> are two e's, the replacement is simply evaluated twice.
It's not nearly as simple as you might think.
A single /e qualifier does not involve any eval() in the sense the it
does not involve any run-time compilation of code. The code yyy is
compiled at compilation time.
Indeed...
s/xxx/yyy/e;
... should be really be considered as more primatve than...
s/xxx/yyy/;
The s///e operator can be seen as a function that takes two arguments,
the first being a regex and the second a block of code that is evaluated
to find the replacement.
The s/// operator without the /e modifier is the same but the block is
implicitly enclosed a qq() operation (but with a delimiter outside the
real character set).
So...
s/xxx/yyy/e; # The most primative form
s/xxx/yyy/; # Same as s/xxx/qq(yyy)/
s/xxx/yyy/ee;# Same as s/xxx/eval(yyy)/
------------------------------
Date: Fri, 26 Nov 2004 19:48:58 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Storing a substitution (similar to qr())
Message-Id: <slrncqfn8a.4v7.tadmc@magna.augustmail.com>
Arvin Portlock <apollock11@hotmail.com> wrote:
> I've never seen 'ee' at the end of a substitution before.
> It's contained in an example in perlop but without any
> explanation as far as I can find.
Options are:
e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
...
A C</e> will cause the
replacement portion to be treated as a full-fledged Perl expression
and evaluated right then and there. It is, however, syntax checked at
compile-time. A second C<e> modifier will cause the replacement portion
to be C<eval>ed before being run as a Perl expression.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
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 7454
***************************************