Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

What would having a & prototype after the first position break?

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


c.nehren/p5p at shadowcat

Jul 3, 2012, 9:58 PM

Post #1 of 11 (158 views)
Permalink
What would having a & prototype after the first position break?

Recently I've been working on a DSL of sorts that would be more
succinctly expressed if I could have a prototype like ($&&). What would
break if we allowed the & prototype after the first position? To make
the proposed feature concrete, it would enable us to have syntax like
(courtesy of LeoNerd):

generic_sort { $a <=> $b } { length $_ } @strings;

or (from my own code):

loop host 'shadowcat' => { do_stuff; };

I'm fine with doing the work to add this feature. I would just like to
be sure I don't break half of CPAN with my efforts.

Thoughts? Comments? Rallying praise? Rotten tomatoes?

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/


c.nehren/p5p at shadowcat

Jul 3, 2012, 8:53 PM

Post #2 of 11 (151 views)
Permalink
What would having a & prototype after the first position break? [In reply to]

Recently I've been working on a DSL of sorts that would be more
succinctly expressed if I could have a prototype like ($&&). What would
break if we allowed the & prototype after the first position? To make
the proposed feature concrete, it would enable us to have syntax like
(courtesy of LeoNerd):

generic_sort { $a <=> $b } { length $_ } @strings;

or (from my own code):

loop host 'shadowcat' => { do_stuff; };

I'm fine with doing the work to add this feature. I would just like to
be sure I don't break half of CPAN with my efforts.

Thoughts? Comments? Rallying praise? Rotten tomatoes?

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/


c.nehren/p5p at shadowcat

Jul 4, 2012, 10:27 AM

Post #3 of 11 (150 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wed, Jul 04, 2012 at 17:54:40 +0200 , demerphq wrote:
> On 4 July 2012 06:58, Chris Nehren <c.nehren/p5p [at] shadowcat> wrote:
> > Recently I've been working on a DSL of sorts that would be more
> > succinctly expressed if I could have a prototype like ($&&). What would
> > break if we allowed the & prototype after the first position? To make
> > the proposed feature concrete, it would enable us to have syntax like
> > (courtesy of LeoNerd):
> >
> > generic_sort { $a <=> $b } { length $_ } @strings;
> >
> > or (from my own code):
> >
> > loop host 'shadowcat' => { do_stuff; };
> >
> > I'm fine with doing the work to add this feature. I would just like to
> > be sure I don't break half of CPAN with my efforts.
> >
> > Thoughts? Comments? Rallying praise? Rotten tomatoes?
>
> How is the interpreter to know that those arent hashes?
>
> Yves

Because I asked it to with a (&&) or ($&) prototype.

To be clear, I am proposing a change to perl to enable that &
prototype after the initial position--this is expressly *not* supposed
to work with present perl. It is more consistent, at least to me, that
the first position of a prototype not be special with regard to the &
prototype specifier.

Given this:

sub foo(&) { ... }

foo { ... };

How does the interpreter know that the {} construct after foo isn't a
hash? The prototype says so. In present perl, this is valid:

use strict;
use warnings;
sub foo(&) { my ($code) = @_; $code->(); }
foo { qw/foo bar baz quux/ };

It doesn't really do anything, but it doesn't explode, either.[0] We're
obviously passing what should be interpreted as a hashref to foo. Maybe
you meant, "How can we add error-checking to this handle the case that
the {} thing passed in is actually a hash ref and not a code ref?" In
that case, we already have syntax for disambiguating between code and
hash refs. And in present perl we don't explode if the thing passed to a
sub(&) is a hashref, as demonstrated above. Why should it be any
different if we change perl to accept a & prototype after the first
position?

[0]: IMO it should (or should at least warn), but that's a discussion
for another thread.

--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/


dmcbride at cpan

Jul 4, 2012, 11:23 AM

