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

Mikhael Goikhman migo at homemail.com
Sat Mar 14 09:30:32 PDT 2009


On 14 Mar 2009 17:32:39 +0200, Shlomi Fish wrote:
> 
> On Saturday 14 March 2009 16:01:02 Evgeny wrote:
> >
> > How can I pass an unspecified amount of arguments to a function,
> > since a function does not really care how many parameters it gets - I
> > just read them one by one with "shift". I want to have a generic
> > $callback->(.........) where instead of the dots I dont use an array,
> > but "explode" this array into multiple arguments.
> >
> > In ruby it is done exactly like that   callback( * array )   where
> > the * is exploding it.

I think what Evgeny asks here is how given array object (in Ruby
terminology) or array reference (in Perl terminology) to flatten it.
In Perl you do this by dereferencing:

  $callback->(@$array);

> > So where I use
> >
> >    sm(qr/Given a (.*)/, sub {
> >      some code here;
> >    })
> >
> > I would want a DSL to allow programmers to write it easier, with less
> > syntax. Like in ruby's cucumber:
> >
> >   Given /a (.*)/ {
> >     some code here;
> >   }
> >
> 
> Hmmmm.... not sure if the Perl 5 syntax is that flexible.

It is flexible enough. Ruby supports one trailing block of code.
Perl supports one leading block of code (if function prototype starts
with '&'). So the DSL syntax using pure Perl 5 may be one of:

  Apply { some code here } /regular-expression-here/;

or:

  Apply { some code here } qr/regular-expression-here/;

or:

  Apply { some code here } On { /regular-expression-here/ };

or the obvious:

  Given /regular-expression-here/, sub { some code here };

depending on what you really want. Example (tested):

  sub Apply (&@) {
      my ($callback, @args) = @_;
      $callback->(@args);
  }

  local $_ = 'Name: John Fight';

  Apply { print @_ } /Name: (.*)/;

  Apply { print @_ } qr/Name: (.*)/;

  Apply { print join(', ', @_) } /Name: (\S+) (\S+)/;

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list