[32261] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3528 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 27 06:09:25 2011

Date: Thu, 27 Oct 2011 03:09:06 -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           Thu, 27 Oct 2011     Volume: 11 Number: 3528

Today's topics:
        Named nested subroutines? <rweikusat@mssgmbh.com>
    Re: Named nested subroutines? <jwkrahn@example.com>
    Re: Named nested subroutines? <rweikusat@mssgmbh.com>
    Re: Named nested subroutines? <ben@morrow.me.uk>
    Re: Named nested subroutines? <rweikusat@mssgmbh.com>
    Re: Named nested subroutines? <rweikusat@mssgmbh.com>
        picking up results of alternate captures? <bugbear@trim_papermule.co.uk_trim>
    Re: picking up results of alternate captures? <willem@toad.stack.nl>
    Re: picking up results of alternate captures? <bugbear@trim_papermule.co.uk_trim>
    Re: picking up results of alternate captures? <willem@toad.stack.nl>
    Re: picking up results of alternate captures? <ben@morrow.me.uk>
    Re: picking up results of alternate captures? <rweikusat@mssgmbh.com>
    Re: picking up results of alternate captures? <derykus@gmail.com>
    Re: search counterpart to sort? <tzz@lifelogs.com>
    Re: search counterpart to sort? <glex_no-spam@qwest-spam-no.invalid>
    Re: search counterpart to sort? <ben@morrow.me.uk>
    Re: Trying to get CGI selection to print <glex_no-spam@qwest-spam-no.invalid>
    Re: Trying to get CGI selection to print <hjp-usenet2@hjp.at>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 25 Oct 2011 21:22:45 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Named nested subroutines?
Message-Id: <8762jcakiy.fsf@sapphire.mobileactivedefense.com>

Are the other gotchas with named nested subroutines than the 'variable
will not stay shared' issue?


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

Date: Tue, 25 Oct 2011 19:57:35 -0700
From: "John W. Krahn" <jwkrahn@example.com>
Subject: Re: Named nested subroutines?
Message-Id: <AGKpq.878$D32.247@newsfe20.iad>

Rainer Weikusat wrote:
> Are the other gotchas with named nested subroutines than the 'variable
> will not stay shared' issue?

Named subroutines cannot be "nested" because their names are package 
variables and so do not follow lexical scoping rules.

The problem you are having is with lexical variables that are in the 
wrong scope.



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein


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

Date: Wed, 26 Oct 2011 11:50:27 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Named nested subroutines?
Message-Id: <87lis8f2mk.fsf@sapphire.mobileactivedefense.com>

"John W. Krahn" <jwkrahn@example.com> writes:
> Rainer Weikusat wrote:
>> Are the other gotchas with named nested subroutines than the 'variable
>> will not stay shared' issue?
>
> Named subroutines cannot be "nested" because their names are package
> variables and so do not follow lexical scoping rules.

To a degree, they can

----------
#!/usr/bin/perl

sub blubb
{
    return 'blubb';
}

sub blah
{
    local *blubb = sub { return 'yadda'; };
    return blubb();
}

print blubb(), " ", blah(), "\n";
----------

but I was asking about named subroutines defined
inside other named subroutines, eg

sub a
{
    sub b { return 1; }
    return 2;
}
	
specifically, because I'm using PL/Perl (Perl as PostgreSQL
procedural language) for a task which isn't entirely trivial anymore
and I would like to avoid having to put all my helper subs as
anonymous subroutines into %_SHARED and invoking them by going through
this hash. The PL/Perl documentation recommends against this,
calling it 'dangerous' and referring to perldiag for details. But the
only thing this manpage has to say to that is the 'variable will
not stay shared' issue[*] which doesn't affect me because I need to
put definitions of named subroutines into an anonymous Perl
subroutine (PL/Perl top-level construct) but all I want is creating
functions with ordinary names which can be invoked as usual.

[*] Not that this would really be true (tested with 5.10.1,
known to be working for some older version as well):

----------------
sub howl
{
    my $x;

    $x = 2;

    {
	sub yodel { return $x; }
    }
    
    return sub { ++$x; };
}

my $inc = howl();

print yodel, "\n";
$inc->();
print yodel, "\n";
$inc->();
print yodel, "\n";
-----------------

[rw@sapphire]/tmp $perl -w a.pl
Variable "$x" will not stay shared at a.pl line 9.
2
3
4


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

