View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Barry Walsh > PerlX-QuoteOperator-0.04 > PerlX::QuoteOperator



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.04   Source   Latest Release: PerlX-QuoteOperator-0.08


PerlX::QuoteOperator - Create new quote-like operators in Perl


Version 0.04


Create a quote-like operator which convert text to uppercase:

    use PerlX::QuoteOperator quc => {
        -emulate => 'q', 
        -with    => sub ($) { uc $_[0] }, 
    say quc/do i have to $hout/;
    # => DO I HAVE TO $HOUT



Perl comes with some very handy Quote-Like Operators :)

But what it doesn't come with is some easy method to create your own quote-like operators :(

This is where PerlX::QuoteOperator comes in. Using the fiendish Devel::Declare under its hood it "tricks", sorry "helps!" the perl parser to provide new first class quote-like operators.


The subterfuge doesn't go that deep. If we take a look at the SYNOPSIS example:

    say quc/do i have to $hout/;

Then all PerlX::QuoteOperator actually does is convert this to the following before perl compiles it:

    say quc q/do i have to $hout/;

Where 'quc' is a defined sub expecting one argument (ie, sub ($) { uc $_[0] } ).

This approach allows PerlX::QuoteOperator to perform the very basic keyhole surgery on the code, ie. just put in the emulated quote-like operator between keyword & argument.

However this approach does have caveats especially when qw// is being used!. See CAVEATS. There is an alternative parser when can be invoked, see -parser Export parameter.


Bit like climbing Mount Everest... because we can! ;-)

Is really having something like:

    say quc/do i have to $hout/;

so much better than:

    say uc 'do i have to $hout';

or more apt this:

    say uc('do i have to $hout');

Probably not... at least in the example shown. But things like this are certainly eye catching:

    use PerlX::QuoteOperator::URL 'qh';
    my $content = qh( );   # does HTTP request

And this:

    use PerlX::QuoteOperator qwHash => { 
        -emulate    => 'qw',
        -with       => sub (@) { my $n; map { $_ => ++$n } @_ },

    my %months = qwHash/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;

Certainly give the code aesthetic a good pause for thought.


By default nothing is exported:

    use PerlX::QuoteOperator;    # => imports nothing

Quote operator is imported when passed a name and options like so:

    use PerlX::QuoteOperator quote_operator_name_i_want_to_use => { }   

A hashref is used to pass the options.



Which Perl quote-like operator required to emulate. q, qq & qw have all been tested.

Default: emulates qq


Your quote-like operator code reference / anon subroutine goes here.

Remember to use subroutine prototype (if not using -parser option):

    -with    => sub ($) { uc $_[0] }, 

This is a mandatory parameter.


If set then alternative parser kicks in. This parser currenly works on single line of code only and must use a parenthesis, braces or same delimeter for beginning and end of quote:

    -parser => 1

When invoked this parser will take this:

    quc/do i have to $hout/;

And by finding the end of the quote will then encapulate it like so:

    quc(q/do i have to $hout/);

Default: Not using alternative parsing.


If set then prints (warn) the transmogrified line so that you can see what PerlX::QuoteOperator has done!

    -debug => 1

Default: No debug.



Module import sub.


When keyword (defined quote operator) is triggered then this sub uses Devel::Declare to provide necessary keyhole surgery/butchery on the code to make it valid Perl code (think Macro here!).


Internal subroutine used in -parser option.


Performing a method call or dereference using -> like below will not work:

    use PerlX::QuoteOperator qurl => { 
        -emulate => 'q', 
        -with    => sub ($) { require URI; URI->new($_[0]) },
    qurl(>authority;   # Throws an error

Because the parsed qurl line becomes this...

    qurl q(>authority;

... so throwing an error trying to call authority on a string. See "HOW DOES IT DO IT" for more info.

A workaround is to use the alternative parser and the line would now be parsed like this:


See -parser option for more information.

Also see examples/ for some more issues with creating qw based quote-like operators. NB. The alternative parser will get around some of these problems but then (potentially) introduces a few new ones! (see TODO)

Recommendation: Stick with Perl parser and all will be fine!



Barry Walsh, <draegtun at>


Please report any bugs or feature requests to bug-perlx-quoteoperator at, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.

    perldoc PerlX::QuoteOperator

You can also look for information at:


From here to oblivion!:

And a round of drinks for the mad genius of MST for creating Devel::Declare in the first place!


This is (near) beta software. I'll strive to make it better each and every day!

However I accept no liability whatsoever should this software do what you expected ;-)


Copyright 2009-2011 Barry Walsh (Draegtun Systems Ltd |, all rights reserved.

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 for more information.

syntax highlighting: