[29571] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 815 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Sep 2 00:09:39 2007

Date: Sat, 1 Sep 2007 21:09:04 -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           Sat, 1 Sep 2007     Volume: 11 Number: 815

Today's topics:
        =encoding <benkasminbullock@gmail.com>
    Re: FAQ 4.32 How do I strip blank space from the beginn (David Combs)
    Re: FAQ 4.32 How do I strip blank space from the beginn <spamtrap@dot-app.org>
    Re: FAQ 4.32 How do I strip blank space from the beginn <stoupa@practisoft.cz>
    Re: FAQ 4.32 How do I strip blank space from the beginn <uri@stemsystems.com>
    Re: How to continue process without waiting for the out <1usa@llenroc.ude.invalid>
    Re: Important Research Project <JCornwall@cox.net>
    Re: Important Research Project <edgrsprj@ix.netcom.com>
        Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN <WeikEngOff@aol.com>
    Re: Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<Patt <noreply@gunnar.cc>
    Re: Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<Patt <anno4000@radom.zrz.tu-berlin.de>
    Re: Searching in a line <lerameur@yahoo.com>
    Re: Searching in a line <1usa@llenroc.ude.invalid>
    Re: Searching in a line xhoster@gmail.com
    Re: Searching in a line <stoupa@practisoft.cz>
    Re: Why use die? <anno4000@radom.zrz.tu-berlin.de>
    Re: Why use die? xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 2 Sep 2007 00:28:01 +0000 (UTC)
From: Ben Bullock <benkasminbullock@gmail.com>
Subject: =encoding
Message-Id: <fbd02h$hkt$1@ml.accsnet.ne.jp>

There seems to be a discrepancy between the documentation for perlpod and
its behaviour.

http://perldoc.perl.org/perlpod.html says

    =encoding encodingname

    This command is used for declaring the encoding of a document. Most
    users won’t need this; but if your encoding isn’t US-ASCII or Latin-1,
    then put a =encoding encodingname command early in the document so
    that pod formatters will know how to decode the document. For
    encodingname, use a name recognized by the Encode::Supported module.
    Examples:

    =encoding utf8

    =encoding koi8-r

    =encoding ShiftJIS

    =encoding big5

However, perldoc says

 ./mymodule.pm:1: Unknown command paragraph "=encoding utf8"

pod2html produced a similar message.

It doesn't depend on the location of the =encoding since I tried putting
it in various positions.

Can anyone explain this?


------------------------------

Date: Sat, 1 Sep 2007 21:42:57 +0000 (UTC)
From: dkcombs@panix.com (David Combs)
Subject: Re: FAQ 4.32 How do I strip blank space from the beginning/end of a string?
Message-Id: <fbcmd1$3qk$1@reader1.panix.com>

I'm wondering how many perl programmers have always-include
libraries of useful functions, one of them being "trim".

So, have a poll, and decide based on that.

David




------------------------------

Date: Sat, 01 Sep 2007 19:50:33 -0400
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: FAQ 4.32 How do I strip blank space from the beginning/end of a string?
Message-Id: <m2y7fq3pyu.fsf@dot-app.org>

dkcombs@panix.com (David Combs) writes:

> I'm wondering how many perl programmers have always-include
> libraries of useful functions, one of them being "trim".

We all do - it's called "CPAN".

sherm--

-- 
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net


------------------------------

Date: Sun, 2 Sep 2007 04:44:25 +0200
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: FAQ 4.32 How do I strip blank space from the beginning/end of a string?
Message-Id: <fbd8bd$7i5$2@ns.felk.cvut.cz>

Sherm Pendley wrote:
> dkcombs@panix.com (David Combs) writes:
>
>> I'm wondering how many perl programmers have always-include
>> libraries of useful functions, one of them being "trim".
>
> We all do - it's called "CPAN".
>
> sherm--
Yes, you are right sherm ;-) But, frankly speaking, the trim() is the 
function which absent in Perl. Everytime I write script for string 
manupulation and I plan to write more then 50 lines I write this

sub trim
{
my $t = shift;
$t =~s/^\s*|\s*$//sg;
return $t;
}

sub ltrim
{
my $t = shift;
$t =~s/^\s*//s;
return $t;
}

sub rtrim
{
my $t = shift;
$t =~s/\s*$//s;
return $t;
}

-- 

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail 
from another non-spammer site please.)




------------------------------

Date: Sun, 02 Sep 2007 03:46:56 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: FAQ 4.32 How do I strip blank space from the beginning/end of a string?
Message-Id: <x7odglloen.fsf@mail.sysarch.com>

>>>>> "PV" == Petr Vileta <stoupa@practisoft.cz> writes:

  PV> sub trim
  PV> {
  PV> my $t = shift;
  PV> $t =~s/^\s*|\s*$//sg;

that is known to be slower than doing left and right trim in separate
operations. google for this and you will see plenty of benchmarks

the /s modifier is useless as you don't use . in the regex.

replacing \s* will happen for every string which is wasteful. use \s+ so
you only replace when you have at least one \s.

  PV> sub ltrim
  PV> {
  PV> my $t = shift;
  PV> $t =~s/^\s*//s;
  PV> return $t;
  PV> }

  PV> sub rtrim
  PV> {
  PV> my $t = shift;
  PV> $t =~s/\s*$//s;
  PV> return $t;
  PV> }

the above comments apply to those as well except for the using two s///
ops.

if you use those subs a lot (and i never seem to need them) you might as
well write them well so they run faster.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: Sun, 02 Sep 2007 00:59:37 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to continue process without waiting for the output of system()?
Message-Id: <Xns999ED58485025asu1cornelledu@127.0.0.1>

Cod <zencod@gmail.com> wrote in news:1188570633.227615.319150
@l22g2000prc.googlegroups.com:

> Thank you all.
> 
> Use Narthring's method or xhos's method, there is one problem: BEFORE
> the calc closed, the programe won't exit. Just like this:
> 
> d:\>perl -e "fork or do {system('calc'); exi
> t}; $|=1; print 'hello' "
> hello
> 
> AFTER calc closed, below appears:
> d:\>
> 
> By Follow Ben Morrow say, I tried:
> d:\>perl -e "fork or do {system('calc'); exi
> t}; $|=1; print 'hello' "
> hello
> d:\>
> 
> This time it works just what I wanted.

If you want more control, you can use Win32::Process

#!/usr/bin/perl

use strict;
use warnings;

use FindBin qw( $Bin );

use Win32::Process;
use Win32;

sub ErrorMsg { Win32::FormatMessage( Win32::GetLastError() ) } 

my $calc_path = q{C:/WINDOWS/system32/calc.exe};

my $calc_process;

Win32::Process::Create(
    $calc_process,
    $calc_path,
    'calc',
    0,
    NORMAL_PRIORITY_CLASS,
    $Bin
) or die sprintf "Error starting '%s': %s", $calc_path, ErrorMsg();

sleep 3;

$calc_process->Suspend();

sleep 3;

$calc_process->Resume();

sleep 10;

$calc_process->Kill(0);

__END__


Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



------------------------------

Date: Sat, 01 Sep 2007 13:07:28 -0500
From: "J. F. Cornwall" <JCornwall@cox.net>
Subject: Re: Important Research Project
Message-Id: <YQhCi.27756$Pv4.21371@newsfe19.lga>

CBFalconer wrote:

> "E.D.G." wrote:
> 
>>This report is being posted to a number of Internet Newsgroups to
>>see if there are any experienced computer programmers who would
>>like to provide some assistance with an effort to develop a Perl
>>language computer program.
> 
> 
> Where is Perl described in the C standard?  This seems rather OT.

This is OT in every single group it was posted in...  Ed wants some 
gullible programmer to write him up a program for analyzing this mass of 
"data" he's been collecting, and use it to predict earthquakes.  He's 
been changing the requested language (Basic, Visual Basic, C, Fortran, 
Perl, and god only knows what else) and putting up the same request 
since *at least* the mid-1990s...

Jim


------------------------------