Date: Wed, 26 Oct 2011 14:04:39 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Named nested subroutines?
Message-Id: <taudnVfARdVaxDXTnZ2dnUVZ7s6dnZ2d@bt.com>


Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
> "John W. Krahn" <jwkrahn@example.com> writes:
> > Rainer Weikusat wrote:
> >> Are the other gotchas with named nested subroutines than the 'variable
> >> will not stay shared' issue?
> >
> > Named subroutines cannot be "nested" because their names are package
> > variables and so do not follow lexical scoping rules.
> 
> To a degree, they can
> 
> ----------
> #!/usr/bin/perl
> 
> sub blubb
> {
>     return 'blubb';
> }
> 
> sub blah
> {
>     local *blubb = sub { return 'yadda'; };
>     return blubb();
> }

This is dynamic, not lexical, scoping, but I expect you know that.

<snip>
> specifically, because I'm using PL/Perl (Perl as PostgreSQL
> procedural language) for a task which isn't entirely trivial anymore
> and I would like to avoid having to put all my helper subs as
> anonymous subroutines into %_SHARED and invoking them by going through
> this hash. The PL/Perl documentation recommends against this,
> calling it 'dangerous' and referring to perldiag for details. But the
> only thing this manpage has to say to that is the 'variable will
> not stay shared' issue[*] which doesn't affect me because I need to
> put definitions of named subroutines into an anonymous Perl
> subroutine (PL/Perl top-level construct) but all I want is creating
> functions with ordinary names which can be invoked as usual.

The ideal strategy here is to move the bulk of your Perl out to a
module. If you are or could be using Pg 9, you can 'require' the module
from plperl.on_init. This will make it available to all PL/Perl code,
trusted or untrusted. 9 also sets up trusted plperl so that 'require'
will succeed iff the file requested is already loaded, rather than
always failing, which means 'use' will work as usual if you preload the
module.

Otherwise, you're right, some level of hack is necessary. The 'cleanest'
hack looks like

    CREATE FUNCTION setup_subs () RETURNS void AS $$

        *first_sub = sub { ... };

        *second_sub = sub { ... };

    $$ LANGUAGE plperl;
    SELECT setup_subs();

or the equivalent with DO under 9. This avoids the warnings since the
subs are now anonymous closures, but still installs them in the symbol
table. If you rerun setup_subs it will reinstall new instances of the
closures which close over new copies of any outer lexicals.

The most important limitation of this approach, apart from the slightly
funky syntax, is that the subs don't know their own names. This means
modules like Carp which print sub names in diagnostics and modules like
mro which use a sub's name to work out which method they're dealing with
will fail. Since I don't think you can load modules into a plperl (not
u) interpreter before Pg 9 I doubt this will be a problem; if it turns
out to be, the usual solution is to use Sub::Name.

If this is, for some reason, impossible for you, you *can* simply use
something like

    CREATE FUNCTION setup_subs() RETURNS void AS $$
        sub foo { ... }
        sub bar { ... }
    $$ LANGUAGE plperl;