Post #4 of 11 (150 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wednesday July 4 2012 1:27:27 PM Chris Nehren wrote:
> On Wed, Jul 04, 2012 at 17:54:40 +0200 , demerphq wrote:
> > On 4 July 2012 06:58, Chris Nehren <c.nehren/p5p [at] shadowcat> wrote:
> > > Recently I've been working on a DSL of sorts that would be more
> > > succinctly expressed if I could have a prototype like ($&&). What would
> > > break if we allowed the & prototype after the first position? To make
> > > the proposed feature concrete, it would enable us to have syntax like
> > > (courtesy of LeoNerd):
> > >
> > > generic_sort { $a <=> $b } { length $_ } @strings;
> > >
> > > or (from my own code):
> > >
> > > loop host 'shadowcat' => { do_stuff; };
> > >
> > > I'm fine with doing the work to add this feature. I would just like to
> > > be sure I don't break half of CPAN with my efforts.
> > >
> > > Thoughts? Comments? Rallying praise? Rotten tomatoes?
> >
> > How is the interpreter to know that those arent hashes?
> >
> > Yves
>
> Because I asked it to with a (&&) or ($&) prototype.

sub do_stuff($;&); # implementation not important, I think.

my %foo;
my $foo;
do_stuff $foo { 'something here' };

Now, is that passing in $foo as the first parameter and a code ref that returns
a static string as the second, or passing in a value contained in %foo as that
first parameter, and no code ref?

Don't get me wrong, I've wanted to be able to put & somewhere other than the
first parameter on many occassions. But first the syntax has to not surprise
the reader[0], and we use enough squiggles already for so many different
purposes, especially the braces, so a bit of care might be required.

[0] Surprise with new syntax is fine. Surprise with something that could be
one of multiple syntaxes, probably not so much.
Attachments: signature.asc (0.19 KB)


rs at 474

Jul 4, 2012, 12:33 PM

Post #5 of 11 (150 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wed, 2012-07-04 at 12:23 -0600, Darin McBride wrote:
> sub do_stuff($;&); # implementation not important, I think.
>
> my %foo;
> my $foo;
> do_stuff $foo { 'something here' };
>
> Now, is that passing in $foo as the first parameter and a code ref that returns
> a static string as the second, or passing in a value contained in %foo as that
> first parameter, and no code ref?
>
> Don't get me wrong, I've wanted to be able to put & somewhere other than the
> first parameter on many occassions. But first the syntax has to not surprise
> the reader[0], and we use enough squiggles already for so many different
> purposes, especially the braces, so a bit of care might be required.
>
> [0] Surprise with new syntax is fine. Surprise with something that could be
> one of multiple syntaxes, probably not so much.

I'm not sure you'd even need to make it optional, since perl would have
to decide if the '{' belongs to the '$foo' and is a hash access or is a
whole new argument.

However, only '&' prototypes seem to not require a comma afterwards, so
shouldn't the above always parse as hash access? I'd assume the code for
the above prototype would be

do_stuff $foo, { 'something here' };

Or am I missing something?

regards,
Robert

--
Robert 'phaylon' Sedlacek

Perl 5 Consultant for
Shadowcat Systems Limited - http://shadowcat.co.uk/


c.nehren/p5p at shadowcat

Jul 4, 2012, 12:55 PM

Post #6 of 11 (153 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wed, Jul 04, 2012 at 21:33:04 +0200 , Robert Sedlacek wrote:
> On Wed, 2012-07-04 at 12:23 -0600, Darin McBride wrote:
> > sub do_stuff($;&); # implementation not important, I think.
> >
> > my %foo;
> > my $foo;
> > do_stuff $foo { 'something here' };
> >
> > Now, is that passing in $foo as the first parameter and a code ref that returns
> > a static string as the second, or passing in a value contained in %foo as that
> > first parameter, and no code ref?
> >
> > Don't get me wrong, I've wanted to be able to put & somewhere other than the
> > first parameter on many occassions. But first the syntax has to not surprise
> > the reader[0], and we use enough squiggles already for so many different
> > purposes, especially the braces, so a bit of care might be required.
> >
> > [0] Surprise with new syntax is fine. Surprise with something that could be
> > one of multiple syntaxes, probably not so much.
>
> I'm not sure you'd even need to make it optional, since perl would have
> to decide if the '{' belongs to the '$foo' and is a hash access or is a
> whole new argument.
>
> However, only '&' prototypes seem to not require a comma afterwards, so
> shouldn't the above always parse as hash access? I'd assume the code for
> the above prototype would be
>
> do_stuff $foo, { 'something here' };
>
> Or am I missing something?

I do believe you're correct there, Robert. To demonstrate with existing
code:

$ cat p
#!/usr/bin/env perl

sub foo (&;$) {my ($code, $arg) = @_; $code->($arg) }

foo { print "$_[0]\n" } 'hello'
$ perl p
hello
$

More interestingly, if we place a , after the block in the invocation,
strange things happen:

$ cat p
#!/usr/bin/env perl

sub foo (&;$) {my ($code, $arg) = @_; $code->($arg) }

foo { print "$_[0]\n" }, 'hello'
$ perl p

$

For those following along at home, perl prints a sole \n (consider od
-bc or the like on the output). Let's peek at what's really happening:

$ perl -MO=Deparse p
sub foo (&;$) {
my($code, $arg) = @_;
&$code($arg);
}
foo(sub {
print "$_[0]\n";
}
), '???';
p syntax OK
$

If we make the $ parameter required:
$ cat p
#!/usr/bin/env perl

sub foo (&$) {my ($code, $arg) = @_; $code->($arg) }

foo { print "$_[0]\n" }, 'hello'
$ perl p
Not enough arguments for main::foo at p line 5, near "},"
Execution of p aborted due to compilation errors.
$

I think this covers all cases of ambiguity Darrin pointed out above. Are
there any others I've missed?
--
Chris Nehren | Coder, Sysadmin, Masochist
Shadowcat Systems Ltd. | http://shadowcat.co.uk/


paul at pjcj

Jul 6, 2012, 3:13 AM

Post #7 of 11 (149 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wed, Jul 04, 2012 at 12:58:21AM -0400, Chris Nehren wrote:
> Recently I've been working on a DSL of sorts that would be more
> succinctly expressed if I could have a prototype like ($&&). What would
> break if we allowed the & prototype after the first position? To make
> the proposed feature concrete, it would enable us to have syntax like
> (courtesy of LeoNerd):
>
> generic_sort { $a <=> $b } { length $_ } @strings;
>
> or (from my own code):
>
> loop host 'shadowcat' => { do_stuff; };
>
> I'm fine with doing the work to add this feature. I would just like to
> be sure I don't break half of CPAN with my efforts.
>
> Thoughts? Comments? Rallying praise? Rotten tomatoes?

My recent rjbs induced trip down memory lane unearthed the following
message from a certain larry [at] wall:

http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg01111.html

Summary:

- "I'm not saying it's impossible, just that it's difficult."
- "I also suspect that this could be generalized into a macro capability"

See Ecclesiastes 1:9,10

--
Paul Johnson - paul [at] pjcj
http://www.pjcj.net


leonerd at leonerd

Jul 10, 2012, 6:41 AM

Post #8 of 11 (132 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Fri, Jul 06, 2012 at 12:13:51PM +0200, Paul Johnson wrote:
> On Wed, Jul 04, 2012 at 12:58:21AM -0400, Chris Nehren wrote:
> > Recently I've been working on a DSL of sorts that would be more
> > succinctly expressed if I could have a prototype like ($&&). What would
> > break if we allowed the & prototype after the first position? To make
> > the proposed feature concrete, it would enable us to have syntax like
> > (courtesy of LeoNerd):
> >
> > generic_sort { $a <=> $b } { length $_ } @strings;

*grin*