Date: Sat, 1 Sep 2007 19:50:22 -0600
From: "E.D.G." <edgrsprj@ix.netcom.com>
Subject: Re: Important Research Project
Message-Id: <13dk3p5mqdbvc7f@corp.supernews.com>

"E.D.G." <edgrsprj@ix.netcom.com> wrote in message
news:13dhm7uec5sp855@corp.supernews.com...

I have the Gnuplot graphics program running now with Windows XP. And it
looks like it will work for my application.
http://www.gnuplot.info




------------------------------

Date: Sat, 01 Sep 2007 16:00:33 -0700
From:  Udo <WeikEngOff@aol.com>
Subject: Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
Message-Id: <1188687633.079933.156580@50g2000hsm.googlegroups.com>

Hello,

I'm looking for an RegEx which replaces
  <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
with
<Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
<PatternN>

Word is just a single word (not a literal)
,_ is a delimiter (just a ',' and optionally Spaces), not so important
<Pattern0> ... <PatternN> are always the same patterns.
Literal is always the same.

Main problem is the correct grouping for the backreferences.

Thanks and greetings
Udo



------------------------------

Date: Sun, 02 Sep 2007 01:41:09 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
Message-Id: <5jubkrF1b2prU1@mid.individual.net>

Udo wrote:
> I'm looking for an RegEx which replaces
>   <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
> with
> <Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
> <PatternN>

C:\home>type test.pl
my $string = '<Word>,_<Pattern0>,_<Pattern1>,_<Pattern2>';
my @words = ('', 'foo', 'bar');
$string =~ s/,_\s*(<Pattern(\d+)>)/ $words[$2] $1/g;
print $string, "\n";

C:\home>test.pl
<Word>  <Pattern0> foo <Pattern1> bar <Pattern2>

C:\home>

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Sun, 2 Sep 2007 02:31:41 +0200
From: Anno Siegel <anno4000@radom.zrz.tu-berlin.de>
Subject: Re: Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
Message-Id: <5juejdF1arl0U1@mid.dfncis.de>

On 2007-09-02 01:00:33 +0200, Udo <WeikEngOff@aol.com> said:

> Hello,
> 
> I'm looking for an RegEx which replaces