and ignore the warnings. (I don't think you can 'no warnings' them off
pre-9, since I don't think the server preloads warnings.pm, but I
haven't got a PL/Perlable Pg instance handy so ICBW.)

There shouldn't be any Perl issues apart from the not-shared lexicals,
and you can avoid those by, um, not attempting to share any lexicals.

The subs are actually defined at (Perl) compile time, so depending on
exactly when Pg passes the string to perl to be compiled you may find
you don't need to *run* setup_subs() at all, just CREATE it. You may
also find this is unpredictable and depends on exactly which perl
instance you end up with each time; I don't know the details of when Pg
passes which functions to which Perl instances, but it may be that if
you haven't run setup_subs() in this session this Perl interpreter won't
have seen the definition at all.

[Being an evil-minded person, it occurs to me at this point to wonder
what happens if you say
    
    CREATE FUNCTION setup_subs() RETURNS void AS $$
        };

        sub foo { ... }
        sub bar { ... }

        sub {
    $$ LANGUAGE plperl;

Little Bobby Tables, and so on...]

> [*] Not that this would really be true (tested with 5.10.1,
> known to be working for some older version as well):
> 
> ----------------
> sub howl
> {
>     my $x;
> 
>     $x = 2;
> 
>     {
> 	sub yodel { return $x; }
>     }
>     
>     return sub { ++$x; };
> }
> 
> my $inc = howl();
> 
> print yodel, "\n";
> $inc->();
> print yodel, "\n";
> $inc->();
> print yodel, "\n";

    my $again = howl();
    say(yodel), $again->() for 1..3;

You only called &howl once, so there was only ever one copy of $x and
therefore no problem. The problem arises when you call it again, so
there are now two copies of $x, so it's no longer clear which copy the
global &yodel should refer to.

You may not have realised this, but the 'variable will not stay shared'
warning is not exclusive to named subs. This

    sub howl {
        my $x = 2;
        BEGIN { *yodel = sub { $x } }
        sub { ++$x }
    }

gives the same warning and the same behaviour as your example, for the
same reasons: the sub that ends up as &yodel is only created once, and
at compile time, so the second-and-subsequent times &howl is run $x in
&howl is no longer the same variable as $x in &yodel.

Ben



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

Date: Wed, 26 Oct 2011 20:45:11 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Named nested subroutines?
Message-Id: <878vo7mta0.fsf@sapphire.mobileactivedefense.com>

Ben Morrow <ben@morrow.me.uk> writes:
> Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
>> "John W. Krahn" <jwkrahn@example.com> writes:
>> > Rainer Weikusat wrote:

[...]

>> sub howl
>> {
>>     my $x;
>> 
>>     $x = 2;
>> 
>>     {
>> 	sub yodel { return $x; }
>>     }
>>     
>>     return sub { ++$x; };
>> }
>> 
>> my $inc = howl();
>> 
>> print yodel, "\n";
>> $inc->();
>> print yodel, "\n";
>> $inc->();
>> print yodel, "\n";
>
>     my $again = howl();
>     say(yodel), $again->() for 1..3;
>
> You only called &howl once, so there was only ever one copy of $x and
> therefore no problem. The problem arises when you call it again, so
> there are now two copies of $x, so it's no longer clear which copy the
> global &yodel should refer to.

Unless it is redefined (which it isn't), it should continue to refer to the same $x it
originally referred to. After all, the closure created by a 2nd
invocation also won't "share" an $x with the closure created by the
first one. But I certainly misunderstood the warning ...



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

Date: Wed, 26 Oct 2011 22:44:15 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Named nested subroutines?
Message-Id: <87y5w7l974.fsf@sapphire.mobileactivedefense.com>

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

[...]

> The subs are actually defined at (Perl) compile time, so depending on
> exactly when Pg passes the string to perl to be compiled you may find
> you don't need to *run* setup_subs() at all, just CREATE it. You may
> also find this is unpredictable and depends on exactly which perl
> instance you end up with each time; I don't know the details of when Pg
> passes which functions to which Perl instances, but it may be that if
> you haven't run setup_subs() in this session this Perl interpreter won't
> have seen the definition at all.

For 'interpreted' procedural language routines, it just stores the
source code in the pg_proc table and passes it to the Perl
interpreter associated with the current session (for Linux/UNIX(*)),
this means 'the process handling the client connection') either when
the subroutine needs to be executed for the first time or after the
defining command was encountered.

> [Being an evil-minded person, it occurs to me at this point to wonder
> what happens if you say
>     
>     CREATE FUNCTION setup_subs() RETURNS void AS $$
>         };
>
>         sub foo { ... }
>         sub bar { ... }
>
>         sub {
>     $$ LANGUAGE plperl;
>
> Little Bobby Tables, and so on...]

The source code is really just wrapped into sub { }, cf

[rw@splittermine]/tmp $sudo -u postgres psql -U postgres -d mad_database
psql (8.4.8)
Type "help" for help.

mad_database=# create or replace function pltest() returns integer as $$                                                                                                                                                                                        }; sub aaa { return 12; } sub { return aaa();                                                                                                                                                                                                                   $$ language plperlu;
CREATE FUNCTION
mad_database=# select pltest();
 pltest 
--------
     12
(1 row)
mad_database=# \q
[rw@splittermine]/tmp $sudo -u postgres psql -U postgres -d mad_database
psql (8.4.8)
Type "help" for help.

mad_database=# create or replace function pltest2() returns integer as $$ return aaa(); $$ language plperlu;
CREATE FUNCTION
mad_database=# select pltest2();
ERROR:  error from Perl function "pltest2": Undefined subroutine &main::aaa called at line 1.
mad_database=# select pltest();
 pltest 
--------
     12
(1 row)

mad_database=# select pltest2();
 pltest2 
---------
      12
(1 row)


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

Date: Wed, 26 Oct 2011 10:50:57 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: picking up results of alternate captures?
Message-Id: <CPidnZP6DYOcRTrTnZ2dnUVZ8kCdnZ2d@brightview.co.uk>

If I have a regular expression using alternation to pick up
various alteranatives, is there an easy way to pick up
the match that "fired" for use?

In the case where I want the whole regexp, it's easy

e.g.

/(regexp1)|(regexp2)|(regexp3)/

This can be done by simply wrapping the whole thing
in parens, and using the result:

/((regexp1)|(regexp2)|(regexp3))/

my $s = $1;

But if the regexp1 contains "qualifiers" as well as the
desired target, it's harder.

For example, if regexp1 were (absurdly)

fred([1-4]+)george

with similar stuff for regexp2 and regexp3, I can't think
of a solution better than just "fishing" for a defined
variable in $1, $2, $3 (having used non capturing parens
for the alternation).

Roughly:

if(defined($1)) {
    $s = $1;
} elsif(defined($2)) {
    $s = $2;
} elsif(defined($3)) {
    $s = $3;
}

Any superior suggestions?

  BugBear


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

Date: Wed, 26 Oct 2011 09:59:15 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: picking up results of alternate captures?
Message-Id: <slrnjafmfj.bgc.willem@toad.stack.nl>

bugbear wrote:
) If I have a regular expression using alternation to pick up
) various alteranatives, is there an easy way to pick up
) the match that "fired" for use?
)
) In the case where I want the whole regexp, it's easy
)
) e.g.
)
) /(regexp1)|(regexp2)|(regexp3)/
)
) This can be done by simply wrapping the whole thing
) in parens, and using the result:
)
) /((regexp1)|(regexp2)|(regexp3))/
)
) my $s = $1;
)
) But if the regexp1 contains "qualifiers" as well as the
) desired target, it's harder.
)
) For example, if regexp1 were (absurdly)
)
) fred([1-4]+)george

