[32688] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3910 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jun 3 14:55:55 2013

Date: Wed, 27 Mar 2013 02:17:15 -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, 27 Mar 2013     Volume: 11 Number: 3910

Today's topics:
    Re: 'Needless flexibilities' and structured records [ve <jwkrahn@example.com>
    Re: 'Needless flexibilities' and structured records [ve <derykus@gmail.com>
    Re: 'Needless flexibilities' and structured records [ve <ben@morrow.me.uk>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <whynot@pozharski.name>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <whynot@pozharski.name>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <klaus03@gmail.com>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <rweikusat@mssgmbh.com>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <ben@morrow.me.uk>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <whynot@pozharski.name>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <rweikusat@mssgmbh.com>
    Re: Issue: unexpected value in $2 (Perl 5.10.1) <rweikusat@mssgmbh.com>
        Perl method names are not tied to specific packages <rweikusat@mssgmbh.com>
        slots.pm <rweikusat@mssgmbh.com>
    Re: slots.pm <rweikusat@mssgmbh.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 25 Mar 2013 00:25:26 -0700
From: "John W. Krahn" <jwkrahn@example.com>
Subject: Re: 'Needless flexibilities' and structured records [very long]
Message-Id: <HZS3t.236729$O02.64701@newsfe18.iad>

Ben Morrow wrote:
>
> The one thing that is slightly weird is that $h{NAME,} calls NAME in
> scalar context, despite the comma.

The comma has no effect on context.  NAME, is inside $h{} and $h{} is a 
scalar (see the $ sigil).  If you had used a hash slice like @h{NAME,} 
then NAME would be in list context, with or without the comma.


> I think what is happening here is
> that the code which decides whether this is a single- or multi-dim
> subscript just looks at the number of expressions in its argument list,
> by which time that comma has long since disappeared.


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: Mon, 25 Mar 2013 04:28:49 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: 'Needless flexibilities' and structured records [very long]
Message-Id: <6f161e9c-e373-495b-9f9f-f467bc22830b@googlegroups.com>

On Sunday, March 24, 2013 11:05:41 PM UTC-7, Ben Morrow wrote:
> Quoth "C.DeRykus" <derykus@gmail.com>:
> 
> > 
> 
> > I regretted the post almost immediately for several reasons.  
> 
> > 
> 
> > However, I was surprised by these exceptions to these hash key 
> 
> > auto-quoting twists. It seems very odd that the trailing "," 
> 
> > (multi-dimensional array emulation) would work at all. Ditto
> 
> > for the pre-pended "+".  No idea why.
> 
> 
> 
> Anything inside the {} which doesn't match \w causes the contents to be
> 
> interpreted as an expression rather than a string. C<+NAME> and C<NAME,>
> 
> are both perfectly valid expressions which call the sub NAME: the + is
> 
> the unary 'term' operator, which does exactly nothing and is
> 
> occasionally useful to persuade Perl to choose one interpretation of an
> 
> ambiguous expression rather than another;the , is an ordinary trailing-comma-on-the-end-of-a-list, which is eaten by the parser and doesn't add
> 
> an extra element.
> 
> 
> The one thing that is slightly weird is that $h{NAME,} calls NAME in
> 
> scalar context, despite the comma. I think what is happening here is
> 
> that the code which decides whether this is a single- or multi-dim
> 
> subscript just looks at the number of expressions in its argument list,
> 
> by which time that comma has long since disappeared.
> 


Thanks for the insights. I actually knew "+" was a "persuader" 
but had forgotten I saw this in the doc:

    Use $hash{CONSTANT()} or $hash{+CONSTANT} to
    prevent the bareword quoting mechanism from kicking in. 

-- 
Charles DeRykus


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

Date: Mon, 25 Mar 2013 22:09:48 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: 'Needless flexibilities' and structured records [very long]
Message-Id: <ciu62a-ip72.ln1@anubis.morrow.me.uk>


Quoth jwkrahn@shaw.ca:
> Ben Morrow wrote:
> >
> > The one thing that is slightly weird is that $h{NAME,} calls NAME in
> > scalar context, despite the comma.
> 
> The comma has no effect on context.  NAME, is inside $h{} and $h{} is a 
> scalar (see the $ sigil).  If you had used a hash slice like @h{NAME,} 
> then NAME would be in list context, with or without the comma.

A perl-4-style multidimensional array subscript evaluates its arguments
in list context:

    sub ctx {
        say wantarray           ? "LIST"
            : defined wantarray ? "SCALAR"
            : "VOID"
    }

    $h{ctx,};
    $h{ctx,1};
    $h{ctx,()};
    $h{(),ctx};

This is a special case, and not in line with the usual rules about Perl
context. It happens because the optimiser (I think) transforms it into

    $h{join $;, ctx, 1};

Ben



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

Date: Mon, 25 Mar 2013 10:15:33 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <slrnkl01t5.40l.whynot@orphan.zombinet>

with <d7732a-ekc1.ln1@anubis.morrow.me.uk> Ben Morrow wrote:
*SKIP*

> Try this:
>
>     sub foo {
>         my ($x) = @_;
>         "y" =~ /(.)/;
>         say "$x:$_[0]";
>     }
>
>     "x" =~ /(.)/;
>     foo $1;
>
> Note that this can only happen if you access @_ directly (or by
> reference) after doing a pattern match, which is not common.

Slowly:  they said
(<909786a5-2ccd-48aa-bd6e-8a82e16340a5@r1g2000yql.googlegroups.com>)
that this:

#!/usr/bin/perl

use strict;
use warnings;
use feature qw{ say };

sub foo {
    my ($x) = @_;
    "y" =~ /(.)/;
    say "$x:$_[0]";
}

"x" =~ /(.)/;
foo $1;
say $1;
__END__

should be this:

	x:y
	y

or this:

	y:y
	y

instead it is this:

	{1894:4} [0:0]% perl ~/foo.H2vhcu.pl
	x:y
	x

p.s.  I've already seen what Devel::Peek::Dump said me, has nothing to
do with subs, @_, or aliasing.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Mon, 25 Mar 2013 10:29:59 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <slrnkl02o7.40l.whynot@orphan.zombinet>

with <86y5dc1fj8.fsf@red.stonehenge.com> Randal L. Schwartz wrote:
>>>>>> "Eric" == Eric Pozharski <whynot@pozharski.name> writes:
>
>Eric> sub holy_cosmos {
>Eric>     my $input = shift @_;
>Eric>     'holy_hallelujah' =~ m{holy_(.*)};
>Eric>     say $1 // 'missing'
>Eric> }
>
> This is bad.  Very bad.  If the match fails, it DOES NOT RESET the match
> variables.  So you have broken code from the getgo.

How do you reset match variables?

p.s.  I already feel much better and would like to pump it even further.

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Mon, 25 Mar 2013 14:24:03 -0700 (PDT)
From: Klaus <klaus03@gmail.com>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <19cf9717-8e1c-45e2-ab19-61776c27a4d9@p5g2000yqj.googlegroups.com>

On 25 mar, 09:29, Eric Pozharski <why...@pozharski.name> wrote:
> with <86y5dc1fj8....@red.stonehenge.com> Randal L. Schwartz wrote:
>
> >>>>>> "Eric" =3D=3D Eric Pozharski <why...@pozharski.name> writes:
>
> >Eric> sub holy_cosmos {
> >Eric> =A0 =A0 my $input =3D shift @_;
> >Eric> =A0 =A0 'holy_hallelujah' =3D~ m{holy_(.*)};
> >Eric> =A0 =A0 say $1 // 'missing'
> >Eric> }
>
> > This is bad. =A0Very bad. =A0If the match fails, it DOES NOT RESET the =
match
> > variables. =A0So you have broken code from the getgo.
>
> How do you reset match variables?

Usually you don't reset match variables at all.

The point is to test the match, if it fails, then you don't look at
match variables.

For example:

if ('holy_hallelujah' =3D~ m{holy_(.*)}) {
    say $1;
}
else {
   # do not look at match variables...
   say 'missing';
}


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

Date: Mon, 25 Mar 2013 21:32:58 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <87d2unqi5x.fsf@sapphire.mobileactivedefense.com>

Eric Pozharski <whynot@pozharski.name> writes:
> with <86y5dc1fj8.fsf@red.stonehenge.com> Randal L. Schwartz wrote:
>>>>>>> "Eric" == Eric Pozharski <whynot@pozharski.name> writes:
>>
>>Eric> sub holy_cosmos {
>>Eric>     my $input = shift @_;
>>Eric>     'holy_hallelujah' =~ m{holy_(.*)};
>>Eric>     say $1 // 'missing'
>>Eric> }
>>
>> This is bad.  Very bad.  If the match fails, it DOES NOT RESET the match
>> variables.  So you have broken code from the getgo.
>
> How do you reset match variables?

The next successful match will 'reset' them. In this case, this means
if the 'holy_hallelujah' match could fail (which it can't but a
'non-constant' one could), the line below would test the value the
last successful match put into $1.

As Klaus already wrote: The easiest way to stay clear of $1
 ... pitfalls is 'never use them except in code which is conditionally
executed when "the match" succeeded'.


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

Date: Mon, 25 Mar 2013 22:35:36 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <o2072a-ip72.ln1@anubis.morrow.me.uk>


Quoth Eric Pozharski <whynot@pozharski.name>:
> with <d7732a-ekc1.ln1@anubis.morrow.me.uk> Ben Morrow wrote:
> *SKIP*
> 
> > Try this:
> >
> >     sub foo {
> >         my ($x) = @_;
> >         "y" =~ /(.)/;
> >         say "$x:$_[0]";
> >     }
> >
> >     "x" =~ /(.)/;
> >     foo $1;
> >
> > Note that this can only happen if you access @_ directly (or by
> > reference) after doing a pattern match, which is not common.
> 
> Slowly:  they said
> (<909786a5-2ccd-48aa-bd6e-8a82e16340a5@r1g2000yql.googlegroups.com>)

Who they? (Google Groups appears to be so broken it can't look up a
msgid any more...)

> that this:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> use feature qw{ say };
> 
> sub foo {
>     my ($x) = @_;
>     "y" =~ /(.)/;
>     say "$x:$_[0]";
> }
> 
> "x" =~ /(.)/;
> foo $1;
> say $1;
> __END__
> 
> should be this:
> 
> 	x:y
> 	y
> 
> or this:
> 
> 	y:y
> 	y

Either of these results would be unexpected from the point of view of
the sub. You do not expect a pattern match to change your arguments.

> 
> instead it is this:
> 
> 	{1894:4} [0:0]% perl ~/foo.H2vhcu.pl
> 	x:y
> 	x
> 
> p.s.  I've already seen what Devel::Peek::Dump said me, has nothing to
> do with subs, @_, or aliasing.

Then you're using it wrong, because this has everything to do with @_
and aliasing.

The calling code passes $1 to foo. This means that, in foo, $_[0] is an
alias to $1. The first line of foo copies $_[0] to $x; $1 is "x" at this
point, so now $x = "x". The second line does a successful pattern match;
this changes $1, which also changes $_[0] (unexpectedly, from foo's
POV). foo then prints $x and $_[0], which are now different. Then foo
returns, which causes $1 to be restored to the value it had when foo was
called (this is what '$1 is dynamically scoped' means), so the print
after the sub prints the old value of $1.

Ben



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

Date: Tue, 26 Mar 2013 12:06:28 +0200
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <slrnkl2sp4.i2g.whynot@orphan.zombinet>

with <o2072a-ip72.ln1@anubis.morrow.me.uk> Ben Morrow wrote:
>
> Quoth Eric Pozharski <whynot@pozharski.name>:
>> with <d7732a-ekc1.ln1@anubis.morrow.me.uk> Ben Morrow wrote:
*SKIP*
>> (<909786a5-2ccd-48aa-bd6e-8a82e16340a5@r1g2000yql.googlegroups.com>)
> Who they? (Google Groups appears to be so broken it can't look up a
> msgid any more...)

Giganews filters googlegroups?  Amazing.  Count me in.

*SKIP*
>> p.s.  I've already seen what Devel::Peek::Dump said me, has nothing
>> to do with subs, @_, or aliasing.
> Then you're using it wrong, because this has everything to do with @_
> and aliasing.

#!/usr/bin/perl

use strict;
use warnings;
use feature qw{ say };

'holy_rainbow' =~ m{(.*)_(.*)};
say $1 // 'not found';

{
    'holy_hallelujah' =~ m{holy_(.*)};
    say $1 // 'missing';
}

say $1 // 'not found';

__END__

	{11145:5} [0:0]% perl ~/foo.4PonDs.pl 
	holy
	hallelujah
	holy

See?  And no @_ involved.

*CUT*

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


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

Date: Tue, 26 Mar 2013 12:26:02 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <87y5da73fp.fsf@sapphire.mobileactivedefense.com>

Eric Pozharski <whynot@pozharski.name> writes:

[...]

>> Then you're using it wrong, because this has everything to do with @_
>> and aliasing.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use feature qw{ say };
>
> 'holy_rainbow' =~ m{(.*)_(.*)};
> say $1 // 'not found';
>
> {
>     'holy_hallelujah' =~ m{holy_(.*)};
>     say $1 // 'missing';
> }
>
> say $1 // 'not found';
>
> __END__
>
> 	{11145:5} [0:0]% perl ~/foo.4PonDs.pl 
> 	holy
> 	hallelujah
> 	holy
>
> See?  And no @_ involved.

What is this now supposed to demonstrate? As documented, the $1
 ... are always implicitly local to the enclosing block. This subthread
was about passing them as arguments to a subroutine call which will
cause them to be aliased to @_ slots and a successful pattern match
executed by the subroutine will then change the arguments.

Slightly less contrived example
-----------------------------
sub move_suffix
{
    $_[0] =~ /_(.*)$/ and return "$_[1]_$1";
    return $_[1];
}

print(move_suffix('aaa_bbb', 'ccc'), "\n");

'hhh_jjj' =~ /(.*)_/ and print(move_suffix('aaa_bbb', $1), "\n");
 ------------------------------

This will print

ccc_bbb
bbb_bbb

instead of

ccc_bbb
hhh_bbb

Since assigning a scalar to another scalar implies copying the value, I
usually don't use scalar assignments in simple (frequently executed)
subroutines which don't need 'modifiable state variables' initialized
to the passed argument values.

    

    


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

Date: Tue, 26 Mar 2013 14:53:13 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Issue: unexpected value in $2 (Perl 5.10.1)
Message-Id: <87hajyw6ue.fsf@sapphire.mobileactivedefense.com>

Eric Pozharski <whynot@pozharski.name> writes:
> with <d7732a-ekc1.ln1@anubis.morrow.me.uk> Ben Morrow wrote:
> *SKIP*
>
>> Try this:
>>
>>     sub foo {
>>         my ($x) = @_;
>>         "y" =~ /(.)/;
>>         say "$x:$_[0]";
>>     }
>>
>>     "x" =~ /(.)/;
>>     foo $1;
>>
>> Note that this can only happen if you access @_ directly (or by
>> reference) after doing a pattern match, which is not common.
>
> Slowly:  they said
> (<909786a5-2ccd-48aa-bd6e-8a82e16340a5@r1g2000yql.googlegroups.com>)
> that this:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
> use feature qw{ say };
>
> sub foo {
>     my ($x) = @_;
>     "y" =~ /(.)/;
>     say "$x:$_[0]";
> }
>
> "x" =~ /(.)/;
> foo $1;
> say $1;
> __END__
>
> should be this:
>
> 	x:y
> 	y
>
> or this:
>
> 	y:y
> 	y

The statement was (paraphrased) that passing $1 ... as arguments to a
subroutine call wouldn't be safe. This actually demonstrated by the
example you posted because the value of $_[0] does change because of
the pattern match: If something other than $1 had been passed, the
output would be two times the value of that, not the value of the
passed arguments once followed by y.


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

Date: Tue, 26 Mar 2013 16:52:09 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Perl method names are not tied to specific packages
Message-Id: <87d2umw1c6.fsf@sapphire.mobileactivedefense.com>

That's something which probably also got lost in the noise: A
subroutine which expects 'an object' (a blessed reference) as argument
and does nothing with this object except invoking methods on it will
work with any object whose corresponding class ('package it is blessed
into _at the moment_') enables access to suitably name subroutines.

Example
-------------
package Named;

use Scalar::Util qw(blessed);

sub name
{
    return blessed($_[0]);
}

package Brazilian::Jaguar;

our @ISA = 'Named';

sub new
{
    return bless([], $_[0]);
}

sub what
{
    return 'feline predator';
}

package British::Jaguar;

our @ISA = 'Named';

sub new
{
    my $x;
    return bless(\$x, $_[0]);
}

sub what
{
    return 'car';
}

package main;

sub info
{
    printf("The %s is a %s\n.", $_[0]->name(), $_[0]->what());
}

my ($j0, $j1);

$j0 = Brazilian::Jaguar->new();
$j1 = British::Jaguar->new();

info($j0);
info($j1);
--------------

Considering that such a subroutine is totally independent of any
package providing object methods, it should not be tied to one because
the code could be useful for all kinds of objects. Such a subroutine
really belongs into a general, non-OO subroutine library.

NB: The obvious implication is that no 'method' which only expects to
work with a 'thing' providing access to a certain set of 'named
properties' actually belongs to the 'the class proper'.


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

Date: Mon, 25 Mar 2013 17:06:44 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: slots.pm
Message-Id: <87hajzs923.fsf@sapphire.mobileactivedefense.com>

After converting the presently largest Perl program I'm dealing with
(with 12,756 LOC of what the web developer would call 'business
logic' and no user interface whatsoever) to using this, I've created a
slightly 'enhanced' version of this and put it on a web page which is
accessible here:

http://mss-uk.mssgmbh.com/~rw/slots.html

Changes:

- use a file-scope my variable instead of state to make
  this usuable for a larger range of perl version

- argument checking code which croaks in case of 'unexpected uses'

- passes constant as first argument to constant::import


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

Date: Mon, 25 Mar 2013 23:30:27 +0000
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: slots.pm
Message-Id: <87obe7cb1o.fsf@sapphire.mobileactivedefense.com>

Rainer Weikusat <rweikusat@mssgmbh.com> writes:

[...]

> http://mss-uk.mssgmbh.com/~rw/slots.html
>

[...]

> - argument checking code which croaks in case of 'unexpected uses'

This attempt to 'improve' this code actually broke the 'just declare a
parent' case :-).


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

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


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