Strictly speaking, a regex doesn't replace things.  A substitution 
operator (s///),
the first part of which is a regex, replaces things.

>   <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
> with
> <Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
> <PatternN>
> 
> Word is just a single word (not a literal)

What do you mean by "literal"?

> ,_ is a delimiter (just a ',' and optionally Spaces), not so important
> <Pattern0> ... <PatternN> are always the same patterns.
> Literal is always the same.
> 
> Main problem is the correct grouping for the backreferences.

It seems that in effect you want to change the delimiter to " Literal " 
everywhere
in the string.

    s/,\s*/ Literal /g;

Is that what you want?

Anno



------------------------------

Date: Sat, 01 Sep 2007 16:59:16 -0700
From:  lerameur <lerameur@yahoo.com>
Subject: Re: Searching in a line
Message-Id: <1188691156.491580.230410@r29g2000hsg.googlegroups.com>

On Sep 1, 3:31 am, "J=FCrgen Exner" <jurge...@hotmail.com> wrote:
> Benoit Lefebvre wrote:
> > Can also be made like that (if you don't want to use the shell
> > functions)
>
> > #!/usr/bin/perl-w
>
> > open (FILE,"<file.txt");
>
> > foreach $line (<FILE>) {
> >  if ($line =3D~ m/beth/) {
> >    @items =3D  split(",",$line);
> >    print $items[3] . "\n";
>
> Much better.
>
> jue

I tried using
use strict;
use warnings;

with the above program and it do not work.
can I know why

k



------------------------------

Date: Sun, 02 Sep 2007 01:36:16 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Searching in a line
Message-Id: <Xns999EDBBB84C79asu1cornelledu@127.0.0.1>

"Jürgen Exner" <jurgenex@hotmail.com> wrote in news:Sp2Ci.655$pm2.601
@trndny08:

> Benoit Lefebvre wrote:
>> Can also be made like that (if you don't want to use the shell
>> functions)
>>
>> #!/usr/bin/perl -w
>>
>> open (FILE,"<file.txt");
>>
>> foreach $line (<FILE>) {
>>  if ($line =~ m/beth/) {
>>    @items =  split(",",$line);
>>    print $items[3] . "\n";
> 
> Much better.

Not good, though.

use strict;

missing.

More importantly, this version still slurps the entire file.

while ( <FILE> ) {
   if ( /beth/ ) {
      print (split /,/)[3], "\n";
   }
}

Of course, open should be checked for failure, lexical filehandles and 
3-argument open are preferred and omitting $line and @items actually 
increases readability.

Sinan 

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



------------------------------

Date: 02 Sep 2007 02:26:40 GMT
From: xhoster@gmail.com
Subject: Re: Searching in a line
Message-Id: <20070901222643.747$XK@newsreader.com>

"Jürgen Exner" <jurgenex@hotmail.com> wrote:
> Benoit Lefebvre wrote:
> > Here is how I'd do it..
> >
> > #!/usr/bin/perl
> > @list = `cat file.txt | grep beth`;
>
> Useless use of cat

It isn't useless.  By doing it that way, it becomes trivially easy
change cat into gzcat if you want to.  Making things trivially easy
for me in the future is quite useful.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Sun, 2 Sep 2007 04:35:07 +0200
From: "Petr Vileta" <stoupa@practisoft.cz>
Subject: Re: Searching in a line
Message-Id: <fbd8bd$7i5$1@ns.felk.cvut.cz>

lerameur wrote:
> On Sep 1, 3:31 am, "Jürgen Exner" <jurge...@hotmail.com> wrote:
>> Benoit Lefebvre wrote:
>>> Can also be made like that (if you don't want to use the shell
>>> functions)
>>
>>> #!/usr/bin/perl-w
>>
>>> open (FILE,"<file.txt");
>>
>>> foreach $line (<FILE>) {
>>>  if ($line =~ m/beth/) {
>>>    @items =  split(",",$line);
>>>    print $items[3] . "\n";
>>
>> Much better.
>>
>> jue
>
> I tried using
> use strict;
> use warnings;
>
> with the above program and it do not work.
> can I know why
>
When you use "use strict" then you must use "my", "our" or "local" variable 
declaration.

open (FILE,"<file.txt");

foreach my $line (<FILE>) {
  if ($line =~ m/beth/) {
    my @items =  split(",",$line);
    print $items[3] . "\n";


-- 

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail 
from another non-spammer site please.)




------------------------------

Date: Sat, 1 Sep 2007 21:29:15 +0200
From: Anno Siegel <anno4000@radom.zrz.tu-berlin.de>
Subject: Re: Why use die?
Message-Id: <5jtssaF16hjmU1@mid.dfncis.de>

On 2007-09-01 16:34:28 +0200, "Petr Vileta" <stoupa@practisoft.cz> said:

> Bill H wrote:
>> I see examples like this in many programs, where you try to open a
>> file for write and you have a "die" statement after it:
>> 
>> open(TMP, '>'.$file) or die "Can't create file";
>> 
>> My question is, why would you use the "die" statement, ESPECIALLY in
>> the context of handling web based requests?
> [...stripped...]
>>  Again, in the context of a web based program, the
>> die "some text", if shown to a visitor in a browser, is not going to
>> help the programmer, and may just help someone figure out how to hack
>> your program (especially if you are showing directory structures (ex:
>> die "Could not write $file /home/sites/www.domain.com/users/
>> webmaster")
> 
> For web scripts I use some like
> 
> my $ok=1;
> open(TMP, '>'.$file) or sub {print "Can't create temporary 
> file.<br>Please contact webmaster"; $ok=0;}
> if($ok) { # then write something to file}
> # continue if possible

That won't work (and probably issue a warning).  Apart from a missing 
semicolon,
the code after "sub" will never be executed, so $ok would remain 1, 
whatever the
outcome of open.  If you changed "sub" to "do", things would look 
better, but it's still
more involved than it has to be.

    my $ok = open(...);
    if ( $ok ) {
        # write to file
    } else {
        print "Can't create temporary file.<br>Please contact webmaster";
    }
    # continue if possible

 ...but then the use of the variable $ok would be rather redundant.

Anno



------------------------------

Date: 01 Sep 2007 22:04:18 GMT
From: xhoster@gmail.com
Subject: Re: Why use die?
Message-Id: <20070901180420.751$8n@newsreader.com>

Bill H <bill@ts1000.us> wrote:
> Just to preface the following so I don't get the "daily WTF" or
> comments on code, I am not a "perl guru", I write code everyday in
> perl, and though good at it, I will be the 1st to admit I don't know
> everything.
>
> I see examples like this in many programs, where you try to open a
> file for write and you have a "die" statement after it:
>
> open(TMP, '>'.$file) or die "Can't create file";
>
> My question is, why would you use the "die" statement, ESPECIALLY in
> the context of handling web based requests?

So that if something goes wrong, you know what it is.

> If you are controlling all of the inputs, filename, permissions, etc
> and using flock, what is the need for a "die" statement?

If you never make mistakes, feel free to dispense with die, as well as
warnings and strict.  Just don't expect to get much sympathy from those
of use who don't assume we are perfect and who don't assume you are
perfect, either, but are often called on waste our time tracking down the
mistakes made by people who do apparently think they are perfect and
therefore didn't make provisions for their own errors.

> It is not accomplishing anything, other than telling you that it could
> not open the file, but again, if you control all the parameters then
> it should be able to open the file everytime. If not, then it would
> seem that the programmer did not anticipate everything that could stop
> his program from working as expected.

Like other people.  And the Spanish Inquisition.  No one ever thinks of
that.


> Also, when perl is used as web based program, the die "some text"
> isn't going to do anything except trigger a Server Error because you
> do not have it in an HTML wrapper, unless you are using Carp, or it is
> putting it in some log for later viewing (or I could be off on this,
> if so let me know).

If you are using CGI::Carp, then you are wrong.

> Again, in the context of a web based program, the
> die "some text", if shown to a visitor in a browser, is not going to
> help the programmer,

Unless the visitor happens to be working in the next cubicle over and pops
his head up and says "Hey, come look what just came up on your page."   Or
if the visitor is, you know, you.  Or if the visitor contacts you, as
CGI::Carp can be configured to instruct them to do.

> and may just help someone figure out how to hack
> your program (especially if you are showing directory structures (ex:
> die "Could not write $file /home/sites/www.domain.com/users/
> webmaster")

If you run a site that anyone really wants to hack into and the only thing
keeping your web site safe is the inability of a user to guess any of those
words above, then you are pretty much screwed anyway.

>
> It would seem more logical to have some error recovery routine instead
> of a simple die. Somethig like:
>
> open(TMP, '>'.$file) or try &errorHandlingRoutine;

That would be more logical only if errorHandlingRoutine actually does
something more logical.  Often, there just isn't anything more logical
to be done.  When there is, go ahead and do it.  You have my permission,
to the extend you need it.

>
> Where if there was a failure it would execute the
> &errorHandlingRoutine to attempt to fix the problem and then restart
> the same line. The &errorHandlingRoutine could then "die" if it
> couldn't fix it.

Sure.  But now you have jumped from "Nothing should ever go wrong, ever,
and there is no reason to think it ever could" to "When something does go
wrong, obviously you should magically be able to fix it."  Why go from
one extreme to the other without even considering stopping somewhere along
the way?

>
> The perldocs say that you can have die almost do as I suggested, but
> you can not return to the same line to try again.
>
> Now knowing perl and the millions of module available out there,
> something similar probably exists, but, if it does I have not seen
> it.

In the rare case in which I have something better to do than just die
in the event of an open failing, I do it.  It had never occurred to me
to look for a module that would tell me what *I* should do when *my* open
fails.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

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 815
**************************************


home help back first fref pref prev next nref lref last post