[31372] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2624 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 7 06:09:31 2009

Date: Wed, 7 Oct 2009 03:09:08 -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           Wed, 7 Oct 2009     Volume: 11 Number: 2624

Today's topics:
        [regexp] Changing lines NOT containing a pattern <azeroiu@poupidoup.com>
    Re: [regexp] Changing lines NOT containing a pattern <tadmc@seesig.invalid>
    Re: [regexp] Changing lines NOT containing a pattern <azeroiu@poupidoup.com>
    Re: [regexp] Changing lines NOT containing a pattern <ben@morrow.me.uk>
    Re: [regexp] Changing lines NOT containing a pattern <tadmc@seesig.invalid>
    Re: [regexp] Changing lines NOT containing a pattern <jurgenex@hotmail.com>
    Re: CGI and UTF-8 <hhr-m@web.de>
        FAQ 4.68 Why does passing a subroutine an undefined ele <brian@theperlreview.com>
        FAQ 5.19 How can I reliably rename a file? <brian@theperlreview.com>
    Re: FAQ 5.19 How can I reliably rename a file? <kst-u@mib.org>
    Re: FAQ 5.19 How can I reliably rename a file? <ben@morrow.me.uk>
        FAQ 5.28 How can I read in an entire file all at once? <brian@theperlreview.com>
        FAQ 6.24 How do I match a regular expression that's in  <brian@theperlreview.com>
    Re: Once again: CGI help <jimsgibson@gmail.com>
    Re: Once again: CGI help <mstep@podiuminternational.org>
        regex - backwards? <john1949@yahoo.com>
    Re: sort function warning, variable will not stay share <justin.0908@purestblue.com>
    Re: sort function warning, variable will not stay share <cartercc@gmail.com>
    Re: sort function warning, variable will not stay share <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 06 Oct 2009 21:51:57 GMT
From: azrazer <azeroiu@poupidoup.com>
Subject: [regexp] Changing lines NOT containing a pattern
Message-Id: <4acbbbfd$0$30972$426a74cc@news.free.fr>

Hello,
I recently found an interesting issue on fr.comp.lang.perl and thought it 
would be good to share [since not answers were found until now]. So here 
it goes.

A file is slurped into a scalar variable (let say $my_text) [NOT AN 
ARRAY].
This $my_text now contains many lines of this form : <code>;<comments>.

The question is : Using a regexp (with mg flags) How to do the following 
for all lines at once ?
1/ if <code> contains a fixed word [let say WORD] then do not remove 
comments
2/ if <code> does nots contain WORD then remove comments

I have tried using look-forward and behind regexps but i guess it is not 
the good way of doing it. Also, i wanted to try using extended regexps 
like (?(COND)true|false) but i ended up drawing a blank...

Any help appreciated !
Thanks a lot !

azra


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

Date: Tue, 06 Oct 2009 17:09:50 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: [regexp] Changing lines NOT containing a pattern
Message-Id: <slrnhcnf61.o44.tadmc@tadmc30.sbcglobal.net>

azrazer <azeroiu@poupidoup.com> wrote:

> A file is slurped into a scalar variable (let say $my_text) [NOT AN 
> ARRAY].
> This $my_text now contains many lines of this form : <code>;<comments>.
>
> The question is : Using a regexp (with mg flags) 


Errrr, there is no need for the m//m flag, since there are
no ^ or $ anchors in the pattern...


> How to do the following 
> for all lines at once ?
> 1/ if <code> contains a fixed word [let say WORD] then do not remove 
> comments
> 2/ if <code> does nots contain WORD then remove comments


    $my_text =~ s/(.*)(;.*)/$1 . (index($1, 'WORD') == -1  ? '' : $2)/ge;


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: 06 Oct 2009 22:15:55 GMT
From: azrazer <azeroiu@poupidoup.com>
Subject: Re: [regexp] Changing lines NOT containing a pattern
Message-Id: <4acbc19b$0$30972$426a74cc@news.free.fr>