> > or (from my own code):
> >
> > loop host 'shadowcat' => { do_stuff; };
> >
> > I'm fine with doing the work to add this feature. I would just like to
> > be sure I don't break half of CPAN with my efforts.
> >
> > Thoughts? Comments? Rallying praise? Rotten tomatoes?

Needless to say, I'd be quite keen to get this ability...

> My recent rjbs induced trip down memory lane unearthed the following
> message from a certain larry [at] wall:
>
> http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg01111.html

I don't see anything there that specifically says it can't be done, or
that it shouldn't. Only that it's not easy...

> Summary:
>
> - "I'm not saying it's impossible, just that it's difficult."
> - "I also suspect that this could be generalized into a macro capability"

So, aren't we all about making difficult things possible?

--
Paul "LeoNerd" Evans

leonerd [at] leonerd
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
Attachments: signature.asc (0.19 KB)


demerphq at gmail

Jul 10, 2012, 11:35 PM

Post #9 of 11 (129 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On 10 July 2012 15:41, Paul LeoNerd Evans <leonerd [at] leonerd> wrote:
> On Fri, Jul 06, 2012 at 12:13:51PM +0200, Paul Johnson wrote:
>> On Wed, Jul 04, 2012 at 12:58:21AM -0400, Chris Nehren wrote:
>> > Recently I've been working on a DSL of sorts that would be more
>> > succinctly expressed if I could have a prototype like ($&&). What would
>> > break if we allowed the & prototype after the first position? To make
>> > the proposed feature concrete, it would enable us to have syntax like
>> > (courtesy of LeoNerd):
>> >
>> > generic_sort { $a <=> $b } { length $_ } @strings;
>
> *grin*
>
>> > or (from my own code):
>> >
>> > loop host 'shadowcat' => { do_stuff; };
>> >
>> > I'm fine with doing the work to add this feature. I would just like to
>> > be sure I don't break half of CPAN with my efforts.
>> >
>> > Thoughts? Comments? Rallying praise? Rotten tomatoes?
>
> Needless to say, I'd be quite keen to get this ability...
>
>> My recent rjbs induced trip down memory lane unearthed the following
>> message from a certain larry [at] wall:
>>
>> http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg01111.html
>
> I don't see anything there that specifically says it can't be done, or
> that it shouldn't. Only that it's not easy...
>
>> Summary:
>>
>> - "I'm not saying it's impossible, just that it's difficult."
>> - "I also suspect that this could be generalized into a macro capability"
>
> So, aren't we all about making difficult things possible?

Just wanted to point out that perl uses heuristics to parse map BLOCK
LIST properly as it otherwise requires infinite lookahead to parse.
Perl gets it wrong regularly, although gets it right sufficiently more
than it gets it wrong that most people don't notice. Try for instance
sticking "use warnings;" (or no warnings;) as the first line of a map:

$ perl -wle'map { no warnings } @ARGV' 1
"no" not allowed in expression at -e line 1, at end of line
BEGIN not safe after errors--compilation aborted at -e line 1.

But put a semicolon right after the open brace and you trigger the
heuristic and it works:

$ perl -wle'map {; no warnings } @ARGV' 1

The proposal is to put in more infinite lookahead constructs, no doubt
requiring yet more heuristics (which will occasionally be wrong).

When these heuristics go wrong, and perl has more than people realize,
the general result is very strange error messages.

So to me the question is: is the feature worth the cost?

Anyway, I guess Ill wait for a patch, and see how it works out, maybe
the problem is easier than I think.

cheers,
Yves

--
perl -Mre=debug -e "/just|another|perl|hacker/"


b2gills at gmail

Jul 11, 2012, 9:32 AM

Post #10 of 11 (121 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

