Coder Social home page Coder Social logo

keyword-simple's Introduction

NAME

Keyword::Simple - define new keywords in pure Perl

SYNOPSIS

package Some::Module;

use Keyword::Simple;

sub import {
    # create keyword 'provided', expand it to 'if' at parse time
    Keyword::Simple::define 'provided', sub {
        my ($ref) = @_;
        substr($$ref, 0, 0) = 'if';  # inject 'if' at beginning of parse buffer
    };
}

sub unimport {
    # lexically disable keyword again
    Keyword::Simple::undefine 'provided';
}

'ok'

DESCRIPTION

Warning: This module is still new and experimental. The API may change in future versions. The code may be buggy.

This module lets you implement new keywords in pure Perl. To do this, you need to write a module and call Keyword::Simple::define in your import method. Any keywords defined this way will be available in the lexical scope that's currently being compiled.

Functions

  • Keyword::Simple::define

    Takes two arguments, the name of a keyword and a coderef. Injects the keyword in the lexical scope currently being compiled. For every occurrence of the keyword, your coderef will be called with one argument: A reference to a scalar holding the rest of the source code (following the keyword).

    You can modify this scalar in any way you like and after your coderef returns, perl will continue parsing from that scalar as if its contents had been the real source code in the first place.

  • Keyword::Simple::undefine

    Takes one argument, the name of a keyword. Disables that keyword in the lexical scope that's currently being compiled. You can call this from your unimport method to make the no Foo; syntax work.

BUGS AND LIMITATIONS

This module depends on the pluggable keyword API introduced in perl 5.12. Older versions of perl are not supported.

Every new keyword is actually a complete statement by itself. The parsing magic only happens afterwards. This means that e.g. the code in the "SYNOPSIS" actually does this:

provided ($foo > 2) {
  ...
}

# expands to

; if
($foo > 2) {
  ...
}

The ; represents a no-op statement, the if was injected by the Perl code, and the rest of the file is unchanged.

This also means your new keywords can only occur at the beginning of a statement, not embedded in an expression.

Keywords in the replacement part of a s//.../e substitution aren't handled correctly and break parsing.

There are barely any tests.

SUPPORT AND DOCUMENTATION

After installing, you can find documentation for this module with the perldoc command.

perldoc Keyword::Simple

You can also look for information at https://metacpan.org/pod/Keyword::Simple.

To see a list of open bugs, visit https://rt.cpan.org/Public/Dist/Display.html?Name=Keyword-Simple.

To report a new bug, send an email to bug-Keyword-Simple [at] rt.cpan.org.

AUTHOR

Lukas Mai, <l.mai at web.de>

COPYRIGHT & LICENSE

Copyright (C) 2012, 2013 Lukas Mai.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

keyword-simple's People

Contributors

mauke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

keyword-simple's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.