[30115] in Perl-Users-Digest
Perl-Users Digest, Issue: 1358 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 13 11:09:46 2008
Date: Thu, 13 Mar 2008 08:09:10 -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, 13 Mar 2008 Volume: 11 Number: 1358
Today's topics:
End of my tether trying to retrieve keys from multi-dim guytew@googlemail.com
Re: End of my tether trying to retrieve keys from multi <ben@morrow.me.uk>
Faster file iteration <vijay@iavian.com>
Re: Faster file iteration <m@rtij.nl.invlalid>
Re: Faster file iteration <bugbear@trim_papermule.co.uk_trim>
Re: Faster file iteration <vijay@iavian.com>
Re: Fastest way to find a match? <jurgenex@hotmail.com>
Re: Fastest way to send file to browser <howachen@gmail.com>
find a matching pattern in file and find it in another <gkreddybh@gmail.com>
Re: find a matching pattern in file and find it in anot <someone@example.com>
Re: Hell of a time extracting bits from a vector <idgarad@gmail.com>
Re: Hell of a time extracting bits from a vector <ben@morrow.me.uk>
Re: Inverted RegEx on list of numbers <nechtom@gmail.com>
Re: Matching multiple subexpressions in a regular expre <devnull4711@web.de>
Re: Matching multiple subexpressions in a regular expre <devnull4711@web.de>
My editfile.pl script <ignoramus17007@NOSPAM.17007.invalid>
new CPAN modules on Thu Mar 13 2008 (Randal Schwartz)
Re: perl html template error <jcharth@gmail.com>
Which split delimiter to use for mailbox <bart@nijlen.com>
Re: Which split delimiter to use for mailbox <m@rtij.nl.invlalid>
Re: Which split delimiter to use for mailbox <cartercc@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 13 Mar 2008 07:10:19 -0700 (PDT)
From: guytew@googlemail.com
Subject: End of my tether trying to retrieve keys from multi-dimensional hash
Message-Id: <1c7a31bd-0307-4f08-828f-b1cbae54ec18@e23g2000prf.googlegroups.com>
First off, apologies if what I'm trying to parse is not a multi-
dimensional hash - I'm still confused even having read the perldoc and
numerous usenet postings :-(
I am using a perl module to store various configurations [see snippet
below]. I want to be able to create an array called @sids that
contains the keys ZA1, YB4, XMM so that I can loop through the array
and retrieve Description, Ports or whatever using. It seems all the
variations for retrieving keys from the simple
keys %Instances to the convoluted display nothing! I'm successfully
using the Config when I know the keys, but now I need to build a list
to work from. Any solutions gratefully received.
foreach $sid (@SIDS) {
my $port = $BASIS::Config::Instances{$sid}{Ports}
# do something with the retrieved value
}
[ snippet from my Config package ]
package BASIS::Config;
use strict;
use vars qw( %Alerts %Instances %Servers );
%Instances = (
ZA1 => {
Description => "Production",
Ports => '3300 3200',
},
YB4 => {
Description => "Quality",
Ports => '3300 3200',
},
XMM => {
Description => "Development",
Ports => '3300 3200',
},
);
------------------------------
Date: Thu, 13 Mar 2008 14:34:15 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: End of my tether trying to retrieve keys from multi-dimensional hash
Message-Id: <7kqna5-h221.ln1@osiris.mauzo.dyndns.org>
Quoth guytew@googlemail.com:
> First off, apologies if what I'm trying to parse is not a multi-
> dimensional hash - I'm still confused even having read the perldoc and
> numerous usenet postings :-(
>
> I am using a perl module to store various configurations [see snippet
> below]. I want to be able to create an array called @sids that
> contains the keys ZA1, YB4, XMM so that I can loop through the array
> and retrieve Description, Ports or whatever using. It seems all the
> variations for retrieving keys from the simple
> keys %Instances to the convoluted display nothing! I'm successfully
> using the Config when I know the keys, but now I need to build a list
> to work from. Any solutions gratefully received.
>
> foreach $sid (@SIDS) {
> my $port = $BASIS::Config::Instances{$sid}{Ports}
> # do something with the retrieved value
$port is a hashref, so you need to apply 'Use Rule 1' from perlreftut:
# If I had an ordinary hash, I would extract the keys like this
keys %hash
# Replace the name of the hash with a block
keys %{ }
# Put the hashref inside
keys %{ $port }
This will do what you want. When you're comfortable with the idea, you
can shorten that into
keys %$port
since $port is a simple scalar; but remember it's just a shorthand. You
could also use something like
keys %{ $BASIS::Config::Instances{$sid}{Ports} }
without the temporary, but that's just ugly :). In this case the {}
*aren't* optional, as what's inside is too complicated.
Ben
------------------------------
Date: Thu, 13 Mar 2008 06:41:59 -0700 (PDT)
From: "vijay@iavian.com" <vijay@iavian.com>
Subject: Faster file iteration
Message-Id: <26fd1101-cf35-4b41-9cbb-945127088f84@h11g2000prf.googlegroups.com>
use strict;
my $file_1 = '1.txt'; # File 1
my $file_2 = '2.txt'; # File 2
if(open(FH1 , $file_1)){
print "File $file_1 Opened\n";
}else{
print "Failed to Open file $file_1\n";
exit;
}
if(open(FH2 , $file_2)){
print "File $file_2 Opened\n";
}else{
print "Failed to Open file $file_2\n";
close FH1;
exit;
}
while(chomp(my $line_2 = <FH2>)){
my($dummy21,$file21_no,$file21_date) = split(/\s+/,$line_2);
next if($file21_no !~ /\d+/);
my $counter1 = 0;
my $least_date1 = 0;
seek(FH1,0,0);
$least_date1 = date_compare($file21_date);
while(chomp(my $line_1 = <FH1>)){
my($d,$file1_no,$file1_date) = split(/;/,$line_1);
if($file1_no == $file21_no){
$file1_date =~/(\d\d\d\d)(\d\d)(\d\d)/;
my $yr1 = $1;
$file21_date =~/(\d\d\d\d)(\d\d)(\d\d)/;
if(($yr1 - $1) < 5){
$counter1++;
}
}
}
$least_date1 = 0 if($counter1 == 0);
print "$dummy21\t$file21_no\t$file21_date\t$counter1\t
$least_date1\n";
print FH3 "$dummy21\t$file21_no\t$file21_date\t$counter1\t
$least_date1\n";
}
Here $file_1 has around 12000000 records , it takes 2 mins to go for a
single record in $file_2.
Any suggestion to make it fast ?
------------------------------
Date: Thu, 13 Mar 2008 14:47:03 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Faster file iteration
Message-Id: <pan.2008.03.13.13.47.03@rtij.nl.invlalid>
On Thu, 13 Mar 2008 06:41:59 -0700, vijay@iavian.com wrote:
> Here $file_1 has around 12000000 records , it takes 2 mins to go for a
> single record in $file_2.
>
> Any suggestion to make it fast ?
Read file_1 once, store it in an appropriate datastructure (hash comes to
mind). It still may take two minutes to read, but after that searching is
fast.
Does take some memory, but 12 million records should take less than 100
Megs.
M4
------------------------------
Date: Thu, 13 Mar 2008 14:52:18 +0000
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: Faster file iteration
Message-Id: <13tifth6jip1m97@corp.supernews.com>
vijay@iavian.com wrote:
>
> Here $file_1 has around 12000000 records , it takes 2 mins to go for a
> single record in $file_2.
>
> Any suggestion to make it fast ?
Are the two files in date-sorted order?
BugBear
------------------------------
Date: Thu, 13 Mar 2008 08:07:01 -0700 (PDT)
From: "vijay@iavian.com" <vijay@iavian.com>
Subject: Re: Faster file iteration
Message-Id: <99e73c2e-8568-42ed-bfd4-8fb4a3631039@s13g2000prd.googlegroups.com>
On Mar 13, 7:52 pm, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
> vi...@iavian.com wrote:
>
> > Here $file_1 has around 12000000 records , it takes 2 mins to go for a
> > single record in $file_2.
>
> > Any suggestion to make it fast ?
>
> Are the two files in date-sorted order?
>
> BugBear
No , they are not sorted on date , no unique key ..
------------------------------
Date: Thu, 13 Mar 2008 04:20:03 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Fastest way to find a match?
Message-Id: <m29ht358mjoj9au43gpprqueiil1s7nmao@4ax.com>
bukzor <workitharder@gmail.com> wrote:
>Hi,
>
>I'm trying to find the fastest way in perl to see if a name contains
>another.
>
>I've a list of 2704 names (aka "A")
>
>I've another name (aka "B")
>
>I need to know if any of A is contained in B.
>
>A = foo foo1 foo2 foo3 foo45 ....
>B = INCASE_foo2_YOUWANT
>is a match
>
>B = INCASE_YOURDONOTWANT
>is not a match.
>
>what would be the fastest way to check the 2704 possible values of
>"A" ?
>
>Thanks,
>
>
>so far, I'm using
>
>foreach $t (keys %A) {
> $v = $B;
> $v = s/$t//;
> if ($v ne $B) {
What does the string value of the number of matches have to do with the
original text of $B? This condition will always succeed unless $t and $B are
both '1'.
Maybe you meant to test the result of s/// directly?
if ($v =~ s/$t/) {
However, why do a s/// and awkwardly restore $v for each iteration in the
first place? A simple
if ($B =~ m/$_/) {
will do without all the temporary assignments, which cost time!
Having said that I strongly believe your code isn't doing what you think
it's doing in the first place. Initially you wrote
"if any of A is contained in B"
But your code is testing if B is a regular expression that matches any of A.
That is something very different.
I would imagine a simple index() is what you are looking for
foreach (keys %A) {
if (index($B, $_) > -1) {
print "FOUND";
}
}
This is probably also the fastest method, but you may want to run some
benchmarks.
jue
------------------------------
Date: Wed, 12 Mar 2008 21:39:38 -0700 (PDT)
From: howa <howachen@gmail.com>
Subject: Re: Fastest way to send file to browser
Message-Id: <abc197ff-3547-45be-9a91-e617e0c959db@e23g2000prf.googlegroups.com>
On 3$B7n(B13$BF|(B, $B>e8a(B2$B;~(B04$BJ,(B, xhos...@gmail.com wrote:
>
> Probably not. I would expect captcha images to be either generated on the
> fly or stored in a database, so there is no path to hide. Or selected on
> the fly, in which case your goal is not to hide the path but to select the
> path.
I have cron job to generate the images from time to time, as real time
is expensive for server.
We also put the image file outside the document root, so we must send
the file using Perl.
>
> Given that you are going to start up Perl in the first place, I don't
> see any obvious problems with our code, other than possibly a lack of
> error checking on the sysread. And that the file handle you close
> is different than the file handle you open.
Thank you .
Howard
------------------------------
Date: Wed, 12 Mar 2008 22:13:01 -0700 (PDT)
From: nani <gkreddybh@gmail.com>
Subject: find a matching pattern in file and find it in another file too
Message-Id: <a84951c8-66b1-4f3c-a7d9-fa6908251308@u10g2000prn.googlegroups.com>
Problem Def: copy the pattern which before ",(comma)" and find a
matching pattern in another file.
status: i wrote following code. but it is not working properly. plz
help me.
#! C:\Perl\bin\perl.exe
print "hello\n";
print "Please Enter Input File name(Give the complete path):";
$infile=<STDIN>; #give the input file name here
chomp($infile);
open ($in, "<", $infile) or die "Cannot open file for reading\n";
#Check whether the file can be opened for reading
while (<$in>)
{
if(/,/) {print "before match: $`\t and after match: $'\n\n";};
$x=$';
$y=$`;
&mysubroutine($x,$y);
}
sub mysubroutine
{
$a=$x;
$b=$y;
print "Please Enter the Output File name (Give the compelte path):";
$infile=<STDIN>; #give the output file name here
chomp($infile);
open ($in, "<", $infile) or die "cannot open file to write\n";
#Check whether the file can be opened for writing
print "$b\n";
while (<$in>)
{
print "pattern to find: $y\n";
if (/$b/) {print "Cheers@\n";};
}
close $in or die "cannot close $out\n";
}
close $in or die "Cannot close $in\n";
------------------------------
Date: Thu, 13 Mar 2008 07:11:06 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: find a matching pattern in file and find it in another file too
Message-Id: <es4Cj.86127$w57.27615@edtnps90>
nani wrote:
> Problem Def: copy the pattern which before ",(comma)" and find a
> matching pattern in another file.
>=20
> status: i wrote following code. but it is not working properly. plz
> help me.
>=20
>=20
> #! C:\Perl\bin\perl.exe
use warnings;
use strict;
> print "hello\n";
>=20
> print "Please Enter Input File name(Give the complete path):";
> $infile=3D<STDIN>; #give the input file name here
> chomp($infile);
> open ($in, "<", $infile) or die "Cannot open file for reading\n";
You should include the $! variable in the error message so you know=20
*why* open failed.
> #Check whether the file can be opened for reading
>=20
> while (<$in>)
> {
> if(/,/) {print "before match: $`\t and after match: $'\n\n";};
> $x=3D$';
> $y=3D$`;
perldoc perlvar
[ SNIP ]
$PREMATCH
$=91 The string preceding whatever was matched by the last
successful pattern match (not counting any matches hidden
within a BLOCK or eval enclosed by the current BLOCK).
(Mnemonic: "=91" often precedes a quoted string.) This
variable is read-only.
The use of this variable anywhere in a program imposes a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
considerable performance penalty on all regular expression
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
matches. See "BUGS".
^^^^^^^^^^^^^^^^^^^^^
$POSTMATCH
$=92 The string following whatever was matched by the last
successful pattern match (not counting any matches hidden
within a BLOCK or eval() enclosed by the current BLOCK).
(Mnemonic: "=92" often follows a quoted string.) Example:
local $_ =3D =92abcdefghi=92;
/def/;
print "$=91:$&:$=92\n"; # prints abc:def:ghi
This variable is read-only and dynamically scoped to the
current BLOCK.
The use of this variable anywhere in a program imposes a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
considerable performance penalty on all regular expression
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
matches. See "BUGS".
^^^^^^^^^^^^^^^^^^^^^
> &mysubroutine($x,$y);
> }
>=20
> sub mysubroutine
> {
You are calling mysubroutine with the arguments $x and $y so the=20
contents of those two variables will be in the @_ array.
> $a=3D$x;
> $b=3D$y;
So why don't you get the contents of $a and $b from @_?
perldoc perlsub
> print "Please Enter the Output File name (Give the compelte path):";
> $infile=3D<STDIN>; #give the output file name here
> chomp($infile);
> open ($in, "<", $infile) or die "cannot open file to write\n";
^^^
You are using the same filehandle that you used to open the other file=20
which means that the other file is now closed.
> #Check whether the file can be opened for writing
>=20
> print "$b\n";
> while (<$in>)
> {
> print "pattern to find: $y\n";
> if (/$b/) {print "Cheers@\n";};
perldoc -q "How do I match a pattern that is supplied by the user"
> }
> close $in or die "cannot close $out\n";
^^^ ^^^^
> }
>=20
>=20
> close $in or die "Cannot close $in\n";
^^^
Printing a filehandle in a string will not yield much useful information.=
John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Thu, 13 Mar 2008 07:25:15 -0700 (PDT)
From: Idgarad <idgarad@gmail.com>
Subject: Re: Hell of a time extracting bits from a vector
Message-Id: <366000c3-9a0c-4425-a975-f0ae66c8aef3@2g2000hsn.googlegroups.com>
On Feb 29, 1:53=A0pm, "John W. Krahn" <some...@example.com> wrote:
> Idgarad wrote:
> > I am generating a SHA1 digest that I want to use for some values.
>
> > I want to take the digest that is generated at extact a given number
> > of bits, in sequence.
>
> > SHA1 generates 160 bits.
>
> > I would like to partition that 160 bits into an array storing the
> > value of those bits
>
> use Digest::SHA1 =A0qw(sha1);
>
> unpack '(a)*', unpack 'B*', sha1( $data );
>
> > for instance (in short form using only 10 bits grabbing 2 at a time)
> > lets say I have:
>
> > 1010010101
>
> > and I am grabbing pairs I need (from least to most):
>
> > @somearray
>
> > $somearray[0] =3D 1 (01)
> > $somearray[1] =3D 1 (01)
> > $somearray[2] =3D 1 (01)
> > $somearray[3] =3D 2 (10)
> > $somearray[4] =3D 2 (10)
>
> my @somearray =3D unpack '(a2)*', unpack 'B*', sha1( $data );
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- =
Larry Wall
Exactly what I was looking for with one exception, I don't want to
store the ASCII in the array but rather the actual integer value.
my @somearray =3D unpack '(a2)*', unpack 'B*', sha1( $data );
Works perfect but I tried changing the a2 to N or n but that fails
miserable. I figure I have to append an bin to int of some sort to the
front of the unpacks but so far I have been unsuccessful. Any
suggestions?
------------------------------
Date: Thu, 13 Mar 2008 14:40:38 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Hell of a time extracting bits from a vector
Message-Id: <60rna5-h221.ln1@osiris.mauzo.dyndns.org>
Quoth Idgarad <idgarad@gmail.com>:
>
> Exactly what I was looking for with one exception, I don't want to
> store the ASCII in the array but rather the actual integer value.
>
> my @somearray = unpack '(a2)*', unpack 'B*', sha1( $data );
>
> Works perfect but I tried changing the a2 to N or n but that fails
> miserable. I figure I have to append an bin to int of some sort to the
> front of the unpacks but so far I have been unsuccessful. Any
> suggestions?
The obvious way is to add
map { oct "0b$_" }
to the front; there may be a cleverer way with pack, but if you don't
need speed that's nice and simple.
Ben
------------------------------
Date: Wed, 12 Mar 2008 20:33:12 -0700 (PDT)
From: paul <nechtom@gmail.com>
Subject: Re: Inverted RegEx on list of numbers
Message-Id: <b8b44111-fc1d-4b28-b699-cc0de818acd6@k13g2000hse.googlegroups.com>
On Mar 8, 7:42=A0pm, Damian Lukowski <dam...@tvk.rwth-aachen.de> wrote:
> Well, okay.
>
> The rough approach to invert a regular expression is this:
>
> - Convert the regex to an equivalent epsilon-NFA.
> - Eliminate epsilon transitions.
> - Convert NFA to equivalent DFA.
> - Invert final state(s) to nonfinal state(s) and vice versa.
> - Convert the inverted DFA back into a regular expression.
>
> Above, I forgot the fourth step and converted a DFA into a regular
> expression without inverting any states. Thus, the former /two/ should
> be equivalent to the bloated one.
>
> Damian
Hello.
Is there any library or tool which can do it for us.
------------------------------
Date: Thu, 13 Mar 2008 03:40:24 +0100
From: Frank Seitz <devnull4711@web.de>
Subject: Re: Matching multiple subexpressions in a regular expression
Message-Id: <63rigpF28brs2U4@mid.individual.net>
ShaunJ wrote:
> If I structure my program as in the example, using many small regex
> instead of one big regex, Perl 5.8.6 runs out of memory and dies:
> vm_allocate failed, Out of memory! I have 400'000 regex of exactly 27
> characters each, and the input string is one line 100 kB long.
[...]
> my @restrings = <REFILE>;
> my @re = map { qr/$_/x } @restrings;
400'000 precompiled regexes are quite a lot!
Why don't you read and create them in chunks of, say, 1000?
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
------------------------------
Date: Thu, 13 Mar 2008 04:29:42 +0100
From: Frank Seitz <devnull4711@web.de>
Subject: Re: Matching multiple subexpressions in a regular expression
Message-Id: <63rld7F28brs2U6@mid.individual.net>
ShaunJ wrote:
>
> If I structure my program as in the example, using many small regex
> instead of one big regex, Perl 5.8.6 runs out of memory and dies:
> vm_allocate failed, Out of memory! I have 400'000 regex of exactly 27
> characters each, and the input string is one line 100 kB long.
400'000 precompiled regexes are quite a lot!
Why do you compile them all at once?
Maybe this is a more suitable program structure (pseudo code):
foreach regex <- read and compile next regex
foreach string <- next input string
find matches in string
order output
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
------------------------------
Date: Thu, 13 Mar 2008 09:30:26 -0500
From: Ignoramus17007 <ignoramus17007@NOSPAM.17007.invalid>
Subject: My editfile.pl script
Message-Id: <VL6dnepSxIwfp0TanZ2dnUVZ_rmjnZ2d@giganews.com>
I wrote a script to edit files from command line. You can either
replace one line with another, add a line after or before a given
line. When matching lines, it trims all spaces to at most one
consecutive space for ease of matching. (ie, a line
/dev/sda2 /data3
would match
/dev/sda2 /data3
The point of this script is to ease remote system administration or
administration of a number of machines from cron/CVS.
Attached is the script as well as a self test shell script that tests
this script.
######################################################################
#!/usr/bin/perl
#
# Copyright (C) Igor Chudov, 2008.
# Released to the public under the GNU Public License V3.
#
use strict;
use warnings;
use Getopt::Long;
use POSIX;
my $before = undef;
my $after = undef;
my $atend = undef;
my $replace = undef;
my $text = undef;
my $line = undef;
my $usage ="USAGE: $0 [--after after_line|--atend|--replace replace_line] --line line {files...}";
die $usage unless
GetOptions(
"before=s" => \$before,
"after=s" => \$after,
"atend!" => \$atend,
"replace=s" => \$replace,
"line=s" => \$line,
);
my @files = @ARGV;
sub Cleanup {
my $x = shift;
return $x unless $x;
# $L and $LINE are used for comparisons to take
# spaces and tabs into account, like diff -w
$x =~ s/^\s+//;
$x =~ s/\s+$//;
$x =~ s/\s+/ /g;
return $x;
}
################################################## Syntax check
my $count = 0;
$count++ if $before;
$count++ if $after;
$count++ if $atend;
$count++ if $replace;
die $usage unless $count == 1;
die $usage unless $line;
die $usage unless @files;
my $BEFORE = Cleanup( $before );
my $AFTER = Cleanup( $after );
my $REPLACE = Cleanup( $replace );
my $LINE = Cleanup( $line );
############################## Now we have good command line arguments.
foreach my $file (@files) {
my $good = undef;
open( IN, $file );
open( OUT, ">$file.new" );
# Check if file has this line already!
my $has = undef;
while( my $l = <IN> ) {
chomp $l;
my $L = $l;
$L =~ s/^\s+//;
$L =~ s/\s+$//;
$L =~ s/\s+/ /g;
if( $L eq $LINE ) {
$has = 1;
last;
}
}
unless( $has ) {
seek( IN, &POSIX::SEEK_SET, 0 );
while( my $l = <IN> ) {
chomp $l;
my $L = $l;
$L =~ s/^\s+//;
$L =~ s/\s+$//;
$L =~ s/\s+/ /g;
if( $before ) {
#print "Before: '$L' ? '$before'\n";
if( $L eq $BEFORE ) {
print OUT "$line\n";
$good = 1;
}
print OUT "$l\n";
} elsif( $after ) {
#print "After? '$L' ? '$after'\n";
print OUT "$l\n";
if( $L eq $AFTER ) {
print OUT "$line\n";
$good = 1;
}
} elsif( $replace ) {
if( $L eq $REPLACE ) {
print OUT "$line\n";
$good = 1;
} else {
print OUT "$l\n";
}
} else {
print OUT "$l\n";
}
}
if( $atend ) {
print OUT "$line\n";
$good = 1;
}
}
close( IN );
close( OUT );
if( $good ) {
rename( "$file.new", $file );
} else {
unlink( "$file.new" );
}
unless( $good || $has ) {
die "$0 Failed to perform requested operation on $file.";
}
}
######################################################################
#!/bin/sh
FILE=/tmp/file.txt
Failed() {
echo FAILED: $@
exit 1
}
Test1() {
echo " line before" > $FILE
editfile.pl --after "line before" --line "after_line" $FILE
echo After Test1: `cat $FILE`
grep after_line $FILE
}
Test2() {
echo " line after" > $FILE
editfile.pl --before "line after " --line "inserted_before_line" $FILE
grep inserted_before_line $FILE
}
Test3() {
echo " line after" > $FILE
editfile.pl --before "line after " --line "inserted_before_line" $FILE
editfile.pl --before "line after " --line "inserted_before_line" $FILE
editfile.pl --before "line after " --line "inserted_before_line" $FILE
editfile.pl --before "line after " --line "inserted_before_line" $FILE
test `grep inserted_before_line $FILE|wc -l ` = 1
}
Test4() {
echo " line 2 replace" > $FILE
editfile.pl --replace "line 2 replace" --line " replacing_line " $FILE
test `grep replacing_line $FILE|wc -l ` = 1 \
&& test `grep 'line 2 replace' $FILE|wc -l ` = 0
}
Test5() {
echo " cannot_find_me " > $FILE
if editfile.pl --replace "look_for_me" --line " replacing_line " $FILE; then
return 1
else
return 0
fi
}
Test() {
FUNC=$1; shift
ARGS="$@"
if $FUNC $ARGS > /tmp/$$.txt; then
echo $FUNC succeeded.
else
echo $FUNC Failed... ':-('
cat /tmp/$$.txt
exit 1
fi
rm /tmp/$$.txt
}
Test Test1
Test Test2
Test Test3
Test Test4
Test Test5
------------------------------
Date: Thu, 13 Mar 2008 04:42:20 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Thu Mar 13 2008
Message-Id: <JxnJqK.23tE@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
Acme-Shukugawa-Atom-0.00001
http://search.cpan.org/~dmaki/Acme-Shukugawa-Atom-0.00001/
???????????????
----
Acme-Shukugawa-Atom-0.00002
http://search.cpan.org/~dmaki/Acme-Shukugawa-Atom-0.00002/
???????????????
----
Algorithm-LBFGS-0.171
http://search.cpan.org/~laye/Algorithm-LBFGS-0.171/
Perl extension for L-BFGS
----
BigIP-ParseConfig-1.1.7
http://search.cpan.org/~sschneid/BigIP-ParseConfig-1.1.7/
F5/BigIP configuration parser
----
CGI-Session-Serialize-yaml-4.21
http://search.cpan.org/~rsavage/CGI-Session-Serialize-yaml-4.21/
serializer for CGI::Session
----
CHI-0.07
http://search.cpan.org/~jswartz/CHI-0.07/
Unified cache interface
----
CPAN-1.92_58
http://search.cpan.org/~andk/CPAN-1.92_58/
query, download and build perl modules from CPAN sites
----
CPAN-LinksToDocs-No404s-0.002
http://search.cpan.org/~zoffix/CPAN-LinksToDocs-No404s-0.002/
get links to http://search.cpan.org documentation by giving short "tags" and make sure all of them point to existant documentation
----
CPU-Emulator-Z80-0.9
http://search.cpan.org/~dcantrell/CPU-Emulator-Z80-0.9/
a Z80 emulator
----
Catalyst-Plugin-Assets-0.031
http://search.cpan.org/~rkrimen/Catalyst-Plugin-Assets-0.031/
Manage and minify .css and .js assets in a Catalyst application
----
Config-Model-TkUI-0.103
http://search.cpan.org/~ddumont/Config-Model-TkUI-0.103/
Perl/Tk widget to edit content of Config::Model
----
Continuity-0.98
http://search.cpan.org/~awwaiid/Continuity-0.98/
Abstract away statelessness of HTTP, for stateful Web applications
----
DBIx-Class-Schema-Loader-0.04999_03
http://search.cpan.org/~ilmari/DBIx-Class-Schema-Loader-0.04999_03/
Dynamic definition of a DBIx::Class::Schema
----
DBIx-Class-Schema-Loader-0.04999_04
http://search.cpan.org/~ilmari/DBIx-Class-Schema-Loader-0.04999_04/
Dynamic definition of a DBIx::Class::Schema
----
Date-MonthSet-0.2
http://search.cpan.org/~diz/Date-MonthSet-0.2/
simple interface to a collection of months
----
Devel-CheckLib-0.5
http://search.cpan.org/~dcantrell/Devel-CheckLib-0.5/
check that a library is available
----
Devel-CheckOS-1.43
http://search.cpan.org/~dcantrell/Devel-CheckOS-1.43/
check what OS we're running on
----
Encode-2.24
http://search.cpan.org/~dankogai/Encode-2.24/
character encodings
----
ExtUtils-Command-1.14
http://search.cpan.org/~rkobes/ExtUtils-Command-1.14/
utilities to replace common UNIX commands in Makefiles etc.
----
File-Assets-0.051
http://search.cpan.org/~rkrimen/File-Assets-0.051/
Manage .css and .js assets in a web application
----
File-Binary-1.5
http://search.cpan.org/~simonw/File-Binary-1.5/
Binary file reading module
----
Fukurama-Class-0.027
http://search.cpan.org/~tobiwan/Fukurama-Class-0.027/
Pragma to extend the Perl-OO (in native Perl)
----
Fukurama-Class-0.028
http://search.cpan.org/~tobiwan/Fukurama-Class-0.028/
Pragma to extend the Perl-OO (in native Perl)
----
Games-Hack-Patch-x86_64-0.13
http://search.cpan.org/~pmarek/Games-Hack-Patch-x86_64-0.13/
----
Ganglia-Gmetric-0.3
http://search.cpan.org/~fungus/Ganglia-Gmetric-0.3/
perl gmetric wrapper
----
Gtk2-Ex-Geo-0.60
http://search.cpan.org/~ajolma/Gtk2-Ex-Geo-0.60/
The main module to use for geospatial applications
----
HTTP-Server-Simple-Dispatched-0.01
http://search.cpan.org/~frodwith/HTTP-Server-Simple-Dispatched-0.01/
Django-like regex dispatching with request and response objects - no CGI.pm cruft!
----
Ham-APRS-FAP-1.10
http://search.cpan.org/~hessu/Ham-APRS-FAP-1.10/
Finnish APRS Parser (Fabulous APRS Parser)
----
Ham-APRS-FAP-1.11
http://search.cpan.org/~hessu/Ham-APRS-FAP-1.11/
Finnish APRS Parser (Fabulous APRS Parser)
----
Hash-Type-1.08
http://search.cpan.org/~dami/Hash-Type-1.08/
pseudo-hashes as arrays tied to a "type" (list of fields)
----
Image-ExifTool-7.21
http://search.cpan.org/~exiftool/Image-ExifTool-7.21/
Read and write meta information
----
MIME-EncWords-1.002
http://search.cpan.org/~nezumi/MIME-EncWords-1.002/
deal with RFC 2047 encoded words (improved)
----
MP3-Podcast-0.06
http://search.cpan.org/~jmerelo/MP3-Podcast-0.06/
Perl extension for podcasting directories full of MP3 files
----
Makefile-Parser-0.209
http://search.cpan.org/~agent/Makefile-Parser-0.209/
A simple parser for Makefiles
----
Module-Pluggable-3.7
http://search.cpan.org/~simonw/Module-Pluggable-3.7/
automatically give your module the ability to have plugins
----
Net-Raccdoc-1.3
http://search.cpan.org/~minter/Net-Raccdoc-1.3/
Perl interface to the Raccdoc system
----
Net-Whois-Gateway-Client-0.06
http://search.cpan.org/~graykot/Net-Whois-Gateway-Client-0.06/
Interface to Net::Whois::Gateway::Server
----
Net-Whois-Gateway-Client-0.07
http://search.cpan.org/~graykot/Net-Whois-Gateway-Client-0.07/
Interface to Net::Whois::Gateway::Server
----
Net-Whois-Gateway-Server-0.06
http://search.cpan.org/~graykot/Net-Whois-Gateway-Server-0.06/
whois gateway
----
Net-Whois-Gateway-Server-0.07
http://search.cpan.org/~graykot/Net-Whois-Gateway-Server-0.07/
whois gateway
----
POE-Component-CPAN-LinksToDocs-No404s-0.001
http://search.cpan.org/~zoffix/POE-Component-CPAN-LinksToDocs-No404s-0.001/
non-blocking wrapper around CPAN::LinksToDocs::No404s
----
POE-Component-Client-Whois-Smart-0.11
http://search.cpan.org/~graykot/POE-Component-Client-Whois-Smart-0.11/
Provides very quick WHOIS queries with smart features.
----
POE-Component-Client-opentick-0.05
http://search.cpan.org/~infidel/POE-Component-Client-opentick-0.05/
A POE component for working with opentick.com's market data feeds.
----
POE-Component-IRC-Plugin-CPAN-LinksToDocs-0.001
http://search.cpan.org/~zoffix/POE-Component-IRC-Plugin-CPAN-LinksToDocs-0.001/
get links to http://search.cpan.org/ documentation from IRC
----
POE-Component-IRC-Plugin-CPAN-LinksToDocs-No404s-0.001
http://search.cpan.org/~zoffix/POE-Component-IRC-Plugin-CPAN-LinksToDocs-No404s-0.001/
link to http://search.cpan.org/ documentation from IRC (and check that all links lead to existing docs)
----
RDF-Server-0.05
http://search.cpan.org/~jsmith/RDF-Server-0.05/
toolkit for building RDF servers
----
Test-Server-0.02_02
http://search.cpan.org/~jkutej/Test-Server-0.02_02/
what about test driven administration?
----
TiVo-Calypso-1.3.5
http://search.cpan.org/~sschneid/TiVo-Calypso-1.3.5/
a Perl interface to the TiVo Calypso protocol
----
VCS-CMSynergy-1.33
http://search.cpan.org/~rschupp/VCS-CMSynergy-1.33/
Perl interface to Telelogic Synergy
----
WWW-Ohloh-API-0.0.9
http://search.cpan.org/~yanick/WWW-Ohloh-API-0.0.9/
Ohloh API implementation
----
WordPress-XMLRPC-1.10
http://search.cpan.org/~leocharre/WordPress-XMLRPC-1.10/
----
cpan_bot-0.05
http://search.cpan.org/~zoffix/cpan_bot-0.05/
an IRC CPAN Info bot
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Thu, 13 Mar 2008 03:07:00 -0700 (PDT)
From: joe <jcharth@gmail.com>
Subject: Re: perl html template error
Message-Id: <ad29e143-3968-4efc-88b8-15407dc94362@e6g2000prf.googlegroups.com>
Thanks, Makes sense. I was going over the tutorial and the template
did not have HEADINGS anywhere.
On Mar 11, 11:09 am, "J. Gleixner" <glex_no-s...@qwest-spam-
no.invalid> wrote:
> joe wrote:
> > Hello, I am lerning the html template module and I keep get an error
> > on this line
>
> > $template->param(HEADINGS=>\@headings);
>
> > HTML::Template->output() : fatal error in loop output :
> > HTML::Template : Attempt to set nonexistent parameter 'heading' - this
> > parameter name doesn't match any declarations in the template file :
> > (die_on_bad_params => 1) at /usr/lib/perl5/site_perl/5.8.0/HTML/
> > Template.pm line 2997
> > at ./one.cgi line 60
>
> > It looks like HEADINGS is not part of html::template any ideas on
> > which module has setting? or is it obsolete?
>
> HTML::Template
>
> Why do you think there is a default variable of HEADINGS?
>
> You have to defined it in your template.
>
> The error is saying that you have a template variable 'heading',
> that you're not setting. You can either set it to '', or read
> the documentation to learn how to use 'die_on_bad_params'.
------------------------------
Date: Thu, 13 Mar 2008 01:41:37 -0700 (PDT)
From: Bart Van der Donck <bart@nijlen.com>
Subject: Which split delimiter to use for mailbox
Message-Id: <1add5f7d-2154-47a2-a0d5-e04f57a795a4@n36g2000hse.googlegroups.com>
Hello,
I'm reading an email inbox file in Perl like /usr/boxes/mydomain.com/
info. This file consists of 1 or more mails that are waiting to be
delivered. Which mechanism is safe enough to split this file in order
to know the number of waiting emails ?
The format looks like this:
From jeff@domain.com Wed Mar 12 19:14:17 2008
[headers]
[blanc line]
[body]
[blanc line]
From john@domain.com Wed Mar 12 19:50:11 2008
[headers]
[blanc line]
[body]
[blanc line]
From kevin@domain.com Wed Mar 12 21:47:07 2008
[headers]
[blanc line]
[body]
I don't understand how Perl can split this input reliably, so that it
knows where a next mail starts. I mean, every message could also
contain blanc lines or "From xx@xx.xx" itself.
Thanks,
--
Bart
------------------------------
Date: Thu, 13 Mar 2008 14:48:39 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Which split delimiter to use for mailbox
Message-Id: <pan.2008.03.13.13.48.37@rtij.nl.invlalid>
On Thu, 13 Mar 2008 01:41:37 -0700, Bart Van der Donck wrote:
> Hello,
>
> I'm reading an email inbox file in Perl like /usr/boxes/mydomain.com/
> info. This file consists of 1 or more mails that are waiting to be
> delivered. Which mechanism is safe enough to split this file in order to
> know the number of waiting emails ?
>
> The format looks like this:
>
> From jeff@domain.com Wed Mar 12 19:14:17 2008 [headers]
> [blanc line]
> [body]
> [blanc line]
> From john@domain.com Wed Mar 12 19:50:11 2008 [headers]
> [blanc line]
> [body]
> [blanc line]
> From kevin@domain.com Wed Mar 12 21:47:07 2008 [headers]
> [blanc line]
> [body]
>
> I don't understand how Perl can split this input reliably, so that it
> knows where a next mail starts. I mean, every message could also contain
> blanc lines or "From xx@xx.xx" itself.
The delimiter is '\n\nFrom'. It it occurs in the body it should be
escaped (by putting a '>' before the 'From' IIRC).
HTH,
M4
------------------------------
Date: Thu, 13 Mar 2008 07:13:00 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Which split delimiter to use for mailbox
Message-Id: <f84701de-e9b7-43e3-a4eb-2ef9e3ea96a4@n58g2000hsf.googlegroups.com>
On Mar 13, 4:41 am, Bart Van der Donck <b...@nijlen.com> wrote:
> Hello,
>
> I'm reading an email inbox file in Perl like /usr/boxes/mydomain.com/
> info. This file consists of 1 or more mails that are waiting to be
> delivered. Which mechanism is safe enough to split this file in order
> to know the number of waiting emails ?
>
> The format looks like this:
>
> From j...@domain.com Wed Mar 12 19:14:17 2008
> [headers]
> [blanc line]
> [body]
> [blanc line]
> From j...@domain.com Wed Mar 12 19:50:11 2008
> [headers]
> [blanc line]
> [body]
> [blanc line]
> From ke...@domain.com Wed Mar 12 21:47:07 2008
> [headers]
> [blanc line]
> [body]
>
> I don't understand how Perl can split this input reliably, so that it
> knows where a next mail starts. I mean, every message could also
> contain blanc lines or "From x...@xx.xx" itself.
>
> Thanks,
>
> --
> Bart
Count the '^Froms'. That will give you an accurate count of the number
of separate messages and you don't need a delimiter.
CC
------------------------------
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 V11 Issue 1358
***************************************