[26460] in Perl-Users-Digest
Perl-Users Digest, Issue: 8629 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 4 00:05:24 2005
Date: Thu, 3 Nov 2005 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 Thu, 3 Nov 2005 Volume: 10 Number: 8629
Today's topics:
Re: dynamically adding member sub to object? <samwyse@gmail.com>
FAQ 3.21 How can I compile my Perl program into byte co <comdog@pair.com>
Re: FAQ 3.24 Can I write useful Perl programs on the co <abigail@abigail.nl>
Permutations <bertilow@gmail.com>
Re: s///x <tadmc@augustmail.com>
Re: s///x <abigail@abigail.nl>
Re: s///x <rvtol+news@isolution.nl>
Re: s///x <abigail@abigail.nl>
Re: s///x <rvtol+news@isolution.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 04 Nov 2005 03:59:25 GMT
From: Samwyse <samwyse@gmail.com>
Subject: Re: dynamically adding member sub to object?
Message-Id: <x0Baf.12704$tV6.10470@newssvr27.news.prodigy.net>
Eckstein C. wrote:
> Is this possible? (Without using eval, as the scripts in this app seem
> to be wrapped in a eval behind the scenes, evident when theres an error
> in the code, in the error mesages. )
It might be impossible, if the package was implemented via the
'flyweight' design pattern. See page 39 (Encapsulation via scalars) of
http://www.csse.monash.edu.au/~damian/papers/PDF/cyberdigest.pdf
------------------------------
Date: Fri, 4 Nov 2005 05:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@pair.com>
Subject: FAQ 3.21 How can I compile my Perl program into byte code or C?
Message-Id: <dkeq25$s5e$1@reader2.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.
--------------------------------------------------------------------
3.21: How can I compile my Perl program into byte code or C?
(contributed by brian d foy)
In general, you can't do this. There are some things that may work for
your situation though. People usually ask this question because they
want to distribute their works without giving away the source code, and
most solutions trade disk space for convenience. You probably won't see
much of a speed increase either, since most solutions simply bundle a
Perl interpreter in the final product (but see "How can I make my Perl
program run faster?").
The Perl Archive Toolkit ( http://par.perl.org/index.cgi ) is Perl's
analog to Java's JAR. It's freely available and on CPAN (
http://search.cpan.org/dist/PAR/ ).
The B::* namespace, often called "the Perl compiler", but is really a
way for Perl programs to peek at its innards rather than create
pre-compiled versions of your program. However. the B::Bytecode module
can turn your script into a bytecode format that could be loaded later
by the ByteLoader module and executed as a regular Perl script.
There are also some commercial products that may work for you, although
you have to buy a license for them.
The Perl Dev Kit ( http://www.activestate.com/Products/Perl_Dev_Kit/ )
from ActiveState can "Turn your Perl programs into ready-to-run
executables for HP-UX, Linux, Solaris and Windows."
Perl2Exe ( http://www.indigostar.com/perl2exe.htm ) is a command line
program for converting perl scripts to executable files. It targets both
Windows and unix platforms.
--------------------------------------------------------------------
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: 04 Nov 2005 01:45:56 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: FAQ 3.24 Can I write useful Perl programs on the command line?
Message-Id: <slrndmlfak.pgk.abigail@alexandra.abigail.nl>
John Bokma (john@castleamber.com) wrote on MMMMCDXLVIII September
MCMXCIII in <URL:news:Xns9703C27C61B91castleamber@130.133.1.4>:
() "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
()
() > John Bokma <john@castleamber.com> wrote in
() > news:Xns9703AB74F4C91castleamber@130.133.1.4:
() >
() >> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
() >>
() >>> D:\> perl -e "print qq{my var$_;\n} for (1 .. 1_000_000)" > payme.pl
() >>
() >> Now why did you do that... :-( (j/k, I blame Ben Wa, and will tell so
() >> to his provider in a not nice way)
() >
() > I had plonked him right after seeing his reply to the Emacs modules
() > message so I did not realize what he had done after that. I noticed it
() > when I checked the group later on my office machine.
() >
() > I beg forgiveness,
()
() LOL
()
() > and I'll get in touch with Adelphia as well.
()
() Let's hope he loses his account. A joke is ok, but 24 x 40.000 lines is
() uhm, well...
Uhm, about 2 seconds of traffic of a random abpe group? Less traffic
than the front page of many websites? It's about 14 Mb. Not nice,
and a lot of traffic back in 1985. But it's now 2005. Where people
swap entire movies. In 2005, misuse of 14Mb isn't worth bothering
people at an ISP for.
Abigail
--
#!/opt/perl/bin/perl -w
$\ = $"; $; = $$; END {$: and print $:} $SIG {TERM} = sub {$ := $_}; kill 15 =>
fork and ($; == getppid and exit or wait) foreach qw /Just another Perl Hacker/
------------------------------
Date: Fri, 04 Nov 2005 13:53:50 +0900
From: Bertilo Wennergren <bertilow@gmail.com>
Subject: Permutations
Message-Id: <dkeph3$vb9$1@domitilla.aioe.org>
As part of an application I had to solve a permutations problem
that gave me much more trouble than I had anticipated. I did
solve the problem, but I have a very strong feeling that my
solution is very far from the best one. It seems very cumbersome
and inefficient. If anyone has any ideas on how to do this in a
better way, I'd be glad for some input.
Here's what I want to do:
#!/usr/bin/perl -w
use strict;
my @numbers = (
[ 1, 2, 3, ],
[ 4, 5, ],
[ 6, 7, ],
);
my $permutations = CreatePermutations(\@numbers);
for (@$permutations) {
print $_ . "\n";
}
sub CreatePermutations {
my $numbers = shift;
my @permutations;
# Magic stuff goes here!
return \@permutations;
}
The print result should be this exciting bunch of numbers:
1 4 6
1 4 7
1 5 6
1 5 7
2 4 6
2 4 7
2 5 6
2 5 7
3 4 6
3 4 7
3 5 6
3 5 7
As you can see the program lists all possible permutations of
picking one number from each group of numbers in the array
"@numbers". The problem for me was that the subroutine
"CreatePermutations" must work for any number of groups, and for
any number of numbers in each group (at least one group though,
and at least one number in each group).
Here's my working but probably very inefficient version of the
subroutine:
sub CreatePermutations {
my $numbers = shift;
my @permutations;
my $h = 1;
for (@$numbers) {
$h = $h * @$_;
}
$#permutations = $h-1;
for (my $i = 0; $i < @$numbers; $i++) {
my $k = 1;
my $l = 0;
my $m = 0;
for (my $j = $i+1; $j < @$numbers; $j++) {
$k = $k * @{$$numbers[$j]};
}
for (@permutations) {
$_ .= ${$$numbers[$i]}[$l] . ' ';
$m++;
if ($m == $k) {
$l++;
if ($l > @{$$numbers[$i]}-1) {
$l = 0;
}
$m = 0;
}
}
}
return \@permutations;
}
Any ideas on how to do that in a better way? The major issue is
speed since the number of permutations rise quickly when there
are lots of groups with lots of numbers.
--
Bertilo Wennergren <http://bertilow.com>
------------------------------
Date: Thu, 3 Nov 2005 16:01:59 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: s///x
Message-Id: <slrndml26n.n15.tadmc@magna.augustmail.com>
Dr.Ruud <rvtol+news@isolution.nl> wrote:
> Ala Qumsieh:
>> $x = s/ # Replace
>> 1 # ONE
>> 2 # TWO
>> 3 # THREE
>> / # by
>> 4 . # FOUR
>> 5 . # FIVE
>> 6 # SIX
>> /gsex; # global sex
>
> Thanks, that looks workable. Will the o-modifier make up for any lost
> performance?
Of course not.
s///o is a no-op when there are no variables in the pattern part.
s///o has no effect whatsoever on the replacement string part.
but
/gosex; # go have sex
would be cute to have in code. :-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 03 Nov 2005 23:25:55 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: s///x
Message-Id: <slrndml743.pgk.abigail@alexandra.abigail.nl>
Dr.Ruud (rvtol+news@isolution.nl) wrote on MMMMCDXLVII September MCMXCIII
in <URL:news:dke4nr.100.1@news.isolution.nl>:
** Ala Qumsieh:
**
**
** > Regular expressions can be messy, so having an
** > option to add comments, and 'beautify' them is a good idea. The
** > replacement part of an s/// is simply a string, and won't really
** > benefit much from such an option.
**
** I met this with some rather lengthy strings of \x{####} in both the
** search and the replacement part.
**
**
** > Moreover, you CAN add comments in the replacement part if you want to.
** > You just need to modify your code slightly, and use the /e modifier.
** > From your example:
** >
** > $_ = "abc 123 def 123 ghi";
** >
** > $x = s/ # Replace
** > 1 # ONE
** > 2 # TWO
** > 3 # THREE
** > / # by
** > 4 . # FOUR
** > 5 . # FIVE
** > 6 # SIX
** > /gsex; # global sex
**
** Thanks, that looks workable. Will the o-modifier make up for any lost
** performance? I'll test it.
No. /o only matters if you have a variable inside regexp, and then only
if you encounter the regex more than once with a different value in the
variable. And then only if you want to keep using the old value.
My advice is to *never* use /o. There's no point in using it for speed,
and when it matters for speed, the effect may not be what you want - and
even if you want it, it may confuse anyone else looking at the code.
for (qw /foo bar/) {
print /$_/ ? "Yes 1\n" : "No 1\n";
print /$_/o ? "Yes 2\n" : "No 2\n";
}
__END__
Yes 1
Yes 2
Yes 1
No 1
Abigail
--
INIT {print "Perl " }
CHECK {print "another "}
END {print "Hacker\n"}
BEGIN {print "Just " }
------------------------------
Date: Fri, 4 Nov 2005 01:11:44 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: s///x
Message-Id: <dkectf.1bg.1@news.isolution.nl>
Abigail:
> /o only matters if you have a variable inside regexp, and then
> only
> if you encounter the regex more than once with a different value in
> the variable. And then only if you want to keep using the old value.
I have series of substitutions that have to be tried in order on every
line of many files.
To make the code more readable, I can store these substitutions in a
hash (with keys like 'A01' meaning phase A, first substitution).
It is no problem to unloop the code for speed, so it might look like:
$x = s/$re{'A01'}[SRCH]/$re{'A01')[REPL]/gsx; # or /gosx
print STDERR $re{'A01'}[NAME], $x if ($x > $re{'A01'}[MIN]);
$x = s/$re{'A02'}[SRCH]/$re{'A02')[REPL]/gsx;
print STDERR $re{'A02'}[NAME], $x if ($x > $re{'A02'}[MIN]);
(and then dozens more)
If possible, I would like the modifiers to be in $re{'key'}[MODS].
(yes, this is all totally untested code yet)
OK, let me first try and test the alternatives. I still have a few days.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 04 Nov 2005 01:05:15 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: s///x
Message-Id: <slrndmlcub.pgk.abigail@alexandra.abigail.nl>
Dr.Ruud (rvtol+news@isolution.nl) wrote on MMMMCDXLVIII September
MCMXCIII in <URL:news:dkectf.1bg.1@news.isolution.nl>:
-: Abigail:
-:
-: > /o only matters if you have a variable inside regexp, and then
-: > only
-: > if you encounter the regex more than once with a different value in
-: > the variable. And then only if you want to keep using the old value.
-:
-:
-: I have series of substitutions that have to be tried in order on every
-: line of many files.
-:
-: To make the code more readable, I can store these substitutions in a
-: hash (with keys like 'A01' meaning phase A, first substitution).
-:
-: It is no problem to unloop the code for speed, so it might look like:
-:
-: $x = s/$re{'A01'}[SRCH]/$re{'A01')[REPL]/gsx; # or /gosx
-: print STDERR $re{'A01'}[NAME], $x if ($x > $re{'A01'}[MIN]);
The question you should ask here is: does "$re{A01}[SRCH]" change?
And if it does, do you want to keep using the *old* value? If the
answer to both questions is yes, you could use /o (although I would
use qr//). If latter question is answered with 'no', using /o will
make that your program will produce the wrong results. If the first
question is answered with 'no', then using /o doesn't matter.
-: $x = s/$re{'A02'}[SRCH]/$re{'A02')[REPL]/gsx;
-: print STDERR $re{'A02'}[NAME], $x if ($x > $re{'A02'}[MIN]);
-:
-: (and then dozens more)
-:
Suppose you have @lines containing all the lines you want to inspect,
and @regexes with all the regexes (as strings), there is a gigantic
difference between:
for my $line (@lines) {
for my $regex (@regexes) {
$line =~ /$regex/
}
}
and
for my $regex (@regexes) {
for my $line (@lines) {
$line =~ /$regex/
}
}
The first code snippet means that you will be doing
scalar (@lines) * scalar (@regexes)
regex compilations, while in the latter case, you only will be
doing
scalar (@regexes)
compilations. (Except if you have only one regex, then you will be
compiling only once, in both code snippets).
-: If possible, I would like the modifiers to be in $re{'key'}[MODS].
-: (yes, this is all totally untested code yet)
s/(?$re{key}[MODS])$re{key}[SRCH]/$re{key}[REPL]/
ought to do the trick.
Abigail
--
map{${+chr}=chr}map{$_=>$_^ord$"}$=+$]..3*$=/2;
print "$J$u$s$t $a$n$o$t$h$e$r $P$e$r$l $H$a$c$k$e$r\n";
------------------------------
Date: Fri, 4 Nov 2005 04:29:59 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: s///x
Message-Id: <dkeo51.18o.1@news.isolution.nl>
Abigail:
> Ruud:
> does "$re{A01}[SRCH]" change?
No, it's a constant.
> If the first
> question is answered with 'no', then using /o doesn't matter.
OK. I still hesitate that /o really doesn't matter, because I still
expect that a test needs to be done to find out if the variable has
changed or not, but even with such a (fast) test it can hardly matter.
>> If possible, I would like the modifiers to be in $re{'key'}[MODS].
>> (yes, this is all totally untested code yet)
>
> s/(?$re{key}[MODS])$re{key}[SRCH]/$re{key}[REPL]/
>
> ought to do the trick.
Ah, nice. Just another thing that I had read about but hadn't used yet.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
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 8629
***************************************