[32827] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4092 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 8 03:09:37 2013

Date: Sun, 8 Dec 2013 00:09:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 8 Dec 2013     Volume: 11 Number: 4092

Today's topics:
    Re: anything like cassandra or riak in perl? <visphatesjava@gmail.com>
    Re: anything like cassandra or riak in perl? <cwilbur@chromatico.net>
    Re: Extract sample (w/o replacement) <bill@todbe.com>
    Re: Extract sample (w/o replacement) <gamo@telecable.es>
    Re: Extract sample (w/o replacement) <bill@todbe.com>
    Re: Extract sample (w/o replacement) <ben@morrow.me.uk>
    Re: Extract sample (w/o replacement) <gamo@telecable.es>
    Re: Extract sample (w/o replacement) <gamo@telecable.es>
    Re: Extract sample (w/o replacement) <rweikusat@mobileactivedefense.com>
    Re: Extract sample (w/o replacement) <rweikusat@mobileactivedefense.com>
    Re: Extract sample (w/o replacement) <gamo@telecable.es>
        fork/exec question <dave@invalid.invalid>
    Re: fork/exec question <ben@morrow.me.uk>
    Re: Why is this sub removing newlines?? <bill@todbe.com>
    Re: Why is this sub removing newlines?? <janek_schleicher@yahoo.de>
    Re: Why is this sub removing newlines?? <derykus@gmail.com>
    Re: Why is this sub removing newlines?? <ben.usenet@bsb.me.uk>
    Re: Why is this sub removing newlines?? <derykus@gmail.com>
    Re: Why is this sub removing newlines?? <derykus@gmail.com>
    Re: Why is this sub removing newlines?? <rweikusat@mobileactivedefense.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 6 Dec 2013 22:16:55 -0800 (PST)
From: johannes falcone <visphatesjava@gmail.com>
Subject: Re: anything like cassandra or riak in perl?
Message-Id: <3319321c-688d-45ce-9128-80a5f9f5eca6@googlegroups.com>

yuk 
don't want mongo

Is there anything like www.prevayler.org or zodb in perl?

or anything like riak or cassandra but native perl?

would openMPI and perl work together to make a fast perl e commerce system?


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

Date: Sun, 08 Dec 2013 00:04:37 -0500
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: anything like cassandra or riak in perl?
Message-Id: <87r49ogcgq.fsf@new.chromatico.net>

>>>>> "jf" == johannes falcone <visphatesjava@gmail.com> writes:

    jf> Is there anything like www.prevayler.org or zodb in perl?

Don't you ever get tired of astroturfing for prevayler?

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Fri, 06 Dec 2013 15:46:58 -0800
From: "$Bill" <bill@todbe.com>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l7tnld$lhp$2@dont-email.me>