Bad example, those parens are not needed.

) with similar stuff for regexp2 and regexp3, I can't think
) of a solution better than just "fishing" for a defined
) variable in $1, $2, $3 (having used non capturing parens
) for the alternation).
)
) Roughly:
)
) if(defined($1)) {
)     $s = $1;
) } elsif(defined($2)) {
)     $s = $2;
) } elsif(defined($3)) {
)     $s = $3;
) }
)
) Any superior suggestions?

Yes.  Named capturing parens.  Look it up in perldoc perlre.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Wed, 26 Oct 2011 11:43:58 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: picking up results of alternate captures?
Message-Id: <KeadnT5RVIbyeTrTnZ2dnUVZ7vGdnZ2d@brightview.co.uk>

Willem wrote:
> bugbear wrote:
> )
> ) For example, if regexp1 were (absurdly)
> )
> ) fred([1-4]+)george
>
> Bad example, those parens are not needed.

Really? In my (absurd) example I only want the digits captured,
but the "fred" and "george" qualify the match. How do I achieve
this without those parens?


> ) with similar stuff for regexp2 and regexp3, I can't think
> ) of a solution better than just "fishing" for a defined
> ) variable in $1, $2, $3 (having used non capturing parens
> ) for the alternation).
> )
> ) Roughly:
> )
> ) if(defined($1)) {
> )     $s = $1;
> ) } elsif(defined($2)) {
> )     $s = $2;
> ) } elsif(defined($3)) {
> )     $s = $3;
> ) }
> )
> ) Any superior suggestions?
>
> Yes.  Named capturing parens.  Look it up in perldoc perlre.

Feels like inelegant overkill, but, yes, it does the
right thing.

  BugBear


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

Date: Wed, 26 Oct 2011 10:54:48 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: picking up results of alternate captures?
Message-Id: <slrnjafpno.icn.willem@toad.stack.nl>

bugbear wrote:
) Willem wrote:
)> bugbear wrote:
)> )
)> ) For example, if regexp1 were (absurdly)
)> )
)> ) fred([1-4]+)george
)>
)> Bad example, those parens are not needed.
)
) Really? In my (absurd) example I only want the digits captured,
) but the "fred" and "george" qualify the match. How do I achieve
) this without those parens?

Oh I misunderstood, my mistake.
Now the question makes a lot more sense.

) Feels like inelegant overkill, but, yes, it does the
) right thing.

Why inelegant overkill?  It seems more elegant to me, and more
self-documenting as well (you don't have to count parens to see
 what is being captured, etc.) 

Another way could be the zero-width lookbehind and lookahead
assertions.  But those may be limited in what they can do.
Perhaps you feel those are more elegant?


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Wed, 26 Oct 2011 08:35:38 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: picking up results of alternate captures?
Message-Id: <s-qdnXDJKKY3kTXTnZ2dnUVZ8hGdnZ2d@bt.com>


