Overriding and extending the given/when construct


K

Klaus

Hello everybody

I want to write a CPAN module that overrides / extends the given/when
construct.

I am a Perl programmer with a little knowledge of C, but no knowledge
of XS,

Is there any documentation (perldoc, blogs, documents on the internet,
etc...) that could start me off writing a given/when extension on
CPAN ? (I prefer a pure perl aproach, but I am willing to learn XS if
needed).

Thanks.

-- Klaus
 
Ad

Advertisements

K

Klaus

How, exactly, do you want to extend it?

I want to add a qualifier to "when()" (i.e. when true (...), when eq
(...), when == (...)) as described in a post on perl5.porters
http://groups.google.com/group/perl.perl5.porters/msg/c0ee23905fe59030?hl=fr

given( $foo ) {
when true ($_ eq 'abc') { blah } ;
# evaluates condition as is
when true (/regex/) { blah } ;
# translates naturally into $_ =~/regex/
when eq ('str1', 'str2') { blah } ;
# translates into $_ in ('str1', 'str2'), which
# translates into $_ eq 'str1' or $_ eq 'str2'
when eq ('str1') { blah } ;
# translates into $_ in ('str1'), which
# translates into $_ eq 'str1'
when == (4, 5) { blah } ;
# translates into $_ in_== (4, 5), which
# translates into $_ == 4 or $_ == 5
when == (4) { blah } ;
# translates into $_ in_== (4), which
# translates into $_ == 4
For some cases, creating a class
with an overloaded ~~ operator will be sufficient. Otherwise, I would
strongly recommend finding some other way of doing whatever you're
trying to do: smartmatch is hairy enough as it is, without adding more.

I don't want to overload smartmatch, but I need to implement the new
operators "in" and "in_==" as follows:

$var in ('str1', 'str2') is equivalent to $var eq 'str1' or $vat eq
'str2'
$var in_== (4,5) is equivalent to $var == 4 or $var == 5

-- Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top