[23625] in Perl-Users-Digest
Perl-Users Digest, Issue: 5832 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 19 21:05:44 2003
Date: Wed, 19 Nov 2003 18:05:09 -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 Wed, 19 Nov 2003 Volume: 10 Number: 5832
Today's topics:
Re: bit sequence match (Jay Tilton)
Re: Excel question. (Jay Tilton)
god im a noob Default@User011011101101.net
Re: god im a noob (Sam Holden)
Re: god im a noob <mgjv@tradingpost.com.au>
Re: god im a noob Default@User011011101101.net
Re: god im a noob Default@User011011101101.net
Re: god im a noob Default@User.xyz
Re: god im a noob <mikeflan@earthlink.net>
NET::SSH:Win32Perl on Win2K (Anand Ramamurthy)
Re: PERL array of arrays <tore@aursand.no>
Re: Project Organization <REMOVEsdnCAPS@comcast.net>
Re: Project Organization (James Willmore)
Re: Project Organization (James Willmore)
Re: Protecting Source code of a perl script <REMOVEsdnCAPS@comcast.net>
Re: RegEx problem (Jay Tilton)
Re: RegEx problem <REMOVEsdnCAPS@comcast.net>
test please ignore Default@User011011101101.xyz
trying to understand fork and wait (John)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 19 Nov 2003 23:39:27 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: bit sequence match
Message-Id: <3fbbfd05.181269253@news.erols.com>
Edo <eddhig22@yahoo.com> wrote:
: what I wanted to do is sort the hash at the time or once it's been created.
The Tie::IxHash module is a convenient way to have this.
See "How can I always keep my hash sorted?" in perlfaq4.
: ok.. let me have a go
[...]
: my %match = map { $_ => $h2{$_} } @k2[ $-[0] .. $-[0]+@k1-1 ];
: my %sorted_match = sort {$a <=> $b} keys %match;
[...]
The list assigned to %sorted_match contains only the keys to %match.
Since the list of keys is all that is fed into sort(), that's all that
comes out. To get a list of key/value pairs to assign to %sorted_match,
my %sorted_match =
map {$_ => $match{$_}}
sort {$a <=> $b}
keys %match;
But an ordinary hash doesn't preserve the order of its elements. If a
hash was able to preserve its order, the %match one would already be
sorted. Re-sorting it into another offers no benefit.
: Also, I need to get the values of sorted %h1 in array
: my @v1 = sort {$a <=> $b} keys %h1; no this is not right, I need the
: values and not the keys in @v1 but I need the values according to sorted
: keys of %h1.
Once you have the keys sorted as you want, getting the values for those
keys is one step away.
It can be done with map,
my @v1 = map $h1{$_}, sort {$a <=> $b} keys %h1;
or it can be done with a hash slice,
my @v1 = @h1{ sort {$a <=> $b} keys %h1 };
: and an equal number of values starting form 1st to number of items in
: @v1 from sorted %h2 in another array @v2
There's already the %match hash. Getting the values from it would be
just like getting the values from %h1.
: and once I have both array, I feed them in a sub which I have that will
: return a number.
: if ( sim (@v1, @v2) > .5 ) {
: again create @AoH and print it like the original code in past post. man
: I hope I am explaining this right.
: }
A subroutine call flattens its arguments into one list. If it is called
like "sim(@v1, @v2)" then there is no way for the subroutine to tell
what values belong in @v1 and which belong in @v2.
The solution to this problem is to pass references to @v1 and @v2 to the
subroutine. See "Pass by Reference" in perlsub for a discussion of how
that works.
There isn't enough information to know what that sim() subroutine might
do and what number it might return.
: just explaining it in english is hard, let alone doing it in perl. I
: which perl was my second language.
You're doing fine. I, being most probably completely ignorant of your
native tongue, must admire your success.
------------------------------
Date: Wed, 19 Nov 2003 23:29:15 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Excel question.
Message-Id: <3fbbfc2d.181052403@news.erols.com>
"Richard S Beckett" <spikeywan@bigfoot.com.delete.this.bit> wrote:
: I want to be able to work out how to do things in excel by myself, but I
: don't know how to convert between the macro syntax, and that required in
: perl modules.
:
: The 3 things I want to do currently are:
: 1. Make columns Autofit.
: 2. Merge cells.
: 3. Freeze panes.
:
: To this end, I recorded a macro. It looks like this...
:
: Sub Macro1()
[snip]
: End Sub
:
: How do I get from this to the commands I need to use in either
: Spreadsheet::WriteExcel, or Win32::OLE?
I would apply the laziness virtue. Leave the macro untouched and tell
Excel to run it itself, rather than bother rewriting it in Perl.
my $excel =
Win32::OLE->GetActiveObject('Excel.Application')
||
Win32::OLE->new('Excel.Application', 'Quit');
my $macros = $excel->Workbooks
->Open('MyMacros.xls'); # wherever Macro1 lives
# Insert steps to make the desired workbook/worksheet active
$excel->Run('Macro1');
------------------------------
Date: Wed, 19 Nov 2003 23:12:45 GMT
From: Default@User011011101101.net
Subject: god im a noob
Message-Id: <NNSub.37254$hB5.4118@nwrdny02.gnilink.net>
please help...
i've just read the perllol and perlreftut perldocs...
and two things are really bugging me.
first why dos the foreach go beyond the curly barckets?
second why cant i assign a has containing ref's to a dbm hash?
heres the lame noob code :-(
#!
use strict;
use warnings;
my $database = "Numbers and Colors";
verify_data_entry();
#Subroutines#
sub data_entry
{
print "\nEnter a number: ";
chomp (my $number = <STDIN>); $number =~ s/\W.*//;
print "\nEnter some colors (ctrl-z when finished):\n";
chomp (my @colors_in = <STDIN>);
foreach my $item (@colors_in)
{$item =~ s/\W.*//; $item = ucfirst (lc($item));}
my $aref_1 = [@colors_in];
my %numbers_colors = ($number, $aref_1);
verify_data_entry();
print "\n$database has been updated with the following entries.\n";
foreach my $key (sort keys %numbers_colors)
{
my @colors_out1 = @{$numbers_colors{$key}};
my $colors_out2 = join ', ', sort @colors_out1;
print "$key\t$colors_out2\n";
}
print "\nWhy does this print between each key?\n"; # why ?!
#Save data to dbm hash#
#dbmopen (my %DATAFILE, "$database", 0666) ||
# die "Can't open the".' "'."$database".'" database.' . "$!";
#%DATAFILE = %numbers_colors; #Does this work?
#print "\nView the entire".'"'."$database".'" database? (y or n): ';
#chomp (my $verify = lc<STDIN>);
#if ($verify eq "y")
#{
# foreach my $key (sort keys %DATAFILE) #Error here.
# {
# my @colors_out1 = @{$DATAFILE{$key}};
# my $colors_out2 = join ', ', sort @colors_out1;
# print "$key\t$colors_out2\n";
# }
#}
#dbmclose (%DATAFILE);
}
sub verify_data_entry
{
print "\nEnter data into $database database? (y or n): ";
chomp (my $verify = lc<STDIN>);
data_entry() if ($verify eq "y");
}
------------------------------
Date: 19 Nov 2003 23:38:20 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: god im a noob
Message-Id: <slrnbrnvnc.v7h.sholden@flexal.cs.usyd.edu.au>
On Wed, 19 Nov 2003 23:12:45 GMT,
Default@User011011101101.net <Default@User011011101101.net> wrote:
Do you own the domain User011011101101.net? Has the owner given
you permission to use one of their email addresses? Have you
checked whether doing so without permission is legal in whatever
jurisdiction you are in?
[yes I know the answers, since the domain is unregistered and hence you
haven't got rights to that domain]...
If you want an invalid email address stick .invalid on the end, or
use example.(com|net|org).
>
> first why dos the foreach go beyond the curly barckets?
It doesn't.
> sub data_entry
> {
> print "\nEnter a number: ";
> chomp (my $number = <STDIN>); $number =~ s/\W.*//;
> print "\nEnter some colors (ctrl-z when finished):\n";
> chomp (my @colors_in = <STDIN>);
> foreach my $item (@colors_in)
> {$item =~ s/\W.*//; $item = ucfirst (lc($item));}
> my $aref_1 = [@colors_in];
> my %numbers_colors = ($number, $aref_1);
> verify_data_entry();
> print "\n$database has been updated with the following entries.\n";
> foreach my $key (sort keys %numbers_colors)
> {
> my @colors_out1 = @{$numbers_colors{$key}};
> my $colors_out2 = join ', ', sort @colors_out1;
> print "$key\t$colors_out2\n";
> }
> print "\nWhy does this print between each key?\n"; # why ?!
Note that %number_colors is a "my" variable and hence a seperate instance
is created for each call of this function.
You call verify_data_entry before you loop which ends up recursively
calling this function again - which works on a completely seperate
%numbers_colors hash.
You only even inserted a single key-value pair into the %numbers_colors
hash, and hence that single instance is printed out and then the
"between each key" line. Then the function returns, and the previous
call prints out its version of the data.
This is visible in the fact the output is not sorted by key but simply
output in reverse order (due to the recusrive call before the output is
done).
--
Sam Holden
------------------------------
Date: 19 Nov 2003 23:47:52 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: god im a noob
Message-Id: <slrnbro09a.q43.mgjv@verbruggen.comdyn.com.au>
On Wed, 19 Nov 2003 23:12:45 GMT,
Default@User011011101101.net <Default@User011011101101.net> wrote:
> please help...
> i've just read the perllol and perlreftut perldocs...
> and two things are really bugging me.
>
> first why dos the foreach go beyond the curly barckets?
What exactly do you mean by that? The loop variable of a foreach is
localised to the block.
> second why cant i assign a has containing ref's to a dbm hash?
References are internal "pointers" to other data structures inside of
perl, i.e. in memory. A DBM hash is stored on disk. How do you propose
that reference would be stored on disk so that on the next run of perl
it would map to the correct locations in perls data storage area?
There are modules available that help you with storing complex data
structures on disk. The MLDBM modules provide a tied hash (DBM-like)
environment that deals with complex data structures. I suggest you
have a look at those.
> use strict;
> use warnings;
> my $database = "Numbers and Colors";
> verify_data_entry();
>
> #Subroutines#
> sub data_entry
> {
> print "\nEnter a number: ";
> chomp (my $number = <STDIN>); $number =~ s/\W.*//;
> print "\nEnter some colors (ctrl-z when finished):\n";
> chomp (my @colors_in = <STDIN>);
> foreach my $item (@colors_in)
> {$item =~ s/\W.*//; $item = ucfirst (lc($item));}
> my $aref_1 = [@colors_in];
> my %numbers_colors = ($number, $aref_1);
Each invocation of this subroutine has its own %numbers_colors
hash.
> verify_data_entry();
verify_data_entry calls this subroutine, which calls
verify_data_entry, which calls data_entry, which calls
verify_data_entry, which calls....
When you enter something that isn't 'y', the callstack unwinds, and
the second half of this subroutine is executed for each original
invocation.
> print "\n$database has been updated with the following entries.\n";
> foreach my $key (sort keys %numbers_colors)
For each invocation, the "private" %numbers_colors hash will have
exactly one entry, which is printed out.
> {
> my @colors_out1 = @{$numbers_colors{$key}};
> my $colors_out2 = join ', ', sort @colors_out1;
> print "$key\t$colors_out2\n";
> }
> print "\nWhy does this print between each key?\n"; # why ?!
Then this is printed out, and you exit from _this_ particular
invocation of the subroutine. falling back to the next one.
In other words, there is nothing wrong with foreach, but with your
program logic. Do something like this (pseudo code)
my %colours;
while (1)
{
last if not read_user_confirmation_y_n;
read_single_user_datum into %colours
}
save_hash
foreach key in %colours
print the key and data
Martien
--
|
Martien Verbruggen |
Trading Post Australia | Hi, Dave here, what's the root password?
|
------------------------------
Date: Wed, 19 Nov 2003 23:58:18 GMT
From: Default@User011011101101.net
Subject: Re: god im a noob
Message-Id: <usTub.499$vz.425@nwrdny01.gnilink.net>
shrug.. i care about the domain
ive been on the internet since 1981 never cared before
anyway.. so your saying to ditch the use strict?
------------------------------
Date: Thu, 20 Nov 2003 00:17:33 GMT
From: Default@User011011101101.net
Subject: Re: god im a noob
Message-Id: <xKTub.559$vz.512@nwrdny01.gnilink.net>
Just curious...
Does anyone ever run the code before answereing?
Here is the output...
C:\Documents and Settings\Administrator\Desktop>perllol
Enter data into Numbers and Colors database? (y or n): y
Enter a number: 1
Enter some colors (ctrl-z when finished):
red
green
blue
^Z
Enter data into Numbers and Colors database? (y or n): y
Enter a number: 2
Enter some colors (ctrl-z when finished):
cyan
magenta
yellow
black
^Z
Enter data into Numbers and Colors database? (y or n): n
Numbers and Colors has been updated with the following entries.
2 Black, Cyan, Magenta, Yellow
Why does this print between each key?
Numbers and Colors has been updated with the following entries.
1 Blue, Green, Red
Why does this print between each key?
C:\Documents and Settings\Administrator\Desktop>
------------------------------
Date: Thu, 20 Nov 2003 01:34:37 GMT
From: Default@User.xyz
Subject: Re: god im a noob
Message-Id: <NSUub.37454$hB5.373@nwrdny02.gnilink.net>
curious; does anyone ever try the programs listed?
here is the output...
C:\Documents and Settings\Administrator\Desktop>perllol
Enter data into Numbers and Colors database? (y or n): y
Enter a number: 1
Enter some colors (ctrl-z when finished):
red
green
blue
^Z
Enter data into Numbers and Colors database? (y or n): y
Enter a number: 2
Enter some colors (ctrl-z when finished):
cyan
magenta
yellow
black
^Z
Enter data into Numbers and Colors database? (y or n): n
Numbers and Colors has been updated with the following entries.
2 Black, Cyan, Magenta, Yellow
Why does this print between each key?
Numbers and Colors has been updated with the following entries.
1 Blue, Green, Red
Why does this print between each key?
C:\Documents and Settings\Administrator\Desktop>
------------------------------
Date: Thu, 20 Nov 2003 01:57:19 GMT
From: Mike Flannigan <mikeflan@earthlink.net>
Subject: Re: god im a noob
Message-Id: <3FBC203C.402B1FDE@earthlink.net>
Default@User011011101101.net wrote:
> snip
Please put the main topic of your post in the Subject field.
------------------------------
Date: 19 Nov 2003 16:22:34 -0800
From: anand_ramamurthy@yahoo.com (Anand Ramamurthy)
Subject: NET::SSH:Win32Perl on Win2K
Message-Id: <761041e6.0311191622.4d2f423a@posting.google.com>
If you have any problems using NET::SSH:Win32Perl please
do the following:
1. Install a fresh copy of ActivePerl 5.6.1
2. Install Net::SSH::W32Perl with the following command:
(newinstalllocation)\bin\ppm2.bat
install --location=http://www.soulcage.net/ppds Net-SSH-W32Perl
3. And re-run your script.
This worked for me and copuple other people.
------------------------------
Date: Thu, 20 Nov 2003 02:48:31 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: PERL array of arrays
Message-Id: <pan.2003.11.19.15.19.54.865490@aursand.no>
On Tue, 18 Nov 2003 09:27:54 +1000, Schlick wrote:
> sub make_random_list {
> # Push random numbers onto an array reference
> my $ret = [];
> # I hate $_ - I don't use $i, but there
> # are no possible side-effects
> for my $i (0..9) {
> push @$ret, int(rand(101));
> }
> return $ret;
> }
Exactly _why_ do you declare $i in the code above? You never use it? You
for() could have been written without that distracting $i variable;
for ( 0..9 ) {
push( @$ret, int(rand(101)) );
}
--
Tore Aursand <tore@aursand.no>
------------------------------
Date: Wed, 19 Nov 2003 19:21:19 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Project Organization
Message-Id: <Xns9438CF37CEE23sdn.comcast@216.196.97.136>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
chance@austin.rr.com wrote in news:bpf4ip$msc$1@localhost.localdomain:
> So anybody out there have any advice on how to organize a
> big perl project?
...
> any better way to do this ?
I don't know if it's "better", but this is how I do it. I put a
"Localize.pm" file in the same directory with any program files that must
be run (scripts, CGI programs, whatever). The purpose of Localize.pm is
to populate a global variable, @INCLUDE, with whatever paths (usually
absolute) where library files for this application may be found. Then,
each perl program has the following six lines at the top:
#!/usr/bin/perl
use strict;
use FindBin; # So this pgm can find where it is installed.
use lib $FindBin::Bin; # So we can find Localize.pm
use Localize; # To set @INCLUDE
use lib @Localize::INCLUDE; # To find other library files.
Then if another library path needs to be added, you just update
Localize.pm.
If you potentially have more than one app installed in a given directory,
you may want to call the file MyAppName_Localize.pm or something, to make
it unique.
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP7wXQmPeouIeTNHoEQJmAwCghe2I4UNt7BdUfx4FbkhmIaQW0IYAoNc0
zuReqXFHF7poQDSx9r28FB6I
=3bD6
-----END PGP SIGNATURE-----
------------------------------
Date: 19 Nov 2003 17:22:00 -0800
From: jwillmore@myrealbox.com (James Willmore)
Subject: Re: Project Organization
Message-Id: <d61170e5.0311191722.587ff3cb@posting.google.com>
"Ben Liddicott" <ben.liddicott@comodogroup.com> wrote in message news:<bpg69s$vg6$1@kylie.comodogroup.com>...
<snip>
The posting guidelines are located here:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Plus, they are posted on an infrequent basis in this newsgroup.
Please read them.
If you have suggestions for this group, you can consult the guidelines
on how to offer them.
Jim
(jwillmore _at_ adelphia _dot_ net)
------------------------------
Date: 19 Nov 2003 17:30:25 -0800
From: jwillmore@myrealbox.com (James Willmore)
Subject: Re: Project Organization
Message-Id: <d61170e5.0311191730.20f6e31b@posting.google.com>
chance@austin.rr.com wrote in message news:<bpf4ip$msc$1@localhost.localdomain>...
> So anybody out there have any advice on how to organize a
> big perl project?
>
> Pretty much all I'm looking for is philosophical banter on
> merits of
>
> 1) setting PERLIB
>
> 2) setting PERL5LIB
>
> 3) perl -Idirs
>
> 4) 'use lib "../dir/dir";',
> followed my 'use module;' (which resides in '../dir/dir/')
>
> 5) something I don't know about
>
> I've got all my stuff checked in to a CVS repository,
> and I want multiple experimental implementations to
> be able to work at the same time, so absolute paths are
> ruled out.
>
> This feels like an instance where having Makefiles and
> associated cruft is actually an advantage. Maybe its just
> a mental block my part though.
>
> Right now, I'm using method (4), which feels like putting
> the the -I flag (to gcc) in a C file. just feels kinda
> icky and wrong.
>
> any better way to do this ?
I use a BEGIN block. If the project is supposed to be portable, this
may be the way to go. I can evaluate the OS in the block and (un)set
variables, libraries, etc. as required before the application gets
started.
You could take all common function/methods and place them into (a)
module(s). Then, you can build your scripts around the module(s).
This removes some of the issues associated with platform specific
functions and the need to (un)set a whole mess of variables,
libraries, etc.
You could do a combination of both a BEGIN block and modules.
Just some random thoughts ....
HTH
Jim
(jwillmore _at_ adelphia _dot_ net)
------------------------------
Date: Wed, 19 Nov 2003 17:43:53 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Protecting Source code of a perl script
Message-Id: <Xns9438BEB33AEC5sdn.comcast@216.196.97.136>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
carltonbrown@hotmail.com (Carlton Brown) wrote in
news:aa611a32.0311191437.43840614@posting.google.com:
> "Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote in message
> news:<Xns94384AEC3609Dsdn.comcast@216.196.97.136>..
>> [ snips }
>> What makes you think that hiding the source code will make your
>> application more secure? Typically, a dedicated cracker will figure
>> out what the program is doing and find a way around it anyhow, while
>> your program may not be reviewed for bugs or security holes by your
>> peers.
>
> "Hey world, come look at my source, I dare you to find a hole in my
> exhaustively checked and peer-reviewed code."
Isn't that pretty much the basis of the OpenBSD project? :-)
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP7wAbGPeouIeTNHoEQIdgwCg0D4bTLZUZGbrWbJZkz1UHIJ9/RcAn1qu
bYiIFR3ln2ShAqF3q75/iA3t
=cQcq
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 20 Nov 2003 00:09:03 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: RegEx problem
Message-Id: <3fbc003f.182095404@news.erols.com>
"Ben Liddicott" <ben@liddicott.com> wrote:
: \w matches a single word character [a-zA-Z0-9_],
\w matches far more characters than just those. Not all the world is
limited to the English alphabet.
: not a "word". You need =
: \w+ to match a word.
:
: Furthermore, you don't actually want a word character. DNS names often
: contain a dash, which won't be matched by \w.
:
: What you need is this:
:
: m/
: \,1107\= # a comma, then 1107=
: ([^,]+)\, # match everything up to the next comma.
: /x # x says I can put comments and whitespace in the regex.
You have needlessly escaped characters that are not regex
metacharacters.
: In this case, I suspect you would be just as well using split to chop it
: up by commas, and map to make a hashref of the fields.
:
: Something like this:
:
: #$sLine is the line
: %fields = map(sub{
: if($_ =~ m/\=/){
: return $_ =~ m/([^=]+)\=(.*)/;
: }
: }, split(',', $sLine));
^^^
^^^
A regex should look like a regex.
The anonymous subroutine in the map() is weird, unnecessary, and broken.
my %fields = map split(/=/, $_, 2), split(/,/ , $sLine);
------------------------------
Date: Wed, 19 Nov 2003 18:20:18 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: RegEx problem
Message-Id: <Xns9438C4DF92AD7sdn.comcast@216.196.97.136>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
hosshp@yahoo.com (Randy) wrote in
news:3d4770c2.0311191336.700549ef@posting.google.com:
> I am trying to take a string out of a line of a log. I have been able
> to identify the line, but when I try to get just the section I want,
> it keeps coming back blank. I was wondering if anyone could tell me
> where I am going wrong?
>
> Example of a line of the log file:
> 20031118,163237,1=1,11=69.6.26.10,21=kschonni@usd.edu,23=4670,1107=.exp
> resstone.com,22=USDSPAMALERT:Order Reliable Online Prescription
> Medication,52=M2003111816285500014,20=expresstoneA@mx01.expresstone.com
> ,100=Message from an email address marked as a
> spammer.,24=b.expresstoneA.0-274725f-1f61.usd.edu.-kschonni@mx01.expres
> stone.com,2=307,121=8
>
> 2=307 lets me know it is the line that I want, and, the part of it
> that I need to use is after 1107=. So in this example, it would be
> '.expresston.com'. I have been trying to get the whole part
> (1107=.expresston.com) but have only been getting back blank returns.
> Here is the expression I have been trying:
>
> $line =~ /1107=\.\w\.\w/;
> $temp = $1;
$1 only works if a) the regular expression succeeds, and b) the regex has
capturing parentheses in it. So:
$temp = $1 if $line =~ /1107=([^,]+)/;
Note I used [^,] which will efficiently match a length of non-comma
characters, which I presume is what you're looking for.
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBP7wI9mPeouIeTNHoEQJK+ACfch3SIRmEN2l/F2a66HFMSYJGSaUAnjat
cTfkYnFUpxSRfb5OefRK2pxF
=f5zA
-----END PGP SIGNATURE-----
------------------------------
Date: Thu, 20 Nov 2003 01:42:29 GMT
From: Default@User011011101101.xyz
Subject: test please ignore
Message-Id: <9_Uub.37471$hB5.6243@nwrdny02.gnilink.net>
posts not showing up.. is nntp usually slow like this?
------------------------------
Date: 19 Nov 2003 16:15:21 -0800
From: jguad98@hotmail.com (John)
Subject: trying to understand fork and wait
Message-Id: <a964da31.0311191615.3d7540f2@posting.google.com>
I attempted to post yesterday, browser crapped out without returning,
don't know if message got out alive ... therefore, my apologies if I
have double posted.
I would really appreciate some feedback on a Perl program I'm trying
to write ... I know what I want it to do, but I'm not certain I'm
going about it the right way.
[requirement]
monitor application logfiles (multiple) on unix server (singular) for
error messages, re-write the error messages to syslog >>as they
occur<<.
[background / challenges]
the application is stupid & closed, cannot be modified, so I am trying
to handle this with a Perl script. The target application has upwards
of 100 users that can be online concurrently. Each user session
writes a user log to an individual directory & logfile. Meaning,
upwards of 100 logfiles all being written to at the same time. There
is no defined start or stop time for any given user, so far no known
signal to indicate that user session has started. Only way (so far)
to tell that a user session is active is to check to see if the log
file has been created (they overwrite the same file every time a
session starts) and is being written to.
My plan (so far) is to have a single Perl script running while the
application is up, periodically scanning (via a simple loop & sleep)
to detect logfiles. Upon detection of active logfile, spawn a child
to do the reading & error message trapping. This so that I have one
program keeping track of what logs are a) present, b) active, and c)
being monitored; then for each log, a child program would sit on an
open filehandle reading it and comparing messages found to a known
list of errors, and when an error is detected send it to syslog via
the logger command.
I know that my design could potentially put a load on the box, but it
is a big & strong fellow ... I think he can take it. But I have never
used fork before, have seen no understandable sample scripts to draw
inspiration from, and even after spending several days reading
articles from clpmisc and clpmod, as well as the Perl Cookbook and
Programming Perl, I still do not understand how to use fork and wait
and related functions properly to accomplish my task.
Please note I would be accurately described as a novice programmer so
there may be some poorly written code. Also also note that some
desired functionality has not been written yet (main loop, sleeps & a
way to track the children) because I'm still trying to get the core
functionality designed properly.
So without further ado, here is my code presented for attack ... er, I
mean analysis and discussion:
#!/usr/local/bin/perl
#=================================================================#
#-----------------------------------------------------------------
# some global vars
#-----------------------------------------------------------------
$mypid=$$;
$basedir="/paf/rpas";
@domainlist=`ls $basedir | egrep '^(top|key)..$'`;
$patternfile="/path/to/patterns.list";
open(PATTERNS,$patternfile);
@patternlist=<PATTERNS>; # this is my store of error messages to
watch for
close(PATTERNS);
#-----------------------------------------------------------------
# scan the directories
#-----------------------------------------------------------------
foreach $domain (@domainlist) {
chomp($domain);
$userdirbase="$basedir/$domain/users";
opendir(USERS, "$userdirbase");
#-------------------------------------------------------------
# read the dir
#-------------------------------------------------------------
while ($file = readdir(USERS)) {
#---------------------------------------------------------
# only select files matching the username pattern
#---------------------------------------------------------
next unless $file =~ /^[a-z][a-z]\d\d\d\d\d$/;
#---------------------------------------------------------
# check if we have a user dir
#---------------------------------------------------------
$currentuserdir="$userdirbase/$file";
if (-d "$currentuserdir") {
#-----------------------------------------------------
# check if there is an applog in the user dir
#-----------------------------------------------------
$logfile="$currentuserdir/applog";
if ( -e "$logfile") {
#-------------------------------------------------
# is the log open or closed?
#-------------------------------------------------
$return=`grep 'known end of session message'
$logfile`;
#-------------------------------------------------
# if open, fire the monitor
#-------------------------------------------------
if (! $return) { # the grep failed, ergo the file is
active
#-------------------------------------------------
# line below is because everything I've read about
fork
# says I gotta wait on the child pid (?) or it will
# become a zombie which is bad. I don't understand
# what this does or how it works.
#-------------------------------------------------
$SIG{'CHLD'} = sub { wait(); };
#-------------------------------------------------
# now we fork if and only if we are the original
program
# we consider ourselves too young to have
grandchildren
#-------------------------------------------------
if ($$ == $mypid) {
$kidpid = fork() or die "cannot fork: $!";
} else {
#-------------------------------------------------
# are we in the child process?
#-------------------------------------------------
if ($kidpid == 0) {
#---------------------------------------------
# read the user log
#---------------------------------------------
open(LOG,"$file");
while ($line = <LOG>) {
close(LOG), exit if ($line =~ /known EOS
message/);
foreach $patt (@patternlist) {
if ($line =~ /$patt/) {
$now=localtime(time);
system("logger $now $domain $user $line");
}#end child's if pattern match
}#end child's foreach loop
}#end child's while loop
}# this is the end of the child block
}#end if mypid
}#end if $return
}#end if logfile exists
}#end if directory (go to top of parent while loop)
} #end parent while loop (go to top of parent foreach loop)
closedir(USERS);
}#end parent foreach loop
#-----------------------------------------------------------------
exit 0;
#== EOF ==========================================================#
The parts that I am worried about are the "wait" (the line was cribbed
in it's entirety from a clpmisc article) and the "fork" (based on
another clpmisc article, but modified to do what I think I want it to
do).
What is it about the wait that I don't get? Well ... I've seen
articles discussing using system() versus fork() and they tell me that
system() has a built-in wait ... and I know that I cannot use system()
because the calling script (parent?) will not continue
processing/looping until the system() function returns ... I do not
want to wait for a return ... I want to keep going in my loop.
Will "wait" make me wait for a return or not? I have read the
perldoc, manpage, Programming Perl, the Perl Cookbook and multiple
clpm articles, and it still is not clear to me what happens when I
invoke / call / issue the "wait" function. My whole script is
predicated upon the notion that I can keep looping and forking
children willy-nilly ... that is to say, "fire and forget" but also
keep an eye on the children insofar as I'd like to track how many I
have and what logs are being monitored.
Then with regard to the fork ... every example I've seen shows a block
for the child along with a block for the parent all under the fork
function, which was very confusing because I always thought that the
parent was the script which generated the child (i.e., OUTside the
fork, not INside), until I read a description saying that the child is
actually a copy of the entire script and apparently can be limited to
execute only certain lines by testing the pid in an 'if' block.
So, in my child image I know that I do not want it to ever execute a
'parental' instruction, so you can see above that I did not actually
code a 'parent' block under the fork function unlike every single
example I've seen so far. Why am I the only person to not have a
'parent' block? There must be a reason for that parent block, but I
cannot figure out what it is ... again, I have read the relative
perldocs, manpages, and Perl book entries. I just don't get it.
I have not actually run the above program mostly because I don't
understand how the wait & fork will work and I fear unintended
consequences. I'm planning on building a test case, but in the
meanwhile I would be quite grateful for any explanations as to things
I am doing right or wrong in my program, as well as any suggestions on
a better way to solve my problem.
regards,
John G.
------------------------------
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.
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 5832
***************************************