Quoth bugbear <bugbear@trim_papermule.co.uk_trim>:
> Willem wrote:
> > bugbear wrote:
> 
<snip>
> > ) with similar stuff for regexp2 and regexp3, I can't think
> > ) of a solution better than just "fishing" for a defined
> > ) variable in $1, $2, $3 (having used non capturing parens
> > ) for the alternation).
> > )
> > ) Roughly:
> > )
> > ) if(defined($1)) {
> > )     $s = $1;
> > ) } elsif(defined($2)) {
> > )     $s = $2;
> > ) } elsif(defined($3)) {
> > )     $s = $3;
> > ) }
> > )
> > ) Any superior suggestions?
> >
> > Yes.  Named capturing parens.  Look it up in perldoc perlre.
> 
> Feels like inelegant overkill, but, yes, it does the
> right thing.

5.10 introduced (?|), which resets the capture group numbering for each
branch. There's an important bugfix to that feature in 5.12, though:
https://rt.perl.org/rt3/Public/Bug/Display.html?id=59734 (the fix went
into 5.12.0, but not any 5.10.x).

One situation where this is particularly useful is where you want the
captures as a list, rather than having to go through %+:

    perl -E'say join ":", "XYZ" =~ /(X) (?: (Y) | (W) ) (Z)/x'
    X:Y::Z
    perl -E'say join ":", "XYZ" =~ /(X) (?| (Y) | (W) ) (Z)/x'
    X:Y:Z

Ben



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

Date: Wed, 26 Oct 2011 14:42:48 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: picking up results of alternate captures?
Message-Id: <87lis7na1z.fsf@sapphire.mobileactivedefense.com>

bugbear <bugbear@trim_papermule.co.uk_trim> writes:
> If I have a regular expression using alternation to pick up
> various alteranatives, is there an easy way to pick up
> the match that "fired" for use?

      $LAST_PAREN_MATCH
       $+      The text matched by the last bracket of the last
       successful search pattern.  This is useful if you don't know
       which one of a set of alternative patterns matched.
       [perlvar(1)]
       


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

Date: Thu, 27 Oct 2011 00:41:05 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: picking up results of alternate captures?
Message-Id: <91ed4ae7-348a-4ba4-8a02-a139d5fc6c07@o14g2000yqh.googlegroups.com>

On Oct 26, 2:50=A0am, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
> If I have a regular expression using alternation to pick up
> various alteranatives, is there an easy way to pick up
> the match that "fired" for use?
>
> In the case where I want the whole regexp, it's easy
>
> e.g.
>
> /(regexp1)|(regexp2)|(regexp3)/
>
> This can be done by simply wrapping the whole thing
> in parens, and using the result:
>
> /((regexp1)|(regexp2)|(regexp3))/
>
> my $s =3D $1;
>
> But if the regexp1 contains "qualifiers" as well as the
> desired target, it's harder.
>
> For example, if regexp1 were (absurdly)
>
> fred([1-4]+)george
>
> with similar stuff for regexp2 and regexp3, I can't think
> of a solution better than just "fishing" for a defined
> variable in $1, $2, $3 (having used non capturing parens
> for the alternation).
>
> Roughly:
>
> if(defined($1)) {
> =A0 =A0 $s =3D $1;
>
> } elsif(defined($2)) {
> =A0 =A0 $s =3D $2;
> } elsif(defined($3)) {
> =A0 =A0 $s =3D $3;
> }
>

Another way maybe:

if('fooa2bar' =3D~ /(foo) (?:(a1)|(a2)) (?=3Dbar)/x)
{
    say "alternative match was: $^N";

--
Charles DeRykus



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

Date: Tue, 25 Oct 2011 13:13:48 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: search counterpart to sort?
Message-Id: <87r521f0z7.fsf@lifelogs.com>

On Tue, 18 Oct 2011 03:13:58 -0700 Jürgen Exner <jurgenex@hotmail.com> wrote: 

JE> bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
>> Perl (for ages) has had a helpful sort function
>> built in, based on a comparison subroutine.
>> 
>> Is there a corresponding  search function, either as core,
>> or a commonly available module?

JE> How about grep()? Just like sort() it takes a list and an
JE> expression/block as arguments and returns a list which contains all the
JE> sorted elements resp. all the 'found' elements.

Unfortunately grep() doesn't exit early so it's equally slow for 1 or
1000 matches.  A for() loop is often a more efficient solution when
looking for 1 or a few elements out of a large list.

Ted


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

Date: Tue, 25 Oct 2011 18:03:16 +0000
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: search counterpart to sort?
Message-Id: <4ea6f9e4$0$75666$815e3792@news.qwest.net>

On 10/25/11 17:13, Ted Zlatanov wrote:
> On Tue, 18 Oct 2011 03:13:58 -0700 Jürgen Exner<jurgenex@hotmail.com>  wrote:
>
> JE>  bugbear<bugbear@trim_papermule.co.uk_trim>  wrote:
>>> Perl (for ages) has had a helpful sort function
>>> built in, based on a comparison subroutine.
>>>
>>> Is there a corresponding  search function, either as core,
>>> or a commonly available module?
>
> JE>  How about grep()? Just like sort() it takes a list and an
> JE>  expression/block as arguments and returns a list which contains all the
> JE>  sorted elements resp. all the 'found' elements.
>
> Unfortunately grep() doesn't exit early so it's equally slow for 1 or
> 1000 matches.  A for() loop is often a more efficient solution when
> looking for 1 or a few elements out of a large list.

grep -m


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

Date: Tue, 25 Oct 2011 13:20:17 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: search counterpart to sort?
Message-Id: <UaSdnWVHP_J8YDvTnZ2dnUVZ7t-dnZ2d@bt.com>


Quoth "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>:
> On 10/25/11 17:13, Ted Zlatanov wrote:
> > On Tue, 18 Oct 2011 03:13:58 -0700 Jürgen Exner<jurgenex@hotmail.com>  wrote:
> >
> > JE>  How about grep()? Just like sort() it takes a list and an
> > JE>  expression/block as arguments and returns a list which contains all the
> > JE>  sorted elements resp. all the 'found' elements.
> >
> > Unfortunately grep() doesn't exit early so it's equally slow for 1 or
> > 1000 matches.  A for() loop is often a more efficient solution when
> > looking for 1 or a few elements out of a large list.
> 
> grep -m

Seems to work...

    ~% perl -E'say for grep -m /x/, qw/x y z/'
    x
    ~% perl -E'say for grep -m /x/, qw/x yx z/'
    x
    yx
    ~%

How about:

    ~% perl -E'say for grep -m $_ eq "x", qw/x yx z/'
    Search pattern not terminated at -e line 1.
    ~%

Ah, I knew it was too good to be true. :)

List::Util::first will return early.

Ben



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

Date: Tue, 25 Oct 2011 15:59:41 +0000
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Trying to get CGI selection to print
Message-Id: <4ea6dced$0$52261$815e3792@news.qwest.net>

On 10/24/11 20:25, Nene wrote:
> I'm still in the process of learning Perl CGI and OOP for Perl.
>
> I'm trying get what I selected to print to screen but It doesn't. I
> want to print $object->getNodes(), br;

When you're trying to learn something, simplify your problem by
doing one thing at a time.  Remove CGI from the equation.  Once
you get your program to print what you're looking for, *then* add
in the CGI parts.

 > print "Content-type: text/html\n\n";
[...]

If you're going to 'use CGI qw( :standard )' you can really
simplify your code, by actually using the methods it provides.
Oh, and print can accept a list.

e.g.

print	header(),
	start_html(
		-title   => 'ESP F5 Control',
		-bgcolor => 'black',
    		-text    => '#00ffff'),
	h1( 'DR F5 Control' ), "\n";



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

Date: Wed, 26 Oct 2011 20:03:54 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Trying to get CGI selection to print
Message-Id: <slrnjagisa.fdo.hjp-usenet2@hrunkner.hjp.at>

On 2011-10-24 21:13, Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth Nene <rodbass63@gmail.com>:
>> #!/usr/bin/perl -w
>
> You want
>
>     use warnings;
>
> instead of -w. warnings allows you to turn some warnings off for
> sections of your code where you need to,

This works with -w, too:

#!/usr/bin/perl -w
use strict;

my $x;

print "outside:\n";
print "-->$x<--\n";

{
    no warnings 'uninitialized';
    print "inside:\n";
    print "-->$x<--\n";
}

print "outside again:\n";
print "-->$x<--\n";
__END__
outside:
Use of uninitialized value $x in concatenation (.) or string at ./foo line 7.
--><--
inside:
--><--
outside again:
Use of uninitialized value $x in concatenation (.) or string at ./foo line 16.
--><--


	hp


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

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


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