[31240] in Perl-Users-Digest
Perl-Users Digest, Issue: 2485 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 19 14:09:54 2009
Date: Fri, 19 Jun 2009 11:09:21 -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 Fri, 19 Jun 2009 Volume: 11 Number: 2485
Today's topics:
Re: FAQ 2.11 Perl Books <brian.d.foy@gmail.com>
Re: FAQ 2.11 Perl Books <brian.d.foy@gmail.com>
Re: FAQ 2.11 Perl Books <cartercc@gmail.com>
Perl icon? <cartercc@gmail.com>
Re: Perl icon? <uri@StemSystems.com>
Re: Perl icon? <ben@morrow.me.uk>
Re: Perl icon? <cartercc@gmail.com>
Re: Perl WTF <brandon@geronimoa11oys.invalid>
Re: Perl WTF <1usa@llenroc.ude.invalid>
Re: Perl WTF <1usa@llenroc.ude.invalid>
Re: Replace the first instance only of a string <smallpond@juno.com>
Re: Replace the first instance only of a string <1usa@llenroc.ude.invalid>
Re: Replace the first instance only of a string <massion@gmx.de>
Re: Replace the first instance only of a string <glennj@ncf.ca>
Re: Replace the first instance only of a string <jimsgibson@gmail.com>
Re: Replace the first instance only of a string <ben@morrow.me.uk>
Re: Replace the first instance only of a string <1usa@llenroc.ude.invalid>
Re: Replace the first instance only of a string <jurgenex@hotmail.com>
Re: Replace the first instance only of a string <willem@snail.stack.nl>
Re: Replace the first instance only of a string <1usa@llenroc.ude.invalid>
Re: Replace the first instance only of a string <1usa@llenroc.ude.invalid>
Re: SetUID <ben@morrow.me.uk>
Re: Test Password (Linux) <ben@morrow.me.uk>
tracking database handles <brandon@geronimoa11oys.invalid>
Re: tracking database handles <ben@morrow.me.uk>
Re: tracking database handles <smallpond@juno.com>
Re: tracking database handles <brandon@geronimoa11oys.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 19 Jun 2009 10:36:31 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <190620091036311253%brian.d.foy@gmail.com>
In article
<9c4e9512-23fc-453a-a7fc-ae12aec63f6b@y7g2000yqa.googlegroups.com>,
ccc31807 <cartercc@gmail.com> wrote:
> On Jun 16, 12:40 pm, brian d foy <brian.d....@gmail.com> wrote:
> > ccc31807 <carte...@gmail.com> wrote:
> > > MySQL and Perl for the Web,
> >
> > Surely out-of-date by now, being 8 years old.
>
> Conjecture, opinion, or fact?
> The book is more out of date for Apache and MySQL than it is for Perl
> and DBI, and besides, the techniques haven't changed.
Indeed, as you say, out of date.
Books targeted at very specific technologies go out-of-date much more
quickly than general books like _Data Munging_ which aren't tied to
current trends.
------------------------------
Date: Fri, 19 Jun 2009 10:37:19 -0500
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <190620091037194165%brian.d.foy@gmail.com>
In article
<9c4e9512-23fc-453a-a7fc-ae12aec63f6b@y7g2000yqa.googlegroups.com>,
ccc31807 <cartercc@gmail.com> wrote:
> I'm not going to suggest that you read the book. I will suggest that
> you refrain from commenting on it until you have read it.
You might have forgot who I am, but I read every Perl book.
------------------------------
Date: Fri, 19 Jun 2009 09:23:20 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: FAQ 2.11 Perl Books
Message-Id: <fc216089-cfa2-4ba6-b90b-7c6e60745cc5@l32g2000vba.googlegroups.com>
On Jun 19, 11:37=A0am, brian d foy <brian.d....@gmail.com> wrote:
> You might have forgot who I am, but I read every Perl book.
Your name graces the spine of a book that I keep on the shelf on my
desk. We've never met, but I know who you are.
My original response was that the book I mentioned was one that I had
found helpful. I don't quite understand the point of your reply. Is it
that you claim that I didn't find the book helpful? Is it that you
think I am the only one who would find the book helpful? Is it that
you didn't find the book helpful?
Do you think that the publication date of a book is the primary
indicator of the datedness of a book? If so, I can list for you a
number of technology books 10 years old or older that are as fresh now
as the day they were written. (e.g., Paul Graham's ANSI Common Lisp,
(c) 1995).
As to being out of date, well, that's really a fuzzy concept. Apache
is not out of date, mod_perl is not out of date, Perl is not out of
date, MySQL is not out of date, database applications are not out of
date, webified database applications are not out of date. Okay, so you
have to read the up to date documentation. So what? Most people don't
use bleeding edge technology, and I prefer to use the second or third
newest version rather than the newest. Besides which, by the time
technology books make it to press, they tend to be somewhat out of
date anyway. My book that has your name on it is out of date, but I
still think it's useful and I wouldn't consider discarding it.
Were you offended that I mentioned a book that wasn't on your list? If
you were, I won't apologize. In fact, I'll do it again. I have taught
several Perl courses in a local technical college using Diane Zak's
Perl/CGI by Course Technology, Web Warrior Series and have found it a
good match for both the school's web program and the students (not to
mention that it's the mandated text for the course.) Is it out of
date? Yes, in reference to Perl 5.10 and mod_perl. No, in reference to
data types, control structures, methodology, and architecture.
No need to have a hissy fit. I'm no threat to you. If you want to post
a critical review of the book I mentioned, I'd be interested in
reading it, but you will have to be more specific than "Surely out-of-
date by now, being 8 years old."
CC
------------------------------
Date: Fri, 19 Jun 2009 08:45:33 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Perl icon?
Message-Id: <6613ffb8-ae13-45d0-961c-f7a1f4b988db@s21g2000vbb.googlegroups.com>
On my Windows desktop, I like to use icons that represent the
particular documentation and/or application, such as the lambda symbol
for Lisp, the coffee cup for Java, the red 'e' for Erlang, the dolphin
for MySQL, the penguin for Linux, the demon for FreeBSD, and so on.
There doesn't seem to be a symbol, mascot, or icon for Perl that I can
find. Does such a thing exist? The closest thing I can find is the AS
lizard or salamander or whatever.
CC
------------------------------
Date: Fri, 19 Jun 2009 12:04:34 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Perl icon?
Message-Id: <87vdms5hwt.fsf@quad.sysarch.com>
>>>>> "c" == ccc31807 <cartercc@gmail.com> writes:
c> On my Windows desktop, I like to use icons that represent the
c> particular documentation and/or application, such as the lambda symbol
c> for Lisp, the coffee cup for Java, the red 'e' for Erlang, the dolphin
c> for MySQL, the penguin for Linux, the demon for FreeBSD, and so on.
c> There doesn't seem to be a symbol, mascot, or icon for Perl that I can
c> find. Does such a thing exist? The closest thing I can find is the AS
c> lizard or salamander or whatever.
what rock have you been living under? :) the camel has always been
perl's primary icon due to it being the animal on the first perl book,
o'reilly's programming perl (written by larry wall). but note that the
camel usage with perl is trademarked and can't be used commercially
without permission. there are a couple of those images you can use on
websites for free if they link back to o'reilly. you should be able to
find them easily by searching for camel logo on the o'reilly site. i
would assume using a camel image on your desktop is something that
nobody will care about but IANAL.
there is a newer image of an onion which is owned by the perl foundation
and it is free to use in all perl situations. it is an onion since larry
wall's annual speeches about perl at oscon were called state of the
onion. you should also be able to search for those easily. i don't have
the time to do it now.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Fri, 19 Jun 2009 17:20:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Perl icon?
Message-Id: <ieqsg6-mfv2.ln1@osiris.mauzo.dyndns.org>
Quoth ccc31807 <cartercc@gmail.com>:
> On my Windows desktop, I like to use icons that represent the
> particular documentation and/or application, such as the lambda symbol
> for Lisp, the coffee cup for Java, the red 'e' for Erlang, the dolphin
> for MySQL, the penguin for Linux, the demon for FreeBSD, and so on.
>
> There doesn't seem to be a symbol, mascot, or icon for Perl that I can
> find. Does such a thing exist? The closest thing I can find is the AS
> lizard or salamander or whatever.
Have you looked at the front of the Camel book lately? :)
The camel icon is included with the perl source as
win32/perlexe.ico.packd. You can unpack it using uupacktool.pl in the
root of the source directory (or a trivial script using unpack "u").
Ben
------------------------------
Date: Fri, 19 Jun 2009 10:14:02 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: Perl icon?
Message-Id: <9af42ee7-3c9e-4af4-ae5e-763fee965b02@n21g2000vba.googlegroups.com>
On Jun 19, 12:20=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Have you looked at the front of the Camel book lately? :)
Okay, I had a mental block. Still, the camel is a little different
from the other icons, which have all been officially adopted in some
way by a sponsoring organization, like Sun for the Java coffee cup or
MySQL for MySQL dolphin or the Apache Foundation for Apache (the
feather) or the FreeBSD Foundation for the Free BSD demon.
> The camel icon is included with the perl source as
> win32/perlexe.ico.packd. You can unpack it using uupacktool.pl in the
> root of the source directory (or a trivial script using unpack "u").
On my machine, it's at C:\Perl\site\lib\Win32\GUI\demos\guiperl.ico.
No need to unpack.
Thanks, CC.
------------------------------
Date: Fri, 19 Jun 2009 11:08:20 -0500
From: Brandon Metcalf <brandon@geronimoa11oys.invalid>
Subject: Re: Perl WTF
Message-Id: <slrnh3ndvd.6il.brandon@cedar.geronimoalloys.com>
On 2009-06-19, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> sisyphus <sisyphus359@gmail.com> wrote in
> news:da9c3cc3-f551-41b7-a666-8e79331dc0c2@x6g2000vbg.googlegroups.com:
...
>> Another way of looking at it - all of the following are true (on many
>> systems):
>> "infertility" == "infertility"
>> "infertility" == "informal"
>> "fanaticists" == "fanaticists"
>> "fanaticists" == "most strings"
>> but
>> "infertility" != "most strings"
>> "nanoseconds" != "nanoseconds"
>>
>> What *were* they thinking of ;-)
>
> Excellent examples. It is clear what is going on if you know what is
> going on but to understand what is going on one needs to connect way too
> many dots. ;-)
I'll bite. What is going on?
--
Brandon
------------------------------
Date: Fri, 19 Jun 2009 16:32:34 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl WTF
Message-Id: <Xns9C2F7F993BBD7asu1cornelledu@127.0.0.1>
Brandon Metcalf <brandon@geronimoa11oys.invalid> wrote in
news:slrnh3ndvd.6il.brandon@cedar.geronimoalloys.com:
> On 2009-06-19, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>> sisyphus <sisyphus359@gmail.com> wrote in
>> news:da9c3cc3-f551-41b7-a666-8e79331dc0c2
@x6g2000vbg.googlegroups.com:
>
> ...
>
>>> Another way of looking at it - all of the following are true (on
>>> many systems):
>>> "infertility" == "infertility"
>>> "infertility" == "informal"
>>> "fanaticists" == "fanaticists"
>>> "fanaticists" == "most strings"
>>> but
>>> "infertility" != "most strings"
>>> "nanoseconds" != "nanoseconds"
>>>
>>> What *were* they thinking of ;-)
>>
>> Excellent examples. It is clear what is going on if you know what is
>> going on but to understand what is going on one needs to connect way
>> too many dots. ;-)
>
>
> I'll bite. What is going on?
Strings that start with 'inf' are being interpreted as infinity (because
the comparison is numeric). By the rules of IEEE floating point
arithmetic, infinity == infinity is true.
Strings that start with 'nan' are being interpreted as nan (because the
comparison is numeric). By the rules of IEEE floating point arithmetic,
a nan cannot compare equal to any other number, even another nan.
"fanaticists" and "most strings" strings are interpreted as zero.
As 0 == 0 is always true, so is "fanaticists" == "most strings".
However, because "infertility" is interpreted as infinity, it does not
compare numerically equal to "most strings".
As was mentioned upthread, you will be warned of such things if you use
warnings.
Argument "most strings" isn't numeric in numeric ne (!=) at -e line 1.
Argument "infertility" isn't numeric in numeric ne (!=) at -e line 1.
So, you have to be careful when using tests for numeric equality.
If you want string comparison, use eq.
On the other hand, I just tried is on my computer with AS Perl
5.10.0.1004, and I get the following:
C:\Temp> perl -e "print 'infertility' == 'most strings'"
1
C:\Temp> perl -e "print 0 + 'infertility'"
0
C:Temp> perl -e "print 'nanoseconds' == 'nanoseconds'
1
etc. so it seems like something was fixed at some point.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 16:42:16 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl WTF
Message-Id: <Xns9C2F813E1CAA2asu1cornelledu@127.0.0.1>
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote in
news:Xns9C2F7F993BBD7asu1cornelledu@127.0.0.1:
> C:Temp> perl -e "print 'nanoseconds' == 'nanoseconds'
> 1
>
> etc. so it seems like something was fixed at some point.
Well, for something to have been fixed,
my $x = 'NaN';
print "Arrrrggghhhh!!!!!\n" if $x == $x;
should not print anything.
But it does.
So, my AS perl on Windows does not seem to support NaNs.
Trying with Strawberry now ... Nope, the same problem.
Cygwin perl works. But then it suffers from the other issue:
$ perl -e 'print "nanoseconds" != "nanoseconds"'
1
$ perl -e 'print "infertility" == "infertility"'
1
$ perl -e 'print "infertility" == "informal"'
1
Sorry for the confusion.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 08:24:53 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: Replace the first instance only of a string
Message-Id: <f6a0163e-ae44-4aac-a16a-d17795562453@r34g2000vba.googlegroups.com>
On Jun 19, 10:13=A0am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>
> __DATA__
> =A0 =A0 A moth ate words. It seemed to me
> =A0 =A0 a strange event when I heard of that wonder,
> =A0 =A0 that a worm, a thief in darkness, should devour
> =A0 =A0 the songs of men, glorious utterance
> =A0 =A0 and a place of strong being. The thievish visitor
> =A0 =A0 was no whit the wiser for swallowing the words.
>
I'm going to remember that poem when a virus
wipes out my hard drive.
------------------------------
Date: Fri, 19 Jun 2009 15:31:22 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Replace the first instance only of a string
Message-Id: <Xns9C2F75390241Basu1cornelledu@127.0.0.1>
smallpond <smallpond@juno.com> wrote in news:f6a0163e-ae44-4aac-a16a-
d17795562453@r34g2000vba.googlegroups.com:
> On Jun 19, 10:13 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
>
>>
>> __DATA__
>> A moth ate words. It seemed to me
>> a strange event when I heard of that wonder,
>> that a worm, a thief in darkness, should devour
>> the songs of men, glorious utterance
>> and a place of strong being. The thievish visitor
>> was no whit the wiser for swallowing the words.
>>
>
> I'm going to remember that poem when a virus
> wipes out my hard drive.
It's a riddle. I stole it from Wikipedia :-)
http://en.wikipedia.org/wiki/Riddle
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 08:53:22 -0700 (PDT)
From: Francois Massion <massion@gmx.de>
Subject: Re: Replace the first instance only of a string
Message-Id: <a239b4c0-5f97-4656-91af-3bf1a5fb1547@n21g2000vba.googlegroups.com>
On 19 Jun., 16:13, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> ccc31807 <carte...@gmail.com> wrote in news:1fa52f6d-4c3a-4f68-ab7b-
> 27b70ee8f...@r13g2000vbr.googlegroups.com:
>
>
>
>
>
> > On Jun 19, 8:27=A0am, Francois Massion <mass...@gmx.de> wrote:
> >> I couldn't find so far a solution to a quite common problem:
>
> >> There is a document in which some words occur many times. I want to
> >> replace ONLY THE FIRST instance of the word and leave the other
> >> instances unchanged.
>
> > I don't know what your input document looks like. Ordinarily, I read
> > in a document line by line, 'line' being defined as a string ending in
> > a newline, \n. I would guess that you want something like this:
>
> > open INFILE, '<', 'inputfile.txt';
> > open OUTFILE, '>', 'outputfile.txt';
> > while (<INFILE>)
> > {
> > =A0 if ($_ =3D~ /target/)
> > =A0 {
> > =A0 =A0 $_ =3D~ s/target/change/;
> > =A0 =A0 last;
> > =A0 }
> > }
> > close INFILE;
> > close OUTFILE;
>
> > This is off the cuff, untested, and Sinan will huff 'sloppy', but the
> > logic is good even if you have to play with the code a bit.
>
> I will not huff 'sloppy'. It is inane. If you are using $_, use it. The
> s/// operator returns a value, use it. As it is, you are doing the same
> work twice -- finding if the line contains 'target'.
>
> Also, he probably does not want to abort the loop, but to write the rest
> of the document unchanged to the output file after the first
> replacement.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $replaced;
>
> while ( <DATA> ) {
> =A0 =A0 $replaced or $replaced =3D s/words/sentences/;
> =A0 =A0 print;
>
> }
>
> __DATA__
> =A0 =A0 A moth ate words. It seemed to me
> =A0 =A0 a strange event when I heard of that wonder,
> =A0 =A0 that a worm, a thief in darkness, should devour
> =A0 =A0 the songs of men, glorious utterance
> =A0 =A0 and a place of strong being. The thievish visitor
> =A0 =A0 was no whit the wiser for swallowing the words.
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
>
> comp.lang.perl.misc guidelines on the WWW:http://www.rehabitation.com/clp=
misc/- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -
Hi Sinan,
Thanks. It is exactly what I needed and works fine! I didn't know the
syntax with "$replaced or $replaced". What is the working principle
behind this?
Francois
------------------------------
Date: 19 Jun 2009 16:05:05 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Replace the first instance only of a string
Message-Id: <slrnh3ndpi.bl0.glennj@smeagol.ncf.ca>
At 2009-06-19 08:27AM, "Francois Massion" wrote:
> There is a document in which some words occur many times. I want to
> replace ONLY THE FIRST instance of the word and leave the other
> instances unchanged.
>
> foreach $sentence (@document) {
> chomp $sentence;
>
> $sentence =~ s/Subject/Topic/;
>
> push (@result,$sentence);
> }
(my $new = join '', @document) =~ s/Subject/Topic/;
my @result = split /\n/, $new; # if you need an array
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
------------------------------
Date: Fri, 19 Jun 2009 09:17:48 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Replace the first instance only of a string
Message-Id: <190620090917487739%jimsgibson@gmail.com>
In article
<a239b4c0-5f97-4656-91af-3bf1a5fb1547@n21g2000vba.googlegroups.com>,
Francois Massion <massion@gmx.de> wrote:
> On 19 Jun., 16:13, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> > ccc31807 <carte...@gmail.com> wrote in news:1fa52f6d-4c3a-4f68-ab7b-
> > 27b70ee8f...@r13g2000vbr.googlegroups.com:
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > my $replaced;
> >
> > while ( <DATA> ) {
> > $replaced or $replaced = s/words/sentences/;
> > print;
> >
> > }
> Thanks. It is exactly what I needed and works fine! I didn't know the
> syntax with "$replaced or $replaced". What is the working principle
> behind this?
The logic is that the 'or' operator is short-circuited. That is, the
expression 'A or B' will be true if either A or B is true. If A is
true, then B need not be evaluated, and Perl does not even evaluate the
expression B. So in the expression
$replaced or $replaced = s/words/sentences/;
if $replaced is true, the assignment expression $replaced = s/../../;
will not be executed, and no substitution will be done. If $replaced is
false, then the substitution will be attempted and the result (true if
the substitution occurred) stored in $replaced. The effect is that only
the first string in the file will be substituted for.
--
Jim Gibson
------------------------------
Date: Fri, 19 Jun 2009 17:13:15 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Replace the first instance only of a string
Message-Id: <r1qsg6-mfv2.ln1@osiris.mauzo.dyndns.org>
Quoth Francois Massion <massion@gmx.de>:
> I couldn't find so far a solution to a quite common problem:
>
> There is a document in which some words occur many times. I want to
> replace ONLY THE FIRST instance of the word and leave the other
> instances unchanged.
>
> Here my code:
> (...)
>
> foreach $sentence (@document) {
> chomp $sentence;
>
> $sentence =~ s/Subject/Topic/;
>
> push (@result,$sentence);
> }
Read the file into a single scalar with File::Slurp then use s///
without the /g option. You appear to be reading the entire file into
memory anyway, so processing line-by-line is futile.
Ben
------------------------------
Date: Fri, 19 Jun 2009 16:20:40 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Replace the first instance only of a string
Message-Id: <Xns9C2F7D953FD2Easu1cornelledu@127.0.0.1>
Francois Massion <massion@gmx.de> wrote in
news:a239b4c0-5f97-4656-91af-3bf1a5fb1547@n21g2000vba.googlegroups.com:
>> Also, he probably does not want to abort the loop, but to write the
>> rest of the document unchanged to the output file after the first
>> replacement.
>>
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my $replaced;
>>
>> while ( <DATA> ) {
>> $replaced or $replaced = s/words/sentences/;
>> print;
>>
>> }
>> --
Please do not quote sigs.
...
> Thanks. It is exactly what I needed and works fine! I didn't know the
> syntax with "$replaced or $replaced". What is the working principle
> behind this?
The s/// operator returns the number of substitutions made. In this
case, it can only ever return zero or one.
The or operator does not evaluate the expression on the right if the
first one is true. So,
$replaced or $replaced = s/words/sentences/;
first looks at the value of $replaced. If that is true, then it does not
try the substitution. If it is false, it does try the substitution. If
the substitution fails, $replaced remains false. If it succeeds,
$replaced becomes true so that the substitution will not be attempted
next time through the loop. Once a substition succeeds, $replaced will
remain true for the rest of the loop.
Read perldoc perlop.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 10:09:55 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Replace the first instance only of a string
Message-Id: <07hn35l22uofq703bsg4ki4si5tlm6mluk@4ax.com>
Francois Massion <massion@gmx.de> wrote:
>On 19 Jun., 16:13, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
[...]
>> while ( <DATA> ) {
>> $replaced or $replaced = s/words/sentences/;
[...]
>Thanks. It is exactly what I needed and works fine! I didn't know the
>syntax with "$replaced or $replaced". What is the working principle
>behind this?
You are using the wrong precedence. It is not "$replaced or $replaced"
but
$replaced or ($replaced = s/words/sentences/);
Meaning: if a substitute is succeeding then $replaced will be set to a
true value.
Then in all following iterations $replaced will be true already and the
short-circuit evaluation of 'or' will prevent the second argument of the
boolean expression to be ever evaluated again.
A neat trick, I have to say.
jue
------------------------------
Date: Fri, 19 Jun 2009 17:23:29 +0000 (UTC)
From: Willem <willem@snail.stack.nl>
Subject: Re: Replace the first instance only of a string
Message-Id: <slrnh3nich.2fpn.willem@snail.stack.nl>
J?rgen Exner wrote:
) Then in all following iterations $replaced will be true already and the
) short-circuit evaluation of 'or' will prevent the second argument of the
) boolean expression to be ever evaluated again.
Does ||= also use short-circuiting ?
I.E.:
$replaced ||= s/foo/bar/;
Anyway,
$replaced = s/foo/bar/ unless $replaced;
Seems more descriptive of its purpose.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Fri, 19 Jun 2009 17:35:35 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Replace the first instance only of a string
Message-Id: <Xns9C2F8A480B1FDasu1cornelledu@127.0.0.1>
Willem <willem@snail.stack.nl> wrote in
news:slrnh3nich.2fpn.willem@snail.stack.nl:
> J?rgen Exner wrote:
> ) Then in all following iterations $replaced will be true already and
> ) the short-circuit evaluation of 'or' will prevent the second
> ) argument of the boolean expression to be ever evaluated again.
...
> $replaced = s/foo/bar/ unless $replaced;
>
> Seems more descriptive of its purpose.
Beauty is in the eye of the beholder. In this case, the substitution
happens only once so
$replaced or $replaced = s/foo/bar/;
makes more sense to me because it relegates the operation that is less
frequent to the end of the sentence.
FWIW:
C:\> perl -MO=Deparse,-p -e "$replaced = s/foo/bar/ unless $replaced;"
($replaced or ($replaced = s/foo/bar/));
;-)
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 17:47:31 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Replace the first instance only of a string
Message-Id: <Xns9C2F8C4E42CE0asu1cornelledu@127.0.0.1>
Willem <willem@snail.stack.nl> wrote in
news:slrnh3nich.2fpn.willem@snail.stack.nl:
> J?rgen Exner wrote:
> ) Then in all following iterations $replaced will be true already and
> the ) short-circuit evaluation of 'or' will prevent the second
> argument of the ) boolean expression to be ever evaluated again.
>
> Does ||= also use short-circuiting ?
Yes it does.
> $replaced ||= s/foo/bar/;
Beautiful if a little more cryptic looking ;-)
My brain short circuits to using or to control flow but in this case that
is not needed.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Fri, 19 Jun 2009 17:10:29 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: SetUID
Message-Id: <lspsg6-mfv2.ln1@osiris.mauzo.dyndns.org>
Quoth jt@toerring.de (Jens Thoms Toerring):
> Ben Morrow <ben@morrow.me.uk> wrote:
> > Quoth jt@toerring.de (Jens Thoms Toerring):
> > > maylcc <sg.maylcc@gmail.com> wrote:
> > > > my chpass.pl was set to -rwsr-sr-x , with this file permission, I'm
> > > > getting an error: Can't do setuid,
> > >
> > > Yes, that's a feature, not a bug. Setuid'ed sripts can be very
> > > dangerous for a number of reasons and thus Perl doesn't run
> > > them blindly. But you can get the script to run when you switch
> > > on taint mode for the script with the -T command line option
> > > (e.g. by having it in the first line of your script):
> > >
> > > #!/usr/bin/perl -T
>
> Sorry for spewing non-sense! I thought I remembered something
> like that, did a fast test and things seemed to work for some
> reason... I guess I better don't post while still having a bit
> of a temperature;-)
That's OK :).
Your system probably has suidperl installed, which is a program that
emulates setid behaviour securely. It relies on the kernel having setid
scripts disabled, so the setid bits are effectively useless on scripts,
and then opens the script itself and execs perl with a /dev/fd-type
argument.
You are probably remembering that if a script has -T on the #! line, it
must also be passed on the command line or perl refuses to run it (by
the time perl comes to parsing the #! line, it's already done other
things that should have been in taint mode, like loaded modules with
-M). This is only a problem if it's run as
perl script
rather than through the executable-script mechanism.
Ben
------------------------------
Date: Fri, 19 Jun 2009 17:15:37 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Test Password (Linux)
Message-Id: <96qsg6-mfv2.ln1@osiris.mauzo.dyndns.org>
Quoth RedGrittyBrick <RedGrittyBrick@spamweary.invalid>:
>
> maylcc wrote:
> > Anyone can share their experience how to allow user to test password
> > with the linux server?
> > i have written a perl script but apparently encounter error to execute
> > without root "Can't do setuid".
> >
> > Waiting for any kind soul to help. Thanks.
>
> I'd look at this:
> http://search.cpan.org/perldoc?Authen::PAM
Generally speaking you can't check someone else's password unless you're
root. If the OP has properly set up suEXEC (or some equivalent) to run
the CGI as the user involved, this will work, otherwise he will still
need to gain privilege somehow.
Ben
------------------------------
Date: Fri, 19 Jun 2009 11:01:42 -0500
From: Brandon Metcalf <brandon@geronimoa11oys.invalid>
Subject: tracking database handles
Message-Id: <slrnh3ndhk.6il.brandon@cedar.geronimoalloys.com>
The following works, but is it the proper way to store, in this case,
DBI database handles for later use?
my %handles;
sub connectdb {
my $dbh = DBI->connect(...);
$handles{$dbh} = $dbh;
$dbh;
}
connectdb();
connectdb();
for my $v (values %handles) {
$v->disconnect();
}
I'm looking for general answers on accomplishing this rather than
anything specific to DBI itself. I initially tried storing the
handles as keys, but that doesn't work for obvious reasons. Nor does
storing references to the handles as keys. When trying as references,
I was doing
$handles{\$dbh} = 1;
for my $k (keys %handles) {
${$k}->disconnect();
}
but this results in
Can't use string ("REF(0x843000c)") as a SCALAR ref while "strict refs"
which makes sense.
Thanks.
--
Brandon
------------------------------
Date: Fri, 19 Jun 2009 17:21:27 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: tracking database handles
Message-Id: <7hqsg6-mfv2.ln1@osiris.mauzo.dyndns.org>
Quoth Brandon Metcalf <brandon@geronimoa11oys.invalid>:
> The following works, but is it the proper way to store, in this case,
> DBI database handles for later use?
>
> my %handles;
>
> sub connectdb {
> my $dbh = DBI->connect(...);
> $handles{$dbh} = $dbh;
> $dbh;
> }
>
> connectdb();
> connectdb();
>
> for my $v (values %handles) {
> $v->disconnect();
> }
Yes, but note that this is not threadsafe. If you need threadsafety, see
the Hash::Util::Fieldhash module that comes with perl 5.10.
Ben
------------------------------
Date: Fri, 19 Jun 2009 09:47:37 -0700 (PDT)
From: smallpond <smallpond@juno.com>
Subject: Re: tracking database handles
Message-Id: <197c8f57-dbf4-4bcf-aaef-3c599db237bf@g20g2000vba.googlegroups.com>
On Jun 19, 12:01=A0pm, Brandon Metcalf <bran...@geronimoa11oys.invalid>
wrote:
> The following works, but is it the proper way to store, in this case,
> DBI database handles for later use?
>
> =A0 my %handles;
>
> =A0 sub connectdb {
> =A0 =A0 =A0 my $dbh =3D DBI->connect(...);
> =A0 =A0 =A0 $handles{$dbh} =3D $dbh;
> =A0 =A0 =A0 $dbh;
> =A0 }
>
> =A0 connectdb();
> =A0 connectdb();
>
> =A0 for my $v (values %handles) {
> =A0 =A0 =A0 $v->disconnect();
> =A0 }
>
> I'm looking for general answers on accomplishing this rather than
> anything specific to DBI itself. =A0I initially tried storing the
> handles as keys, but that doesn't work for obvious reasons. =A0Nor does
> storing references to the handles as keys. =A0When trying as references,
> I was doing
>
> =A0 $handles{\$dbh} =3D 1;
>
> =A0 for my $k (keys %handles) {
> =A0 =A0 =A0 ${$k}->disconnect();
> =A0 }
>
> but this results in
>
> =A0 Can't use string ("REF(0x843000c)") as a SCALAR ref while "strict ref=
s"
>
> which makes sense.
>
> Thanks.
Why use a hash when you don't need hash lookups?
my @handles;
push @handles, $dbh;
foreach (@handles) {...}
etc.
------------------------------
Date: Fri, 19 Jun 2009 12:17:02 -0500
From: Brandon Metcalf <brandon@geronimoa11oys.invalid>
Subject: Re: tracking database handles
Message-Id: <slrnh3nguo.6il.brandon@cedar.geronimoalloys.com>
On 2009-06-19, smallpond <smallpond@juno.com> wrote:
> On Jun 19, 12:01 pm, Brandon Metcalf <bran...@geronimoa11oys.invalid>
> wrote:
>> The following works, but is it the proper way to store, in this case,
>> DBI database handles for later use?
>>
>> my %handles;
>>
>> sub connectdb {
>> my $dbh = DBI->connect(...);
>> $handles{$dbh} = $dbh;
>> $dbh;
>> }
>>
>> connectdb();
>> connectdb();
>>
>> for my $v (values %handles) {
>> $v->disconnect();
>> }
>>
>> I'm looking for general answers on accomplishing this rather than
>> anything specific to DBI itself. I initially tried storing the
>> handles as keys, but that doesn't work for obvious reasons. Nor does
>> storing references to the handles as keys. When trying as references,
>> I was doing
>>
>> $handles{\$dbh} = 1;
>>
>> for my $k (keys %handles) {
>> ${$k}->disconnect();
>> }
>>
>> but this results in
>>
>> Can't use string ("REF(0x843000c)") as a SCALAR ref while "strict refs"
>>
>> which makes sense.
>>
>> Thanks.
>
> Why use a hash when you don't need hash lookups?
>
> my @handles;
> push @handles, $dbh;
> foreach (@handles) {...}
I'm using DBI->connect_cached(), so some of the handles will be the
same. I don't want to call disconnect() multiple times on the same
handle.
--
Brandon
------------------------------
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 2485
***************************************