On 12/6/2013 03:34, gamo wrote:
>
> It's a better way to do this?
>
> my @matrix = 1..10_000_000;
>
>
> sub extract{
>      my $ind = int rand ($#matrix+1);
>      ($matrix[$ind],$matrix[-1]) = ($matrix[-1],$matrix[$ind]);
>      return pop @matrix;
> }
>
> The goal is to extact random elements without using shuffle, and
> ever removing them from the original list.

What's the swapping for ?
Wouldn't this work?:

sub extract {
return $matrix[int rand ($#matrix+1)];
}



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

Date: Sat, 07 Dec 2013 01:17:44 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l7tpfd$kon$1@speranza.aioe.org>

El 07/12/13 00:46, $Bill escribió:
>> The goal is to extact random elements without using shuffle, and
>> ever removing them from the original list.
>
> What's the swapping for ?
> Wouldn't this work?:
>
> sub extract {
> return $matrix[int rand ($#matrix+1)];
> }

No, this doesn't work because you could extract the same list element
many times, and this is not permitted.

Thanks



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

Date: Fri, 06 Dec 2013 18:57:20 -0800
From: "$Bill" <bill@todbe.com>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l7u2qa$9bq$2@dont-email.me>

On 12/6/2013 16:17, gamo wrote:
> El 07/12/13 00:46, $Bill escribió:
>>> The goal is to extact random elements without using shuffle, and
>>> ever removing them from the original list.
>>
>> What's the swapping for ?
>> Wouldn't this work?:
>>
>> sub extract {
>> return $matrix[int rand ($#matrix+1)];
>> }
>
> No, this doesn't work because you could extract the same list element
> many times, and this is not permitted.

A little unmentioned caveat.  :)





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

Date: Sat, 7 Dec 2013 03:10:47 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <n6gana-91u1.ln1@anubis.morrow.me.uk>


Quoth gamo <gamo@telecable.es>:
> 
> It's a better way to do this?
> 
> my @matrix = 1..10_000_000;
> 
> 
> sub extract{
> 	my $ind = int rand ($#matrix+1);

Peter has already shown you can better write this

    int rand @matrix

> 	($matrix[$ind],$matrix[-1]) = ($matrix[-1],$matrix[$ind]);
> 	return pop @matrix;

Perhaps

    my $rv = $matrix[$ind];
    $matrix[$ind] = pop @matrix;
    return $rv;

would be clearer?

Ben



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

Date: Sat, 07 Dec 2013 09:00:26 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l7ukiu$ktd$1@speranza.aioe.org>

El 07/12/13 04:10, Ben Morrow escribió:
>
> Quoth gamo <gamo@telecable.es>:
>>
>> It's a better way to do this?
>>
>> my @matrix = 1..10_000_000;
>>
>>
>> sub extract{
>> 	my $ind = int rand ($#matrix+1);
>
> Peter has already shown you can better write this
>
>      int rand @matrix
>
>> 	($matrix[$ind],$matrix[-1]) = ($matrix[-1],$matrix[$ind]);
>> 	return pop @matrix;
>
> Perhaps
>
>      my $rv = $matrix[$ind];
>      $matrix[$ind] = pop @matrix;
>      return $rv;
>
> would be clearer?
>
> Ben
>

It's clearer and a 14% faster.

  Thanks a lot


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

Date: Sat, 07 Dec 2013 11:45:05 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l7uu7k$fbe$1@speranza.aioe.org>

El 07/12/13 09:00, gamo escribió:
> El 07/12/13 04:10, Ben Morrow escribió:
>> Perhaps
>>
>>      my $rv = $matrix[$ind];
>>      $matrix[$ind] = pop @matrix;
>>      return $rv;
>>
>> would be clearer?
>>
>> Ben
>>
>
> It's clearer and a 14% faster.
>

But, what happens if $ind = $#matrix ?

The line $matrix[$ind] = pop @matrix could cause that element be repeated.

I'm afraid that's not a valid altenative.

Best regards




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

Date: Sat, 07 Dec 2013 17:20:21 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <87mwkcvaqy.fsf@sable.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 07/12/13 09:00, gamo escribió:
>> El 07/12/13 04:10, Ben Morrow escribió:
>>> Perhaps
>>>
>>>      my $rv = $matrix[$ind];
>>>      $matrix[$ind] = pop @matrix;
>>>      return $rv;
>>>
>>> would be clearer?
>>>
>>> Ben
>>>
>>
>> It's clearer and a 14% faster.
>>
>
> But, what happens if $ind = $#matrix ?
>
> The line $matrix[$ind] = pop @matrix could cause that element be
> repeated.

That's not a problem for $ind == $#matrix but for @matrix == 1: In this
case, the $matrix[$ind] = pop(@matrix) will reinsert the single remaning
element into the array whenever it is called, IOW, the size will stay 1.

It is possible to express this with slice notation as

@matrix[$ndx, -1] = @matrix[-1, $ndx]

At least one the computer where I tested this, this also seems to be
slightly faster with the difference being more marked as the input array
gets longer.




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

Date: Sat, 07 Dec 2013 17:22:37 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <87iov0van6.fsf@sable.mobileactivedefense.com>

Ben Morrow <ben@morrow.me.uk> writes:

[...]

> Perhaps
>
>     my $rv = $matrix[$ind];
>     $matrix[$ind] = pop @matrix;
>     return $rv;
>
> would be clearer?

Considering that this is buggy and at least three people (me included, I
found the issue by testing) didn't realize this, the answer would be:
Decidedly no.


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

Date: Sat, 07 Dec 2013 23:15:58 +0100
From: gamo <gamo@telecable.es>
Subject: Re: Extract sample (w/o replacement)
Message-Id: <l806n0$cok$1@speranza.aioe.org>

El 07/12/13 18:20, Rainer Weikusat escribió:
> It is possible to express this with slice notation as
>
> @matrix[$ndx, -1] = @matrix[-1, $ndx]
>
> At least one the computer where I tested this, this also seems to be
> slightly faster with the difference being more marked as the input array
> gets longer.

I get the inverse result: a swap of values is sightly faster

Thanks




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

Date: Sat, 7 Dec 2013 16:16:07 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: fork/exec question
Message-Id: <fV45K0OBJxbE-pn2-ddbBgLYHuimd@paddington.bear.den>

In C terms a fork/exec of foo execing bar, where both are executables,
will show in ps two processes foo and bar and IIRC the parent pid of 
bar will be foo's.

I have a perl script that forks and execs. 

my $pid = fork()
die "Can't fork $!" if ! defined $pid;

unless ( $pid )
{
  # child

  exec "mplayer.........

}

print "$pid\n";

 ....

Assume $pid is 123 If I run a ps I get

pid ppid prog
100 *** cmd
101 100 perl
123 100 perl
124 123 sh
125 124 sh
126 125 mplayer

Which is not what I would have expected. 

I would have expected

100 *** cmd
101 100 perl
123 101 mplayer

Or very close. Of course this could be another OS/2 oddity :-)

TIA
-- 
Regards
Dave Saville


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

Date: Sun, 8 Dec 2013 05:56:58 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: fork/exec question
Message-Id: <aaedna-4dd2.ln1@anubis.morrow.me.uk>


Quoth "Dave Saville" <dave@invalid.invalid>:
> In C terms a fork/exec of foo execing bar, where both are executables,
> will show in ps two processes foo and bar and IIRC the parent pid of 
> bar will be foo's.
> 
> I have a perl script that forks and execs. 
> 
> my $pid = fork()
> die "Can't fork $!" if ! defined $pid;
> 
> unless ( $pid )
> {
>   # child
> 
>   exec "mplayer.........
> 
> }
> 
> print "$pid\n";
> 
> ....
> 
> Assume $pid is 123 If I run a ps I get
> 
> pid ppid prog
> 100 *** cmd
> 101 100 perl
> 123 100 perl
> 124 123 sh
> 125 124 sh
> 126 125 mplayer
> 
> Which is not what I would have expected. 

You're using 1-arg exec (tut). This is roughly equivalent to

    sub exec {
        my ($cmd) = @_;

        if ($cmd =~ /\Q$&*(){}[]'";\\|?<>~`\n/) {
            exec "sh", "-c", $cmd;
        }
        else {
            exec split " ", $cmd;
        }
    }

that is: if using a shell will make a difference, run it with the shell,
otherwise split on whitespace and run it directly. This is exactly the
same behaviour as system(), where it more closely matches what a C
programmer would expect, and it allows you to include shell redirections
in your command. (And, in fact, you must have done so, or perl wouldn't
have used the shell.)

It's better in general to avoid this by using multi-arg system, which
always runs the command directly. Obviously in that case you have to do
any redirections yourself, between fork and exec; sometimes it's not
worth it.

Ben



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

Date: Fri, 06 Dec 2013 15:40:55 -0800
From: "$Bill" <bill@todbe.com>
Subject: Re: Why is this sub removing newlines??
Message-Id: <l7tna3$lhp$1@dont-email.me>

On 12/5/2013 13:30, Henry Law wrote:
> On 05/12/13 18:50, John Black wrote:
>> \s does not include newline, right?
>
> John, I would have agreed with you.  Plainly we're both wrong, as the follow-ups, not to mention the documentation, have shown, but what is it we're (mis)remembering?  There's some circumstance in which newline \n behaves differently from the other white space characters.

Not sure if this helps, but I searched the manual for
/white.*newline and /\\s and /\\s.*newline and it yielded:

perlintro
 ...
     More complex regular expressions
         You don't just have to match on fixed strings. In fact, you can match on
         just about anything you could dream of by using more complex regular
         expressions. These are documented at great length in perlre, but for the
         meantime, here's a quick cheat sheet:

             .                   a single character
             \s                  a whitespace character (space, tab, newline, ...)

perlglossary
 ...
     continuation
         The treatment of more than one physical "line" as a single logical line.
         "Makefile" lines are continued by putting a backslash before the
         "newline". Mail headers as defined by RFC 822 are continued by putting a
         space or tab *after* the newline. In general, lines in Perl do not need
         any form of continuation mark, because "whitespace" (including newlines)
         is gleefully ignored. Usually.

perlrequick
 ...
     Perl has several abbreviations for common character classes:

     *   \d is a digit and represents

             [0-9]

     *   \s is a whitespace character and represents

             [\ \t\r\n\f]

perlretut
 ...
     *   \s matches a whitespace character, the set [\ \t\r\n\f] and others
 ...
	The "[:digit:]", "[:word:]", and "[:space:]" correspond to the
     familiar "\d", "\w", and "\s" character classes.

perlfaq4
 ...
   How do I strip blank space from the beginning/end of a string?
     (contributed by brian d foy)

     A substitution can do this for you. For a single line, you want to replace
     all the leading or trailing whitespace with nothing. You can do that with a
     pair of substitutions.

             s/^\s+//;
             s/\s+$//;

     You can also write that as a single substitution, although it turns out the
     combined statement is slower than the separate ones. That might not matter
     to you, though.

             s/^\s+|\s+$//g;

     In this regular expression, the alternation matches either at the beginning
     or the end of the string since the anchors have a lower precedence than the
     alternation. With the "/g" flag, the substitution makes all possible
     matches, so it gets both. Remember, the trailing newline matches the "\s+",
     and the "$" anchor can match to the physical end of the string, so the
     newline disappears too. Just add the newline to the output, which has the
     added benefit of preserving "blank" (consisting entirely of whitespace)
     lines which the "^\s+" would remove all by itself.

             while( <> )
                     {
                     s/^\s+|\s+$//g;
                     print "$_\n";
                     }

     For a multi-line string, you can apply the regular expression to each
     logical line in the string by adding the "/m" flag (for "multi-line"). With
     the "/m" flag, the "$" matches *before* an embedded newline, so it doesn't
     remove it. It still removes the newline at the end of the string.

             $string =~ s/^\s+|\s+$//gm;

     Remember that lines consisting entirely of whitespace will disappear, since
     the first part of the alternation can match the entire string and replace it
     with nothing. If need to keep embedded blank lines, you have to do a little
     more work. Instead of matching any whitespace (since that includes a
     newline), just match the other whitespace.

             $string =~ s/^[\t\f ]+|[\t\f ]+$//mg;

perlrebackslash
 ...
     "\w" is a character class that matches any single *word* character (letters,
     digits, underscore). "\d" is a character class that matches any decimal
     digit, while the character class "\s" matches any whitespace character. New
     in perl 5.10.0 are the classes "\h" and "\v" which match horizontal and
     vertical whitespace characters.

perlrecharclass
 ...
    Whitespace
     "\s" matches any single character that is considered whitespace. The exact
     set of characters matched by "\s" depends on whether the source string is in
     UTF-8 format and the locale or EBCDIC code page that is in effect. If it's
     in UTF-8 format, "\s" matches what is considered whitespace in the Unicode
     database; the complete list is in the table below. Otherwise, if there is a
     locale or EBCDIC code page in effect, "\s" matches whatever is considered
     whitespace by the current locale or EBCDIC code page. Without a locale or
     EBCDIC code page, "\s" matches the horizontal tab ("\t"), the newline
     ("\n"), the form feed ("\f"), the carriage return ("\r"), and the space.
     (Note that it doesn't match the vertical tab, "\cK".) Perhaps the most
     notable possible surprise is that "\s" matches a non-breaking space only if
     the non-breaking space is in a UTF-8 encoded string or the locale or EBCDIC
     code page that is in effect has that character. See "Locale, EBCDIC, Unicode
     and UTF-8".
 ...
     Note that unlike "\s", "\d" and "\w", "\h" and "\v" always match the same
     characters, regardless whether the source string is in UTF-8 format or not.
     The set of characters they match is also not influenced by locale nor EBCDIC
     code page.

     One might think that "\s" is equivalent to "[\h\v]". This is not true. The
     vertical tab ("\x0b") is not matched by "\s", it is however considered
     vertical whitespace. Furthermore, if the source string is not in UTF-8
     format, and any locale or EBCDIC code page that is in effect doesn't include
     them, the next line (ASCII-platform "\x85") and the no-break space
     (ASCII-platform "\xA0") characters are not matched by "\s", but are by "\v"
     and "\h" respectively. If the source string is in UTF-8 format, both the
     next line and the no-break space are matched by "\s".

     The following table is a complete listing of characters matched by "\s",
     "\h" and "\v" as of Unicode 5.2.

     The first column gives the code point of the character (in hex format), the
     second column gives the (Unicode) name. The third column indicates by which
     class(es) the character is matched (assuming no locale or EBCDIC code page
     is in effect that changes the "\s" matching).

      0x00009        CHARACTER TABULATION   h s
      0x0000a              LINE FEED (LF)    vs
      0x0000b             LINE TABULATION    v
      0x0000c              FORM FEED (FF)    vs
      0x0000d        CARRIAGE RETURN (CR)    vs
      0x00020                       SPACE   h s
      0x00085             NEXT LINE (NEL)    vs  [1]
      0x000a0              NO-BREAK SPACE   h s  [1]
      0x01680            OGHAM SPACE MARK   h s
      0x0180e   MONGOLIAN VOWEL SEPARATOR   h s
      0x02000                     EN QUAD   h s
      0x02001                     EM QUAD   h s
      0x02002                    EN SPACE   h s
      0x02003                    EM SPACE   h s
      0x02004          THREE-PER-EM SPACE   h s
      0x02005           FOUR-PER-EM SPACE   h s
      0x02006            SIX-PER-EM SPACE   h s
      0x02007                FIGURE SPACE   h s
      0x02008           PUNCTUATION SPACE   h s
      0x02009                  THIN SPACE   h s
      0x0200a                  HAIR SPACE   h s
      0x02028              LINE SEPARATOR    vs
      0x02029         PARAGRAPH SEPARATOR    vs
      0x0202f       NARROW NO-BREAK SPACE   h s
      0x0205f   MEDIUM MATHEMATICAL SPACE   h s
      0x03000           IDEOGRAPHIC SPACE   h s

     [1] NEXT LINE and NO-BREAK SPACE only match "\s" if the source string is in
         UTF-8 format, or the locale or EBCDIC code page that is in effect
         includes them.

perl561delta
 ...
     Unicode support
         ...
         The Unicode character classes \p{Blank} and \p{SpacePerl} have been
         added. "Blank" is like C isblank(), that is, it contains only
         "horizontal whitespace" (the space character is, the newline isn't), and
         the "SpacePerl" is the Unicode equivalent of "\s" (\p{Space} isn't,
         since that includes the vertical tabulator character, whereas "\s"
         doesn't.)



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

Date: Sat, 07 Dec 2013 07:47:07 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: Why is this sub removing newlines??
Message-Id: <bgfujeFb8jkU1@mid.individual.net>

Am 06.12.2013 15:29, schrieb Rainer Weikusat:
> Janek Schleicher <janek_schleicher@yahoo.de> writes:
>> Am 05.12.2013 19:50, schrieb John Black:
>>> This sub is just supposed to strip off whitespace (at both the beginning and end of a
>>> string).  But its also stripping off newlines at the end of the string!  Why would that be?
>>> \s does not include newline, right?
>>>
>>>      sub trim()
>>>      {
>>>         my $string = shift;
>>>         $string =~ s/^\s+//;
>>>         $string =~ s/\s+$//;
>>>         return $string;
>>>      }
>>
>> BTW,
>> is there any reason to reinvent the wheel.
>> There are several CPAN-modules doing one of the most often needed Jobs:
>> - https://metacpan.org/pod/String::Trim
>> - https://metacpan.org/pod/String::Strip
>> - https://metacpan.org/pod/Text::Trim
>
> Using a gross oversimplification, ...

So, you also prefer to write
s/\r?\n$// instead of oversimplifying chomp; ?

I'd prefer instead to write 2 easy lines that express exactly what we 
intend to do

use WhateverModule::Trim|Strip;
 ...
trim($string);

to half a dozen lines in close to most scripts.

All I'd wonder is why trim/strip isn't a system command like chomp.

If we use a reg exp in program logic, usually they should do something 
that is special to our program, maybe s/blue/green/ or s/(\d+)/2*$1/ge.

Well, o.k., maybe I get religious here, so TMTOWTDI.


Greetings,
Janek



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

Date: Fri, 6 Dec 2013 23:56:44 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Why is this sub removing newlines??
Message-Id: <e7d2cdec-9cb2-4f8d-ae72-7891e64395ed@googlegroups.com>

On Thursday, December 5, 2013 1:56:46 PM UTC-8, John Black wrote:
> ...

> keep this in mind - I had wanted that trim function to not strip the newlines (and not add  any either if there wasn't one).  Should not be hard to workaround.  Thanks all.
> 
> 

Another option: a regex that'd handle any
trailing newline:  

$string  =~ s/ ^\s+ | \s+(?=\n|)$ //gx;


-- 
Charles DeRykus


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

Date: Sat, 07 Dec 2013 12:34:44 +0000
From: Ben Bacarisse <ben.usenet@bsb.me.uk>
Subject: Re: Why is this sub removing newlines??
Message-Id: <0.087048096a536edcfa97.20131207123444GMT.87haakkfff.fsf@bsb.me.uk>

"C.DeRykus" <derykus@gmail.com> writes:

> On Thursday, December 5, 2013 1:56:46 PM UTC-8, John Black wrote:
>> ...
>
>> keep this in mind - I had wanted that trim function to not strip the
>> newlines (and not add any either if there wasn't one).  Should not be
>> hard to workaround.  Thanks all.
>> 
>
> Another option: a regex that'd handle any
> trailing newline:  
>
> $string  =~ s/ ^\s+ | \s+(?=\n|)$ //gx;

Surely this strips the newline?

-- 
Ben.


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

Date: Sat, 7 Dec 2013 05:49:21 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Why is this sub removing newlines??
Message-Id: <48119f3e-6de5-4c2b-9009-6a57ec39c88f@googlegroups.com>

On Saturday, December 7, 2013 4:34:44 AM UTC-8, Ben Bacarisse wrote:
> "C.DeRykus" <derykus@gmail.com> writes:
> 
> > On Thursday, December 5, 2013 1:56:46 PM UTC-8, John Black wrote:>
> 
> >> keep this in mind - I had wanted that trim function to not strip the
> >> newlines (and not add any either if there wasn't one).  Should not be
> >> 
> > Another option: a regex that'd handle any
> > trailing newline:  
> >
> > $string  =~ s/ ^\s+ | \s+(?=\n|)$ //gx;
> 
> Surely this strips the newline?
> 

Indeed.  I was slipping off the end... I think,hope a redemptive tweak will do it:

$string =~ s/ ^s+ | \s++(?=\n) /gx;

--
Charles DeRykus 


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

Date: Sat, 7 Dec 2013 06:06:44 -0800 (PST)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Why is this sub removing newlines??
Message-Id: <4a78d25b-3f55-4173-ab8e-dabe3fd18296@googlegroups.com>

On Saturday, December 7, 2013 5:49:21 AM UTC-8, C.DeRykus wrote:
> On Saturday, December 7, 2013 4:34:44 AM UTC-8, Ben Bacarisse wrote:
> 
> > "C.DeRykus" <derykus@gmail.com> writes:
> 
> > 
> 
> > > On Thursday, December 5, 2013 1:56:46 PM UTC-8, John Black wrote:>
> 
> > 
> 
> > >> keep this in mind - I had wanted that trim function to not strip the
> 
> > >> newlines (and not add any either if there wasn't one).  Should not be
> 
> 
> > > Another option: a regex that'd handle any
> > > trailing newline:  
> 
> > > $string  =~ s/ ^\s+ | \s+(?=\n|)$ //gx;
> 
> > Surely this strips the newline?
> 
> Indeed.  I was slipping off the end... I think,hope a redemptive tweak will do it:
> 
> $string =~ s/ ^s+ | \s++(?=\n) /gx;
> 

Sorry, more redemption is needed.

-- 
Charles DeRykus



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

Date: Sat, 07 Dec 2013 16:51:46 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Why is this sub removing newlines??
Message-Id: <87r49ovc2l.fsf@sable.mobileactivedefense.com>

Janek Schleicher <janek_schleicher@yahoo.de> writes:
> Am 06.12.2013 15:29, schrieb Rainer Weikusat:
>> Janek Schleicher <janek_schleicher@yahoo.de> writes:
>>> Am 05.12.2013 19:50, schrieb John Black:
>>>> This sub is just supposed to strip off whitespace (at both the beginning and end of a
>>>> string).  But its also stripping off newlines at the end of the string!  Why would that be?
>>>> \s does not include newline, right?
>>>>
>>>>      sub trim()
>>>>      {
>>>>         my $string = shift;
>>>>         $string =~ s/^\s+//;
>>>>         $string =~ s/\s+$//;
>>>>         return $string;
>>>>      }
>>>
>>> BTW,
>>> is there any reason to reinvent the wheel.
>>> There are several CPAN-modules doing one of the most often needed Jobs:
>>> - https://metacpan.org/pod/String::Trim
>>> - https://metacpan.org/pod/String::Strip
>>> - https://metacpan.org/pod/Text::Trim
>>
>> Using a gross oversimplification, ...
>
> So, you also prefer to write
> s/\r?\n$// instead of oversimplifying chomp; ?

Since you apparently missed this: While there's doubtlessly many a
developer who is convinced to have invented something comparable to 'the
wheel', ie, a basic design which will remain in use for a few thousand
years, as soon as he managed to tack three lines of code together doing
something other than 'crash immediately', possibly even more so on CPAN,
using this comparison is either a case of hybris bordering serious
megalomania or just someone babbling along without spending much effort
on thinking about what he's actually saying, not the least because this
simile is actually wrong: Wheels come in many different kinds and even a
seriously reality-impaired mathead should have noticed the difference
between, say, tanks, push chairs, racing cars and pottery wheels. "But
can't you see they all round and rotate!" isn't much of a similarity at
the technical level.


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

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:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 4092
***************************************


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