On Wed, Jul 11, 2012 at 1:35 AM, demerphq <demerphq [at] gmail> wrote:
> On 10 July 2012 15:41, Paul LeoNerd Evans <leonerd [at] leonerd> wrote:
>> On Fri, Jul 06, 2012 at 12:13:51PM +0200, Paul Johnson wrote:
>>> On Wed, Jul 04, 2012 at 12:58:21AM -0400, Chris Nehren wrote:
>>> > Recently I've been working on a DSL of sorts that would be more
>>> > succinctly expressed if I could have a prototype like ($&&). What would
>>> > break if we allowed the & prototype after the first position? To make
>>> > the proposed feature concrete, it would enable us to have syntax like
>>> > (courtesy of LeoNerd):
>>> >
>>> > generic_sort { $a <=> $b } { length $_ } @strings;
>>
>> *grin*
>>
>>> > or (from my own code):
>>> >
>>> > loop host 'shadowcat' => { do_stuff; };
>>> >
>>> > I'm fine with doing the work to add this feature. I would just like to
>>> > be sure I don't break half of CPAN with my efforts.
>>> >
>>> > Thoughts? Comments? Rallying praise? Rotten tomatoes?
>>
>> Needless to say, I'd be quite keen to get this ability...
>>
>>> My recent rjbs induced trip down memory lane unearthed the following
>>> message from a certain larry [at] wall:
>>>
>>> http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-08/msg01111.html
>>
>> I don't see anything there that specifically says it can't be done, or
>> that it shouldn't. Only that it's not easy...
>>
>>> Summary:
>>>
>>> - "I'm not saying it's impossible, just that it's difficult."
>>> - "I also suspect that this could be generalized into a macro capability"
>>
>> So, aren't we all about making difficult things possible?
>
> Just wanted to point out that perl uses heuristics to parse map BLOCK
> LIST properly as it otherwise requires infinite lookahead to parse.
> Perl gets it wrong regularly, although gets it right sufficiently more
> than it gets it wrong that most people don't notice. Try for instance
> sticking "use warnings;" (or no warnings;) as the first line of a map:
>
> $ perl -wle'map { no warnings } @ARGV' 1
> "no" not allowed in expression at -e line 1, at end of line
> BEGIN not safe after errors--compilation aborted at -e line 1.
>
> But put a semicolon right after the open brace and you trigger the
> heuristic and it works:
>
> $ perl -wle'map {; no warnings } @ARGV' 1
>
> The proposal is to put in more infinite lookahead constructs, no doubt
> requiring yet more heuristics (which will occasionally be wrong).
>
> When these heuristics go wrong, and perl has more than people realize,
> the general result is very strange error messages.
>
> So to me the question is: is the feature worth the cost?
>
> Anyway, I guess Ill wait for a patch, and see how it works out, maybe
> the problem is easier than I think.
>
> cheers,
> Yves
>
> --
> perl -Mre=debug -e "/just|another|perl|hacker/"

As long as these statements apply to your code, I don't see much of a problem.

- doesn't break code that doesn't use it ( that isn't already broken )
- doesn't significantly slow down perl during compilation
- doesn't slow down perl during run-time
- doesn't crash perl
- is heavily tested

---

Off-topic:

It would be nice if there were a way to specify this in a prototype,
and have it actually work:

sub foo([&\%]){...}

because this

sub foo(\[&%]){...}

only accepts code-references of the form:

foo &bar;


sprout at cpan

Jul 29, 2012, 4:20 PM

Post #11 of 11 (94 views)
Permalink
Re: What would having a & prototype after the first position break? [In reply to]

Chris Nehren wrote:
> I'm fine with doing the work to add this feature. I would just like to
> be sure I don't break half of CPAN with my efforts.
>
> Thoughts? Comments? Rallying praise? Rotten tomatoes?

I like the idea. Implementing it may be complex, because the parser (perly.y) has a special branch of its listop rule for this case (search for LSTOPSUB). To allow (&&), ($&), (&$&$), and all other variations would need infinite parser rules, if the current approach were to be used. So that could never work. Instead, the lexer (toke.c; search for PREBLOCK(LSTOPSUB)) would have to invent commas to feed to the parser. You might need to add new possible values for PL_expect. For an example of remembering what type of block we are in, see the last few commits on the sprout/overridesε branch, particularly ae8d0518822.

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.