[30104] in Perl-Users-Digest
Perl-Users Digest, Issue: 1347 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 10 00:24:48 2008
Date: Sun, 9 Mar 2008 21:09:07 -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 Sun, 9 Mar 2008 Volume: 11 Number: 1347
Today's topics:
Re: FAQ 5.2 How do I change, delete, or insert a line i <brian.d.foy@gmail.com>
Re: finding modules in an activeperl install <invalid@invalid.net>
Re: finding modules in an activeperl install <invalid@invalid.net>
Re: Regular Expression to Replace UPPER Case Text with (David Combs)
Re: Regular Expression to Replace UPPER Case Text with <cwilbur@chromatico.net>
Re: Setting chmod <John.Smith@invalid.com>
Re: Setting chmod <joost@zeekat.nl>
Re: Setting chmod <noreply@gunnar.cc>
Re: Setting chmod <joost@zeekat.nl>
Re: Setting chmod <noreply@gunnar.cc>
Re: Setting chmod <joost@zeekat.nl>
Re: Setting chmod <ben@morrow.me.uk>
Re: Setting chmod <noreply@gunnar.cc>
substitution - variable in variable <stoupa@practisoft.cz>
Re: substitution - variable in variable <noreply@gunnar.cc>
Re: Use of "return" in place of "last" (newbie question <mike@nospam.co.uk>
Re: Use of "return" in place of "last" (newbie question <mike@nospam.co.uk>
Re: Use of "return" in place of "last" (newbie question (aka ? the Platypus)
Re: Use of "return" in place of "last" (newbie question <mike@nospam.co.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 09 Mar 2008 15:30:37 -1000
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: FAQ 5.2 How do I change, delete, or insert a line in a file, or append to the beginning of a file?
Message-Id: <090320081530372433%brian.d.foy@gmail.com>
In article <yluer6ekx2c5.fsf@jonfen.knupf.com>, Hannes Hrl
<nospam@tugraz.at> wrote:
> PerlFAQ Server <brian@stonehenge.com> writes:
>
> [...]
>
> > To prepend lines to the beginning, print those lines before you enter
> > the loop that prints the existing lines.
> >
> > open my $in, '<', $file or die "Can't read old file: $!";
> > open my $out, '>', "$file.new" or die "Can't write new file:
> $!";
> >
> > print "# Add this line to the top\n"; # <--- HERE'S THE MAGIC
>
> Shouldn't it read
>
> print $out "# Add this line to the top\n"; # <--- HERE'S THE
> MAGIC
Yes, that print should use the $old filehandle. I've fixed both
ommissions in the source.
Thanks,
------------------------------
Date: Sun, 9 Mar 2008 17:02:50 -0700
From: "Gerry Ford" <invalid@invalid.net>
Subject: Re: finding modules in an activeperl install
Message-Id: <1205106887_58307@news.newsgroups.com>
"Ben Morrow" <ben@morrow.me.uk> wrote in message
news:ucmca5-mb92.ln1@osiris.mauzo.dyndns.org...
>
> Quoth "Gerry Ford" <invalid@invalid.net>:
>> ## minimal nntp client
>>
>>
>> #!/usr/bin/perl -1
>>
>> use strict;
>> use warnings;
>> use Net::NNTP;
>> use Date::Parse;
>>
> <snip>
>>
>> # perl client2.pl
>> #end script begin comment by perl.exe
>>
>> Can't locate Date/Parse.pm in @INC (@INC contains: C:/Perl/site/lib
>> C:/Perl/lib .) at client2.pl line 9.
>> BEGIN failed--compilation aborted at client2.pl line 9.
>>
>> I was hoping to look at the time on the messages in the next several
>> hours
>> but can't find date/parse on my own machine. How do I tell perl.exe
>> where
>> to find the date/parse module?
>
> You need to install it with ppm.
>
> Ben
>
>
http://zaxfuuq.net/perl148.jpg
This screen dump shows some of my options with activestate's ppm, none of
which seems to be to download Date/Parse.pm .
I did take my best shot by downloading Date-Object, and that caused a file
named Date to appear in the site file, which is where perl.exe is looking
for it, so that's a good thing. But there was nothing that resulted in a
.pm file, which is what I would expect. ??
I also seem not to have Net::nntp, but this seems not to matter.
--
Gerry Ford
"Anybody who says, that a high-speed collision with water is the same as
with concrete, likely has more experience with the former than the latter."
------------------------------
Date: Sun, 9 Mar 2008 17:50:04 -0700
From: "Gerry Ford" <invalid@invalid.net>
Subject: Re: finding modules in an activeperl install
Message-Id: <1205109721_58309@news.newsgroups.com>
"Ben Morrow" <ben@morrow.me.uk> wrote in message
news:ucmca5-mb92.ln1@osiris.mauzo.dyndns.org...
> You need to install it with ppm.
http://zaxfuuq.net/perl149.jpg
I got it. A little persistence pays off. The trick was to look it up on
cpan. What results is not date/parse.pm but Parse.pm in the date folder of
the lib folder. What I needed was tipped off by the download of
Timedate.tz.tarball. I found Timedate in activestate and voila.
The author is shown on the right of the above screen dump. (A little
acknowledgement and local celebrity go a long way.)
--
Gerry Ford
"Anybody who says, that a high-speed collision with water is the same as
with concrete, likely has more experience with the former than the latter."
------------------------------
Date: Mon, 10 Mar 2008 01:06:55 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: Regular Expression to Replace UPPER Case Text with lower case text
Message-Id: <fr21jf$nte$1@reader2.panix.com>
In article <8763wk4ww1.fsf@mithril.chromatico.net>,
Charlton Wilbur <cwilbur@chromatico.net> wrote:
...
>
>I have a whole series of anecdotes to illustrate this, but I'll leave
>you with punch lines.
>
Yep, those are good'uns. You say you have more?
Maybe post them here for us all to see?
Thanks,
David
------------------------------
Date: 09 Mar 2008 21:14:03 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Regular Expression to Replace UPPER Case Text with lower case text
Message-Id: <871w6jv0ro.fsf@mithril.chromatico.net>
>>>>> "DC" == David Combs <dkcombs@panix.com> writes:
DC> Maybe post them here for us all to see?
Anecdotes don't get you anywhere; the people who are capable of
learning from them don't generally need them, and they're inadequate
to convince people who want glossy magazines and highly-funded case
studies.
Charlton
--
Charlton Wilbur
cwilbur@chromatico.net
------------------------------
Date: Sun, 09 Mar 2008 22:44:51 +0200
From: John <John.Smith@invalid.com>
Subject: Re: Setting chmod
Message-Id: <cvi8t3ln1j6guva3p893ltib4tgfktveh7@4ax.com>
Joost Diepenmaat <joost@zeekat.nl> wrote:
>John <John.Smith@invalid.com> writes:
>
>> Why is the test.txt not chmodded to 0600 but to 0755
>
>Because $sec is not a file name or a file handle. It's a good idea to
>check if any file operation succeeded:
>
>open(TEKST,">test.txt") or die $!;
>chmod 0600,*TEKST or die $!;
>binmode(TEKST);
>print TEKST "hello world";
>close TEKST;
I still got different result. This time I've got 0644
------------------------------
Date: Sun, 09 Mar 2008 22:17:17 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Setting chmod
Message-Id: <87skyzvbqa.fsf@zeekat.nl>
John <John.Smith@invalid.com> writes:
> Joost Diepenmaat <joost@zeekat.nl> wrote:
>>Because $sec is not a file name or a file handle. It's a good idea to
>>check if any file operation succeeded:
>>
>>open(TEKST,">test.txt") or die $!;
>>chmod 0600,*TEKST or die $!;
>>binmode(TEKST);
>>print TEKST "hello world";
>>close TEKST;
>
> I still got different result. This time I've got 0644
That's strange. With that code you should only get permissions of 0600
or less (when umask is set to something other than 0).
What are the owners and permissions of text.txt and its containing
directory prior to running the command?
What operating system & perl version are you using?
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Sun, 09 Mar 2008 22:17:45 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Setting chmod
Message-Id: <63j2kgF22ohuiU1@mid.individual.net>
John wrote:
> Joost Diepenmaat <joost@zeekat.nl> wrote:
>> John <John.Smith@invalid.com> writes:
>>> Why is the test.txt not chmodded to 0600 but to 0755
>>
>> Because $sec is not a file name or a file handle. It's a good idea to
>> check if any file operation succeeded:
>>
>> open(TEKST,">test.txt") or die $!;
>> chmod 0600,*TEKST or die $!;
>> binmode(TEKST);
>> print TEKST "hello world";
>> close TEKST;
>
> I still got different result. This time I've got 0644
Did you check the return value from chmod() as suggested? When I run the
code as above, I got a "No such file or directory" error message. Then I
tried
chmod 0600, 'test.txt' or die $!;
which worked.
Please also see my other message in this thread.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 09 Mar 2008 22:26:20 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Setting chmod
Message-Id: <87od9nvbb7.fsf@zeekat.nl>
Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
>>> open(TEKST,">test.txt") or die $!;
>>> chmod 0600,*TEKST or die $!;
>>> binmode(TEKST);
>>> print TEKST "hello world";
>>> close TEKST;
[ ... ]
> Did you check the return value from chmod() as suggested? When I run
> the code as above, I got a "No such file or directory" error
> message.
AFAIK the code as posted above should work in systems that support
fchmod() - basically any recent Unix - and should generate a fatal error
at line 2 on systems that don't.
Whatever it does it should never give a "No such file or directory
error". At least not at line 2.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Sun, 09 Mar 2008 23:42:07 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Setting chmod
Message-Id: <63j7inF274bjdU1@mid.individual.net>
Joost Diepenmaat wrote:
> Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
>
>>>> open(TEKST,">test.txt") or die $!;
>>>> chmod 0600,*TEKST or die $!;
>>>> binmode(TEKST);
>>>> print TEKST "hello world";
>>>> close TEKST;
>
> [ ... ]
>
>> Did you check the return value from chmod() as suggested? When I run
>> the code as above, I got a "No such file or directory" error
>> message.
>
> AFAIK the code as posted above should work in systems that support
> fchmod() - basically any recent Unix - and should generate a fatal error
> at line 2 on systems that don't.
>
> Whatever it does it should never give a "No such file or directory
> error". At least not at line 2.
$ cat test.pl
print "$^O\n";
open(TEKST,">test.txt") or die $!;
chmod 0600,*TEKST or die $!;
$ perl test.pl
linux
No such file or directory at test.pl line 3.
$ ls -l test.txt
-rw-rw-r-- 1 gunnarh gunnarh 0 Mar 9 18:42 test.txt
$
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 09 Mar 2008 23:54:23 +0100
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: Setting chmod
Message-Id: <87k5kbv78g.fsf@zeekat.nl>
Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
> $ cat test.pl
> print "$^O\n";
> open(TEKST,">test.txt") or die $!;
> chmod 0600,*TEKST or die $!;
> $ perl test.pl
> linux
> No such file or directory at test.pl line 3.
> $ ls -l test.txt
> -rw-rw-r-- 1 gunnarh gunnarh 0 Mar 9 18:42 test.txt
> $
Agh. I just checked and this code only runs as I described in perl 5.8.8
and higher. Older perls don't accept file handles as arguments to chmod.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Sun, 9 Mar 2008 23:41:23 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Setting chmod
Message-Id: <369ea5-q95.ln1@osiris.mauzo.dyndns.org>
Quoth Joost Diepenmaat <joost@zeekat.nl>:
> John <John.Smith@invalid.com> writes:
>
> > Why is the test.txt not chmodded to 0600 but to 0755
>
> Because $sec is not a file name or a file handle. It's a good idea to
> check if any file operation succeeded:
>
> open(TEKST,">test.txt") or die $!;
> chmod 0600,*TEKST or die $!;
Don't use bare globs unless you really have to. They're very magic, and
it's always safer to use a globref when you can:
chmod 0600, \*TEKST or die $!;
Of course, a lexical filehandle would be better again.
Ben
------------------------------
Date: Mon, 10 Mar 2008 02:48:42 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Setting chmod
Message-Id: <63jigjF26o05bU1@mid.individual.net>
Joost Diepenmaat wrote:
> Gunnar Hjalmarsson <noreply@gunnar.cc> writes:
>
>> $ cat test.pl
>> print "$^O\n";
>> open(TEKST,">test.txt") or die $!;
>> chmod 0600,*TEKST or die $!;
>> $ perl test.pl
>> linux
>> No such file or directory at test.pl line 3.
>> $ ls -l test.txt
>> -rw-rw-r-- 1 gunnarh gunnarh 0 Mar 9 18:42 test.txt
>> $
>
> Agh. I just checked and this code only runs as I described in perl 5.8.8
> and higher. Older perls don't accept file handles as arguments to chmod.
That explains it; I'm using v5.8.1.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Mon, 10 Mar 2008 04:15:13 +0100
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: substitution - variable in variable
Message-Id: <fr29ff$2i87$1@ns.felk.cvut.cz>
Please can anybody help me with this substitution? The example is described
well I hope ;-)
#!/usr/bin/perl
use strict;
my $string = '<a href="xxx.cgi?parameter">click</a>';
my $what = 'xxx.cgi'; # in reality readed from database (value is variable)
my $rx1=quotemeta($what);
my $rx2 = '^.+?$rx1\?(.+?)".+$';
# this not work
$string =~ s/$rx2/$1/;
print "$string\n";
# output is:
#<a href="xxx.cgi?parameter">click</a>
# this work as expected
$rx2 =~ s/\$rx1/$rx1/; # how write s/// in next line without this line?
$string =~ s/$rx2/$1/;
print "$string\n";
# output is:
#parameter
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail from
another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>
------------------------------
Date: Mon, 10 Mar 2008 04:57:21 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: substitution - variable in variable
Message-Id: <63jq1qF273bp2U1@mid.individual.net>
Petr Vileta wrote:
> Please can anybody help me with this substitution? The example is
> described well I hope ;-)
>
> #!/usr/bin/perl
> use strict;
> my $string = '<a href="xxx.cgi?parameter">click</a>';
> my $what = 'xxx.cgi'; # in reality readed from database (value is variable)
> my $rx1=quotemeta($what);
> my $rx2 = '^.+?$rx1\?(.+?)".+$';
>
> # this not work
> $string =~ s/$rx2/$1/;
> print "$string\n";
> # output is:
> #<a href="xxx.cgi?parameter">click</a>
>
> # this work as expected
> $rx2 =~ s/\$rx1/$rx1/; # how write s/// in next line without this line?
> $string =~ s/$rx2/$1/;
> print "$string\n";
> # output is:
> #parameter
Why bother with the s/// operator?
if ( $string =~ /\Q$what\E\?(.+?)"/ ) {
print "$1\n";
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 09 Mar 2008 21:14:15 +0000
From: Michael <mike@nospam.co.uk>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205097255.21969.0@proxy00.news.clara.net>
On Sun, 09 Mar 2008 19:49:41 +0000, Jürgen Exner wrote:
<snip>
>>Just as a matter of interest, so far as I could see, my technique of
>>setting a flag was the simplest way of maintaining the integrity of the
>>foreach (terminating it with "last" rather than jumping out of it with
>>"return"), whilst not wasting processor cycles performing irrelevant
>>loops later in the function. I am a newbie and don't know much - is
>>there a more elegant alternative which would satisfy the functional
>>programmers?
>
> Well, doing strict functional programming in Perl is extremely hard
> anyway because that't not how the language was designed. But if you
> follow structured programming/formal programming/program
> transformations/... then the condition under which the loop terminiates
> should be defined in the condition of the loop. Neither last() nor
> return() fits that bill because they exit the loop unexpectedly from
> somewhere in the middle which is a nightmare for e.g. formal program
> verification.
>
> Using tons of flags is the typical workaround by inexperienced
> programmers. I have seen that literally hundreds of times in our
> university classes. Unfortunately flags can easily lead to to convoluted
> and difficult to maintain code because it can become very difficult to
> follow their logic and to analyse where which flag is being set under
> what conditions.
>
> Very often it takes just a little bit of restructuring of the loop to
> get a clean and easy to understand single exit condition without
> bail-outs in the middle. One approach that works often is to rearrange
> the exection sequence in the loop and/or reduce the loop by one
> iteration by moving some initialization code before the loop:
>
> Generic example:
> while (cond_x) {
> do_some_work();
> if (cond_y) {last};
> do_more_work();
> }
>
> can usually be transformed into something like
> do__prep_work(); #includes do_some_work() for first iteration while
> (cond_x and not cond_y) {
> do_more_work();
> do_some_work();
> }
Thank-you very much for taking the time to write so helpfully Jue.
/M
------------------------------
Date: Sun, 09 Mar 2008 21:15:42 +0000
From: Michael <mike@nospam.co.uk>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205097342.21969.1@proxy00.news.clara.net>
On Sun, 09 Mar 2008 18:56:51 +0100, Joost Diepenmaat wrote:
<snip>
>> Just as a matter of interest, so far as I could see, my technique of
>> setting a flag was the simplest way of maintaining the integrity of the
>> foreach (terminating it with "last" rather than jumping out of it with
>> "return"), whilst not wasting processor cycles performing irrelevant
>> loops later in the function. I am a newbie and don't know much - is
>> there a more elegant alternative which would satisfy the functional
>> programmers?
>
> There are more "elegant" (for some value of elegant) functional
> alternatives , but perl's constructs don't really allow you to write
> them as nicely as more functional programming languages do. For one,
> perl's if { ... } else { ... } constructs aren't expressions, and also,
> perl uses arrays instead of linked lists:
>
> example in common lisp
>
> (defun find-value (value list)
> (and list
> (if (string= value (first list))
> value
> (find-value (rest list)))))
>
> (defun example
> (or (find-value my-value my-list)
> (find-value my-other-value my-other-list)))
>
> Using these kind of recursive calls almost automatically force you to
> break up your sample problem into 2 subs.
>
> Also note that you can then re-use the find-value sub to reduce the
> total amount of code.
>
>
> same algorithm in perl.
>
> sub find_value {
> my ($value,@list) = @_;
> if (@list) {
> my $first = shift @list;
> if ($first eq $value) {
> return $first;
> }
> return find_value($value,@list);
> }
> return; # return false
> }
>
> sub example {
> find_value($value,@list) or
> find_value($other_value,@other_list);
> }
>
> note that you probably still want to use plenty of return()s here.
>
> also note: is is pretty inefficient if @list is long. you'd generally
> write the find_value function using foreach(), and pass the lists as
> array references.
>
> ps: none of this code has been tested. YMMV.
Thank-you very much for taking the time to write so helpfully Joost.
/M
------------------------------
Date: Sun, 09 Mar 2008 21:28:36 GMT
From: "David Formosa (aka ? the Platypus)" <dformosa@usyd.edu.au>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <slrnft8mgv.b50.dformosa@localhost.localdomain>
On Sun, 09 Mar 2008 14:34:32 +0000, Michael <mike@nospam.co.uk> wrote:
> I am new to perl, and I am wondering whether it is a bad thing to jump out
> of a foreach loop with return. I have a function which searches for a
> value in one of several arrays.
You might look at the function "first" from the core module List::Util
which implements that for you.
------------------------------
Date: Sun, 09 Mar 2008 22:07:11 +0000
From: Michael <mike@nospam.co.uk>
Subject: Re: Use of "return" in place of "last" (newbie question)?
Message-Id: <1205100431.17703.0@proxy01.news.clara.net>
On Sun, 09 Mar 2008 21:28:36 +0000, David Formosa (aka ? the Platypus)
wrote:
<snip>
> You might look at the function "first" from the core module List::Util
> which implements that for you.
Thank-you. I had no idea that existed. Perl is a rather extensive
wonderland.
/M
------------------------------
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 1347
***************************************