On Tue, 06 Oct 2009 17:09:50 -0500, Tad J McClellan wrote:

[snip]
>> The question is : Using a regexp (with mg flags) 
> Errrr, there is no need for the m//m flag, since there are no ^ or $
> anchors in the pattern...
Well, since the file is slurped, m flag might help finding line 
boundaries, isn't it ... ?
[snip]
>     $my_text =~ s/(.*)(;.*)/$1 . (index($1, 'WORD') == -1  ? '' :
>     $2)/ge;
Wow... so great, thanks a lot...
Much more easy [and definitely cleaner] than what i tried... 

Thanks !

azra


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

Date: Tue, 6 Oct 2009 23:28:50 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: [regexp] Changing lines NOT containing a pattern
Message-Id: <2ussp6-hhp1.ln1@osiris.mauzo.dyndns.org>


Quoth azrazer <azeroiu@poupidoup.com>:
> Hello,
> I recently found an interesting issue on fr.comp.lang.perl and thought it 
> would be good to share [since not answers were found until now]. So here 
> it goes.
> 
> A file is slurped into a scalar variable (let say $my_text) [NOT AN 
> ARRAY].
> This $my_text now contains many lines of this form : <code>;<comments>.
> 
> The question is : Using a regexp (with mg flags) How to do the following 
> for all lines at once ?
> 1/ if <code> contains a fixed word [let say WORD] then do not remove 
> comments
> 2/ if <code> does nots contain WORD then remove comments
> 
> I have tried using look-forward and behind regexps but i guess it is not 
> the good way of doing it. Also, i wanted to try using extended regexps 
> like (?(COND)true|false) but i ended up drawing a blank...

The obvious answer (besides the one Tad suggested, or simply splitting
twice on newlines and then on ';') would be

    s/(?<! WORD .*) ; .*//gx

but that doesn't work because perl doesn't do variable-length
look-behind. 5.10 implements a limited form of variable-length
lookbehind using the \K escape, which will work in this case:

    s/WORD .* \K ; .*//gx

The /m flag is irrelevant here, but the pattern relies on the *absence*
of the /s flag, to make sure /./ won't match across newlines.

You can also perform the obvious translation from a \K-using pattern to
one with an extra capture, which will work on any version of perl:

    s/( WORD .* ) ; .*/$1/gx

Ben



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

Date: Tue, 06 Oct 2009 19:44:20 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: [regexp] Changing lines NOT containing a pattern
Message-Id: <slrnhcno7n.pp7.tadmc@tadmc30.sbcglobal.net>

azrazer <azeroiu@poupidoup.com> wrote:
> On Tue, 06 Oct 2009 17:09:50 -0500, Tad J McClellan wrote:
>
> [snip]
>>> The question is : Using a regexp (with mg flags) 
>> Errrr, there is no need for the m//m flag, since there are no ^ or $
>> anchors in the pattern...
> Well, since the file is slurped, m flag might help finding line 
> boundaries, isn't it ... ?


No.

m//m ONLY affects the meaning of the ^ and $ anchors.

It is useless and does nothing when those anchors are not used.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Tue, 06 Oct 2009 17:57:41 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: [regexp] Changing lines NOT containing a pattern
Message-Id: <g9pnc5hur0sso06io58j1m1970rvkh38iu@4ax.com>

azrazer <azeroiu@poupidoup.com> wrote:
>A file is slurped into a scalar variable (let say $my_text) [NOT AN 
>ARRAY].

And there is your underlying basic problem. 

>This $my_text now contains many lines of this form : <code>;<comments>.
>
>The question is : Using a regexp (with mg flags) How to do the following 
>for all lines at once ?
>1/ if <code> contains a fixed word [let say WORD] then do not remove 
>comments
>2/ if <code> does nots contain WORD then remove comments

Unless you are interesting in an academic excercise or intellectual mind
twister it is _MUCH_ better to choose a data structure that fits the
problem description. 

