[19746] in Perl-Users-Digest
Perl-Users Digest, Issue: 1941 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Oct 16 18:10:34 2001
Date: Tue, 16 Oct 2001 15:10:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1003270210-v10-i1941@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Tue, 16 Oct 2001 Volume: 10 Number: 1941
Today's topics:
Re: putting $1..$9 into a replacement string such as $r <johni@pa.press.net>
Re: putting $1..$9 into a replacement string such as $r <joe+usenet@sunstarsys.com>
Re: putting $1..$9 into a replacement string such as $r (=?ISO-8859-1?Q?sh=F2wd=F6g?=)
Re: Rookie with a perl issue <stevea@wrq.com>
Searching and splitting.. (Drew)
Re: Searching and splitting.. <please@no.spam>
Re: Searching and splitting.. (Damian James)
Re: Searching and splitting.. <please@no.spam>
Re: Socket read/write select problem <sean.egan@eei.ericsson.se>
Re: Writing and reading encrypted string (password) (Logan Shaw)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 16 Oct 2001 16:21:19 +0100
From: John Imrie <johni@pa.press.net>
Subject: Re: putting $1..$9 into a replacement string such as $repl="=csinfo$1$2^$3"
Message-Id: <3BCC506F.7050303@pa.press.net>
shòwdög wrote:
> Bernard El-Hagin <bernard.el-hagin@lido-tech.net> wrote in message news:<slrn9sno35.85e.bernard.el-hagin@gdndev25.lido-tech>...
>
>> On 15 Oct 2001 14:44:07 -0700, shòwdög <showdog@my-deja.com> wrote:
>>
>>
>> [snip]
>>
>>
>>
>>> Another related question, when you read a line :
>>>
>>> while <FOO> {}
>>>
>>> is $_ a single quoted or double quoted variable? based on what i see
>>> it is neither, if that makes sense.
>>
>>
>> I don't understand this. The way $_ is interpreted depends
>> on how it's quoted.
>
>
> while <FOO> {
> print;
> }
>
> In that context?
>
> This is not important to me anyway. What I REALLY need is an answer
> to the first q I posted. Its got me and my development team
> stumped!!! I will post it again here...
>
> use warnings;
> use strict;
> my $repl='this is $1. hi there.';
> my $search='(foo)';
> my $strText="foo foo";
>
> $strText =~ s/$search/$repl/gi;
> # what I want the above line to evaluate to is:
> # $strText =~ s/(foo)/this is $1. hi there./gi;
>
> print $strText;
> __END__
> this is $1. hi there. this is $1. hi there.
>
> how do I get $strText to contain "foo" instead of $1?
$strText =~ s/$search/$repl/gie;
------------------------------
Date: 16 Oct 2001 12:22:40 -0400
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: putting $1..$9 into a replacement string such as $repl="=csinfo$1$2^$3"
Message-Id: <m3ofn7d06n.fsf@mumonkan.sunstarsys.com>
John Imrie <johni@pa.press.net> writes:
> > use warnings;
> > use strict;
> > my $repl='this is $1. hi there.';
> > my $search='(foo)';
> > my $strText="foo foo";
> >
> > $strText =~ s/$search/$repl/gi;
> > # what I want the above line to evaluate to is:
> > # $strText =~ s/(foo)/this is $1. hi there./gi;
> >
> > print $strText;
> > __END__
> > this is $1. hi there. this is $1. hi there.
> >
> > how do I get $strText to contain "foo" instead of $1?
>
> $strText =~ s/$search/$repl/gie;
No- contrary to the documentation in perlop, the "e" modifier
eval's the replacement text as a BLOCK, and *not* as an EXPR.
That means the return value of
eval { $repl } # same as (eval '$repl'): see perldoc -f eval
is substituted for foo, which of course does not interpolate $1.
Besides using /qq{"$repl"}/giee, an alternative (and somewhat ugly)
approach to this might be something like:
# untested code
# interpolate $search, $repl text *before* constructing s// operator
# can fail if $search or $repl have unescaped "/"'s
sub build_s ($$$) {
my $s = join '/', "s", @_;
eval " sub{ $s " . ' for @_; @_ }' or die;
}
build_s($search, $repl, 'gi') -> ($strText);
--
Joe Schaefer "Few things are harder to put up with than the annoyance of a
good example."
--Mark Twain
------------------------------
Date: 16 Oct 2001 11:29:08 -0700
From: showdog@my-deja.com (=?ISO-8859-1?Q?sh=F2wd=F6g?=)
Subject: Re: putting $1..$9 into a replacement string such as $repl="=csinfo$1$2^$3"
Message-Id: <187c116b.0110161029.4e04e12b@posting.google.com>
Bernard El-Hagin <bernard.el-hagin@lido-tech.net> wrote in message news:<slrn9snnu3.85e.bernard.el-hagin@gdndev25.lido-tech>...
> On 15 Oct 2001 14:36:54 -0700, shòwdög <showdog@my-deja.com> wrote:
>
>
> [snip]
>
> [snip]
> > use strict;
> > use warnings;
> > [snip]
> > foreach my $line (@lines) { #lines look like: /UU|Ü
> > my @tokens = split (/\|/, $line);
> > $hECodes{$counter++} = \@tokens;
> > # I quotemeta the lh expression elsewhere in the code
> > }
> > @sortedECodeKeys = sort { $a <=> $b } keys %hECodes;
> >
> > Then later in the code I do this:
> >
> > while (<INPUTFILE>) {
> > chomp;
> > foreach my $ecode (@sortedECodeKeys) {
> > my $source = $hECodes{$ecode}[0];
> > my $dest = $hECodes{$ecode}[1];
> > s{$source}{$dest}g;
> > }
> > }
> >
> > SO, when $source equals '\=csinfo(.*)([A-Za-z]+)\/([A-Za-z]+)'
> > and $dest equals '=csinfo$1$2^$3'
> > and $_ equals '=csinfo 00000130c/c overruled on other grounds'
> > Then,
> > s{$source}{$dest}g
> > sets $_ equal to '=csinfo$1$2^$3 overruled on other grounds'
> >
> > so the digits variables are being taken literally.
>
> Try changing the substitute to:
>
> eval "s{$source}{$dest}g;";
This is what I got:
my $search = '(foo)';
my $repl = 'this is $1. hi there.';
my $strText = 'foo foo';
eval "$strText =~ s/$search/$repl/g;";
print $strText;
__END__
foo foo
------------------------------
Date: Tue, 16 Oct 2001 20:13:13 GMT
From: Steve Allan <stevea@wrq.com>
Subject: Re: Rookie with a perl issue
Message-Id: <m3lmib5ooi.fsf@vhilx.wrq.com>
choareau@aol.com (Choareau) writes:
>#/usr/local/bin/perl
>
>print("Entrez l'adresse du Packetshaper \n");
>$Ipadresse= <STDIN>;
>chop($ipadresse);
chomp is preferred over chop for stripping line endings. See perldoc
for reasons why.
perldoc -f chomp
>print(" salut voici l'adresse");
>print($ipadresse, "\n");
The case sensitivity issue was already covered.
--
-- Steve __
------------------------------
Date: 16 Oct 2001 09:57:06 -0700
From: web_forums@yahoo.com (Drew)
Subject: Searching and splitting..
Message-Id: <59c77510.0110160857.36405162@posting.google.com>
Hey there,
Another newbie question. I'm creating a CGI form (written in Perl)
that takes in user input, writes it to a file and sends the user an
email confirmation. The user must be *registered* to actually fill out
the form - i.e provide a valid username. The username and password
details are stored in a text file in like so: 'username:passwd'. I've
basically got everything sorted out, but just need some help on
searching the file for the username. I've tried grep, and it works
fine, but some books say grep may not be the best option. So I tried
this instead:
$datafile = "password.txt"|| die "Can't Open $datafile:
$!\n";;
$str = $FORM{'username'};
open(INF,$datafile);
@mydata = <INF>;
close(INF);
@mydata = split(/:/, $mydata);
if ($mydata =~ /$str/)
{
# do stuff..
}
else
{
# do stuff
}
This obviously doesn't work. If someone could point out where I'm
going wrong, it'd be great. Thanks for your time.
- Drew
------------------------------
Date: Tue, 16 Oct 2001 20:36:36 GMT
From: Andrew Cady <please@no.spam>
Subject: Re: Searching and splitting..
Message-Id: <87n12rl3xv.fsf@homer.cghm>
web_forums@yahoo.com (Drew) writes:
> Hey there,
>
> Another newbie question. I'm creating a CGI form (written in Perl)
> that takes in user input, writes it to a file and sends the user an
> email confirmation. The user must be *registered* to actually fill
> out the form - i.e provide a valid username. The username and
> password details are stored in a text file in like so:
> 'username:passwd'. I've basically got everything sorted out, but
> just need some help on searching the file for the username. I've
> tried grep, and it works fine, but some books say grep may not be
> the best option. So I tried this instead:
>
> $datafile = "password.txt"|| die "Can't Open $datafile: $!\n";;
The assignment of the string to the scalar is not where you want to
check for failure.
> $str = $FORM{'username'};
$str is not a very descriptive name. Also the 'quotes' are
unnecessary.
> open(INF,$datafile);
Here is where you check if open fails (i.e. when you call open).
> @mydata = <INF>;
> close(INF);
>
> @mydata = split(/:/, $mydata);
What do you mean by $mydata?
Anyway you should get your data into a structure that conceptually
makes sense, i.e. a hash:
%mydata = map { split /:/ } @mydata;
map will run the split on each element of @mydata and return a list of
all the results (it evaluates split in list context and puts the
entire list into the result). Assigning the list to %mydata will
create a hash using the first element as key, second its value, third
as key, fourth as its value, etc.
If there are other fields in the password file, you'll have to use a
slice of split's return, like so:
%mydata = map { (split /:/)[0,1] } @mydata;
Even better is to put this before you close(INF) and use:
my %password = map { (split /:/)[0,1] } <INF>;
Then you use $password{user} to get user's password, and the whole
thing is only three lines:
open(PW, "<password.txt") or die "Couldn't open password file: $!";
my %password = map { (split /:/)[0,1] } <PW>;
close(PW);
Of course, you could also do it without map:
open(PW, "<password.txt") or die "Couldn't open password file: $!";
my %password;
for (<PW>) {
my ($user, $pw) = split /:/;
$password{$user} = $pw;
}
close(PW);
but that's rather ugly.
------------------------------
Date: 16 Oct 2001 21:00:33 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: Searching and splitting..
Message-Id: <slrn9sp7pe.s8p.damian@puma.qimr.edu.au>
[ a quick code review ]
On 16 Oct 2001 09:57:06 -0700, Drew said:
>...
No -w or use strict; Please use these vefore posting review code,
since otherwise you are asking us to do teh same syntax checking your
computer could do for you.
#!perl -w
use strict;
> $datafile = "password.txt"|| die "Can't Open $datafile:
>$!\n";;
Err, this will never fail (it's just an assignment). You probably
meant to test the open() below. Also, to make strict happy, you will
need to declare this var:
my $datafile = 'password.txt';
> $str = $FORM{'username'};
> open(INF,$datafile);
> @mydata = <INF>;
> close(INF);
>
> @mydata = split(/:/, $mydata);
> if ($mydata =~ /$str/)
>
You probably meant something like:
my $line = grep /^$str/o, @mydata;
my ($username, $passwd) = split ':', $line;
or
my $line = $_ if /^$str/o for @mydata
Note that the /o modifier just stops /^$str/ from being re-compiled
each time through the loop, since $str doesn't change.
But you know you can rewrite this to avoid slurping the whole file:
open INF, $datafile or die "Can't open $datafile: $!\n";
my $line;
while (<INF>) {
last if /^$FORM{'username':)$}/o && $line = $_;
};
my ($username, $passwd) = split ':', $line;
If you are just checking for authorised users, you might want to
consider using crypt() to encode the password.
Also, I believe I have seen examples of code where CGI parameters are
kept in %FORM before. I have no taste for such code. You are much
better off using the CGI module, which is part of the standard Perl
distribution. You can probably replace the code you have for parsing
CGI with something like [untested]:
use CGI;
my $cgi = CGI->new();
my %FORM = map { $_ => $cgi->param($_) } $cgi->param();
Then continue with otherwise unchanged code.
See perldoc CGI.
HTH,
Cheers,
Damian
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub
------------------------------
Date: Tue, 16 Oct 2001 22:04:57 GMT
From: Andrew Cady <please@no.spam>
Subject: Re: Searching and splitting..
Message-Id: <87heszkzum.fsf@homer.cghm>
Andrew Cady <please@no.spam> writes:
>
> web_forums@yahoo.com (Drew) writes:
>
> > Hey there,
> >
> > Another newbie question. I'm creating a CGI form (written in Perl)
> > that takes in user input, writes it to a file and sends the user
> > an email confirmation. The user must be *registered* to actually
> > fill out the form - i.e provide a valid username. The username and
> > password details are stored in a text file in like so:
> > 'username:passwd'. I've basically got everything sorted out, but
> > just need some help on searching the file for the username.
>
> open(PW, "<password.txt") or die "Couldn't open password file: $!";
> my %password = map { (split /:/)[0,1] } <PW>;
> close(PW);
Oops, reading Damian's response I realize it makes no sense to store
the whole thing in a hash given your requirements -- you're only going
to use it once per run.
Maybe:
my $password;
while (my @F = split /:/, <PW>) {
$password = $F[1], last if $F[0] eq $username;
}
------------------------------
Date: Tue, 16 Oct 2001 17:58:13 +0200
From: Sean Egan <sean.egan@eei.ericsson.se>
Subject: Re: Socket read/write select problem
Message-Id: <3BCC5915.A1C73452@eei.ericsson.se>
Hi,
I tried disabling buffering, but to no avail.
I do have an update though.
If I run on two different machines (host1: server, host2: client)
then there is no problem.
It appears to be a problem when both are on the same workstation.
Regards, Sean.
Real wrote:
>
> "Sean Egan" <sean.egan@eei.ericsson.se> wrote in message
> news:3BCB3B39.3617FC68@eei.ericsson.se...
>
> > I you uncomment the 'sleep 3' in the code the problem goes away...
>
> You can try to disable buffering using $| = 1;
>
> Good luck,
> Real
------------------------------
Date: 16 Oct 2001 12:36:55 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Writing and reading encrypted string (password)
Message-Id: <9qhr7n$gfm$1@charity.cs.utexas.edu>
In article <sdgost4aghf0ivlngf4qaonge67fkn6git@4ax.com>,
Lars Oeschey <oeschey@media-saturn.com> wrote:
>I know that there's not too much security. But the password is needed
>for an ftp connect (where it is cleartext on the network anyway), so
>for the ftp server I need the plaintext password. I just want to
>prevent poeple from getting the password just by opening the .ini
>file.
But you have to store the key somewhere so that you can decrypt the
passwords. If they can get the .ini file, they can get the key too,
right?
- Logan
--
"In order to be prepared to hope in what does not deceive,
we must first lose hope in everything that deceives."
Georges Bernanos
------------------------------
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 1941
***************************************