[Israel.pm] Fwd: [Telux] Next Meeting: "High-Level Programming Concepts Using Perl 6" on 22-March

Evgeny evgeny.zislis at gmail.com
Sat Mar 14 15:15:03 PDT 2009


The requirements are simple

They are:
    Word regular_expression block_of_code

Where:
  word is Given/When/Then. Currently I use a function name for this.
  regular_expression is just that, a regexp
  block_of_code is an anonymous function, or callback, or call it what you like

Current minimal syntax for doing this, in perl, as I have found with
your really really greatly appreciated help is this:

Given qr/a color (.*)/, sub {
  $color = shift;
};

Every character that I wrote (other that the inside of the block) is
significant. And if only I could make the "qr" and the comma and the
"sub" go away, it would be just perfect. But it is not bad as it is
right now really. Quite DSLish :)

Any way to turn it into :
  Given /a color (.*)/ {
    $color = shift;
  }

?


- evgeny


-------------
p5-cucumber
http://github.com/kesor/p5-cucumber


On Sat, Mar 14, 2009 at 11:55 PM, Mikhael Goikhman <migo at homemail.com> wrote:
> On 14 Mar 2009 23:17:26 +0200, Evgeny wrote:
>>
>> So the block (sub) works without the word "sub" only if it is the first
>> parameter?
>
> Yes. My second answer was for block meaning closure, not about the
> syntactic sugar.
>
> Actually having two pairs of {} together is kind of possible, but
> does not do what you want:
>
>  sub run (&$$) { $_[0]->(); print %{$_[1]}, %{$_[2]} }
>
>  run { print "ok" } { key => "value" }, { 1 .. 9 };
>
> I didn't see your actual DSL requirements. As I also mentioned, possibly
> this syntax may work for you (try to figure out the needed prototypes):
>
>  apply { one-thing } on { another-thing };
>
> Regards,
> Mikhael.
>
>> On Sat, Mar 14, 2009 at 10:40 PM, Mikhael Goikhman <migo at homemail.com>
>> > wrote: On 14 Mar 2009 19:20:05 +0200, Evgeny wrote:
>> >>
>> >> Actually, in ruby I can have a method that accepts multiple blocks.
>> >> Blocks don't HAVE to come as the last argument.   def (&first_block,
>> >> &second_block) will work as well.
>> >
>> > The same syntax works in Perl. We just spoke about DSL (syntactic
>> > sugar).
>> >
>> > Moreover, in Perl you can pass any number of anonymous functions
>> > (closures) defined inline, something that is for example impossible
>> > in Python by design.
>> >
>> >  run(sub { do something; do well }, sub { do something else });
>> >
>> >> But in perl, I tried to do the same - by replacing sm(&$) to be
>> >> sm($&) - but it does not work. So does that mean that in perl I
>> >> can't have the block where ever I want, and pass it as the last
>> >> argument? Why?
>> >
>> > Nope, this is incorrect. I demonstrated this in the previous message:
>> >
>> >  Given /regular-expression-here/, sub { some code here };
>> >
>> > This works too (no need for parentheses if you specify prototypes):
>> >
>> >  run $first_arg, sub { "second" }, 3, sub { "forth" };
>> >
>> > Regards,
>> > Mikhael.
>
> --
> perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'
>



More information about the Perl mailing list