Overriding and extending the given/when construct

Discussion in 'Perl Misc' started by Klaus, Jul 16, 2012.

  1. Klaus

    Klaus Guest

    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
    Klaus, Jul 16, 2012
    #1
    1. Advertising

  2. Klaus

    Klaus Guest

    On 17 juil, 00:36, Ben Morrow <> wrote:
    > Quoth Klaus <>:
    > > 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).

    >
    > 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
    Klaus, Jul 17, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lord0
    Replies:
    1
    Views:
    562
    Thomas Weidenfeller
    Apr 19, 2006
  2. chiara
    Replies:
    6
    Views:
    465
    Barry Schwarz
    Oct 6, 2005
  3. 2Barter.net
    Replies:
    0
    Views:
    363
    2Barter.net
    Dec 13, 2006
  4. Casey Hawthorne
    Replies:
    385
    Views:
    5,582
    ng2010
    Apr 4, 2010
  5. Mukesh
    Replies:
    4
    Views:
    618
    Paul N
    Mar 26, 2010
Loading...

Share This Page