develooper Front page | perl.perl5.porters | Postings from July 2012

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

Thread Previous
From:
Chris Nehren
Date:
July 4, 2012 12:55
Subject:
Re: What would having a & prototype after the first position break?
Message ID:
20120704195525.GE6569@isuckatdomains.isuckatdomains.net
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/

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About