You have an abstract concept of "lines" and you want to do something
with each line or don't want to do something with each line depending
upon if that line contains something. 
Then for haven's sake choose a data structure that represents such a
line!!! And convert your mega-string $my_text into an array of such
lines, e.g. using split(). This way your whole problem will collapse
into a simple 

	s/.../.../ unless m/.../;

Problem trivially solved.

jue


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

Date: Tue, 6 Oct 2009 17:12:31 +0200
From: Helmut Richter <hhr-m@web.de>
Subject: Re: CGI and UTF-8
Message-Id: <Pine.LNX.4.64.0910061656250.4423@lxhri01.lrz.lrz-muenchen.de>

On Sat, 3 Oct 2009, Ben Bullock wrote:

> This web page has a working example of what sln is discussing:
> 
> http://www.lemoda.net/perl/strip-diacritics/

This is an entirely different issue, albeit also an interesting one.

It occurs when data are expected in a restricted character set, e.g. in 
ISO-8859-1, but are input via a medium that allows UTF-8, e.g. a Web form. 
Then an encode function is needed that would not blow up with the first 
character that is not in the target character set.

It is not easy to provide a standard solution because the solution is 
dependent on the target character set, where US-ASCII is not the only 
conceivable one. For instance, real quotes (U+201C/D/E) should be mapped 
to ASCII quotes (U+0022) if and only if the target character set does not 
contain them (e.g. they are not contained in ISO-8859-1 but contained in 
its Windows cousin CP1252). 

The solution presented at that URL covers only the trivial (but useful 
because many characters are affected) case where stripping diacritics does 
the job. Many others (quotes, ordinary mathematical symbols, etc.) must be 
given reasonable substitutes by hand.

And, of course, different cultures may differ which substitutes do the job best.
For a German, an "ä" is in any case to be rendered as "ae", for Finnish or 
Swedish people, this is not the right thing to do.

-- 
Helmut Richter


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

Date: Wed, 07 Oct 2009 04:00:05 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 4.68 Why does passing a subroutine an undefined element in a hash create it?
Message-Id: <9nUym.17065$ma7.2552@newsfe04.iad>

This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

4.68: Why does passing a subroutine an undefined element in a hash create it?

    (contributed by brian d foy)

    Are you using a really old version of Perl?

    Normally, accessing a hash key's value for a nonexistent key will *not*
    create the key.

            my %hash  = ();
            my $value = $hash{ 'foo' };
            print "This won't print\n" if exists $hash{ 'foo' };

    Passing $hash{ 'foo' } to a subroutine used to be a special case,
    though. Since you could assign directly to $_[0], Perl had to be ready
    to make that assignment so it created the hash key ahead of time:

        my_sub( $hash{ 'foo' } );
            print "This will print before 5.004\n" if exists $hash{ 'foo' };

            sub my_sub {
                    # $_[0] = 'bar'; # create hash key in case you do this
                    1;
                    }

    Since Perl 5.004, however, this situation is a special case and Perl
    creates the hash key only when you make the assignment:

        my_sub( $hash{ 'foo' } );
            print "This will print, even after 5.004\n" if exists $hash{ 'foo' };

            sub my_sub {
                    $_[0] = 'bar';
                    }

    However, if you want the old behavior (and think carefully about that
    because it's a weird side effect), you can pass a hash slice instead.
    Perl 5.004 didn't make this a special case:

            my_sub( @hash{ qw/foo/ } );



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Tue, 06 Oct 2009 22:00:05 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 5.19 How can I reliably rename a file?
Message-Id: <F5Pym.25621$tG1.3642@newsfe22.iad>

This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

5.19: How can I reliably rename a file?

    If your operating system supports a proper mv(1) utility or its
    functional equivalent, this works:

            rename($old, $new) or system("mv", $old, $new);

    It may be more portable to use the File::Copy module instead. You just
    copy to the new file to the new name (checking return values), then
    delete the old one. This isn't really the same semantically as a
    rename(), which preserves meta-information like permissions, timestamps,
    inode info, etc.

    Newer versions of File::Copy export a move() function.



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Tue, 06 Oct 2009 17:38:10 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: FAQ 5.19 How can I reliably rename a file?
Message-Id: <lnvdisavil.fsf@nuthaus.mib.org>

PerlFAQ Server <brian@theperlreview.com> writes:
> 5.19: How can I reliably rename a file?
>
>     If your operating system supports a proper mv(1) utility or its
>     functional equivalent, this works:
>
>             rename($old, $new) or system("mv", $old, $new);
>
>     It may be more portable to use the File::Copy module instead. You just
>     copy to the new file to the new name (checking return values), then
>     delete the old one. This isn't really the same semantically as a
>     rename(), which preserves meta-information like permissions, timestamps,
>     inode info, etc.
>
>     Newer versions of File::Copy export a move() function.

How hold is File::Copy::move()?  Is the "Newer versions" qualifier
still necessary?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


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

Date: Wed, 7 Oct 2009 02:22:47 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 5.19 How can I reliably rename a file?
Message-Id: <747tp6-7cq1.ln1@osiris.mauzo.dyndns.org>


Quoth Keith Thompson <kst-u@mib.org>:
> 
> How hold is File::Copy::move()?  Is the "Newer versions" qualifier
> still necessary?

A little digging in http://perl5.git.perl.org/ says it was added in
1996. So, no.

Ben



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

Date: Tue, 06 Oct 2009 16:00:01 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 5.28 How can I read in an entire file all at once?
Message-Id: <5QJym.472850$Ta5.371741@newsfe15.iad>

This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

5.28: How can I read in an entire file all at once?

    You can use the File::Slurp module to do it in one step.

            use File::Slurp;

            $all_of_it = read_file($filename); # entire file in scalar
            @all_lines = read_file($filename); # one line per element

    The customary Perl approach for processing all the lines in a file is to
    do so one line at a time:

            open (INPUT, $file)     || die "can't open $file: $!";
            while (<INPUT>) {
                    chomp;
                    # do something with $_
                    }
            close(INPUT)            || die "can't close $file: $!";

    This is tremendously more efficient than reading the entire file into
    memory as an array of lines and then processing it one element at a
    time, which is often--if not almost always--the wrong approach. Whenever
    you see someone do this:

            @lines = <INPUT>;

    you should think long and hard about why you need everything loaded at
    once. It's just not a scalable solution. You might also find it more fun
    to use the standard Tie::File module, or the DB_File module's $DB_RECNO
    bindings, which allow you to tie an array to a file so that accessing an
    element the array actually accesses the corresponding line in the file.

    You can read the entire filehandle contents into a scalar.

            {
            local(*INPUT, $/);
            open (INPUT, $file)     || die "can't open $file: $!";
            $var = <INPUT>;
            }

    That temporarily undefs your record separator, and will automatically
    close the file at block exit. If the file is already open, just use
    this:

            $var = do { local $/; <INPUT> };

    For ordinary files you can also use the read function.

            read( INPUT, $var, -s INPUT );

    The third argument tests the byte size of the data on the INPUT
    filehandle and reads that many bytes into the buffer $var.



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Wed, 07 Oct 2009 10:00:06 GMT
From: PerlFAQ Server <brian@theperlreview.com>
Subject: FAQ 6.24 How do I match a regular expression that's in a variable?
Message-Id: <GEZym.37783$Bl2.2698@newsfe14.iad>

This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to 
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

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

6.24: How do I match a regular expression that's in a variable?

    
,
    (contributed by brian d foy)

    We don't have to hard-code patterns into the match operator (or anything
    else that works with regular expressions). We can put the pattern in a
    variable for later use.

    The match operator is a double quote context, so you can interpolate
    your variable just like a double quoted string. In this case, you read
    the regular expression as user input and store it in $regex. Once you
    have the pattern in $regex, you use that variable in the match operator.

            chomp( my $regex = <STDIN> );

            if( $string =~ m/$regex/ ) { ... }

    Any regular expression special characters in $regex are still special,
    and the pattern still has to be valid or Perl will complain. For
    instance, in this pattern there is an unpaired parenthesis.

            my $regex = "Unmatched ( paren";

            "Two parens to bind them all" =~ m/$regex/;

    When Perl compiles the regular expression, it treats the parenthesis as
    the start of a memory match. When it doesn't find the closing
    parenthesis, it complains:

            Unmatched ( in regex; marked by <-- HERE in m/Unmatched ( <-- HERE  paren/ at script line 3.

    You can get around this in several ways depending on our situation.
    First, if you don't want any of the characters in the string to be
    special, you can escape them with "quotemeta" before you use the string.

            chomp( my $regex = <STDIN> );
            $regex = quotemeta( $regex );

            if( $string =~ m/$regex/ ) { ... }

    You can also do this directly in the match operator using the "\Q" and
    "\E" sequences. The "\Q" tells Perl where to start escaping special
    characters, and the "\E" tells it where to stop (see perlop for more
    details).

            chomp( my $regex = <STDIN> );

            if( $string =~ m/\Q$regex\E/ ) { ... }

    Alternately, you can use "qr//", the regular expression quote operator
    (see perlop for more details). It quotes and perhaps compiles the
    pattern, and you can apply regular expression flags to the pattern.

            chomp( my $input = <STDIN> );

            my $regex = qr/$input/is;

            $string =~ m/$regex/  # same as m/$input/is;

    You might also want to trap any errors by wrapping an "eval" block
    around the whole thing.

            chomp( my $input = <STDIN> );

            eval {
                    if( $string =~ m/\Q$input\E/ ) { ... }
                    };
            warn $@ if $@;

    Or...

            my $regex = eval { qr/$input/is };
            if( defined $regex ) {
                    $string =~ m/$regex/;
                    }
            else {
                    warn $@;
                    }



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

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in 
perlfaq.pod.


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

Date: Tue, 06 Oct 2009 12:28:10 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Once again: CGI help
Message-Id: <061020091228109274%jimsgibson@gmail.com>

In article
<c2a8a596-9ca7-4c2f-8315-da5a3b015124@g6g2000vbr.googlegroups.com>,
Marek <mstep@podiuminternational.org> wrote:

> Hello all!
> 
> Sorry for this long posting. I boiled down my script as far as I
> could. But it is still very long!

It is too long. If you expect to get help with your problem(s), you
need to write a shorter program that exhibits the problem you are
having.

> 
> My Questions:
> 
> 1. How do I create the $order_date. I am doing something wrong here

What is $order_date supposed to contain?

> 2. How do I check, whether the $order_date was changed against the
> default: 01/Jan/2009 - 00:00. The $order_date would be later
> obligatory (must    => 1; it is commented out)

You can compare strings with the eq and ne operators:

  if( $order_date ne $default_date ) {
    # $order_date has been changed
  }

> 3. How to pass the form elements to the formmail.pl from :
> http://nms-cgi.sourceforge.net/ ???

You would define the query string that formmail expects from an
appropriate web page and call formmail. However, that is probably not
what you want to do. You would be better off combining your script with
formmail. If all you want to do is email some information somewhere,
you don't need all of formmail. Just extract the parts of formmail that
send email (paying attention to the security measures that formmail
uses) and add those to your program. Or use one of the many other ways
to send email from a Perl program.

> 
> Thank you all for your patience.

My patience is limited.

-- 
Jim Gibson


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

Date: Wed, 7 Oct 2009 01:30:26 -0700 (PDT)
From: Marek <mstep@podiuminternational.org>
Subject: Re: Once again: CGI help
Message-Id: <e0076b7b-03eb-432c-92d5-6c7a1ceb3dc8@33g2000vbe.googlegroups.com>


>
> My patience is limited.
>


So why are you answering? I am beginner, and your answers you gave are
not really helpful!


> What is $order_date supposed to contain?

For that reason I posted the very long (sorry again!) script:

push( @dates_day, $value );    # but this is not working!


> You can compare strings with the eq and ne operators:
>
>   if( $order_date ne $default_date ) {
>     # $order_date has been changed
>   }
>

This I know! Even as a beginner.


> you don't need all of formmail. Just extract the parts of formmail that
send email

Not really helpful either. Considering the logic of my script, I have
no place for <form method="post" action="../cgi-bin/FormMail.pl">. How
do I hand over the form elements after sub formular_check to
FormMail.pl ?

There are people jumping on every excuse, to make them smaller to seem
themselves bigger.


marek




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

Date: Wed, 7 Oct 2009 08:19:00 +0100
From: "John" <john1949@yahoo.com>
Subject: regex - backwards?
Message-Id: <hahfd0$lc5$1@news.albasani.net>

Hi

$x =~s  |a|b|;

That will replace the first 'a' by 'b' in $x.

Is there any obvious tweek so that it replaces the last occurrence of 'a'?

Regards
John





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

Date: Tue, 06 Oct 2009 15:57:30 -0000
From: Justin C <justin.0908@purestblue.com>
Subject: Re: sort function warning, variable will not stay shared
Message-Id: <1117.4acb68ea.959b7@zem>

On 2009-10-02, ccc31807 <cartercc@gmail.com> wrote:
> [Fri Oct  2 10:26:09 2009] CONSOLE.pm: Variable "$content" will not
> stay shared at CONSOLE.pm line 164.

GIYF

I started typing "variable w" into Google and "variable will not stay
shared" was the fourth option - it only moved higher the more I typed.
In what way does Google not resolve this problem?

	Justin.

-- 
Justin C, by the sea.


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

Date: Tue, 6 Oct 2009 12:48:27 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: sort function warning, variable will not stay shared
Message-Id: <3d60a7ce-1d88-488e-a367-d8515fe715bb@v2g2000vbb.googlegroups.com>

On Oct 6, 11:57=A0am, Justin C <justin.0...@purestblue.com> wrote:
> In what way does Google not resolve this problem?

I googled the warning message and probably read the same things you
did. That wasn't really the point. What I failed to understand was
that you could use lexical variables in a sort block.

The other issue was whether the sort function would ever misbehave.
This wasn't addressed directly, but I understood implicitly that the
sort function would do its job even with the warning message.

CC


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

Date: Tue, 6 Oct 2009 21:14:52 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: sort function warning, variable will not stay shared
Message-Id: <s2lsp6-9ro1.ln1@osiris.mauzo.dyndns.org>


Quoth ccc31807 <cartercc@gmail.com>:
> On Oct 6, 11:57 am, Justin C <justin.0...@purestblue.com> wrote:
> > In what way does Google not resolve this problem?
> 
> I googled the warning message and probably read the same things you
> did. That wasn't really the point. What I failed to understand was
> that you could use lexical variables in a sort block.
> 
> The other issue was whether the sort function would ever misbehave.
> This wasn't addressed directly, but I understood implicitly that the
> sort function would do its job even with the warning message.

I think you are still misunderstanding the problem. sort *will* *not* do
its job correctly if you pass it a function that has closed over the
wrong variables. Consider

    perl -wE'
        for (-1,1) {
            my @x = ($_, $_*2, $_*3);
            sub mycmp { $x[$a] <=> $x[$b] }
            say join ",", sort mycmp 0..2;
        }'
    2,1,0
    2,1,0

vs.

    perl -wE'
        for (-1,1) {
            my @x = ($_, $_*2, $_*3);
            my $mycmp = sub { $x[$a] <=> $x[$b] };
            say join ",", sort $mycmp 0..2;
        }'
    2,1,0
    0,1,2

In the first example, sort returns the *same* results each time, even
though the arrays it is supposed to be comparing are different. This is
because &mycmp is incorrectly using the array from the first time
through the loop, which is exactly what the warning is warning you
about.

Ben



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

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


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