Acme-Locals

 view release on metacpan or  search on metacpan

lib/Acme/Locals.pm  view on Meta::CPAN

# $Id$
# $Source$
# $Author$
# $HeadURL$
# $Revision$
# $Date$
package Acme::Locals;

use strict;
use warnings;
use version; our $VERSION = qv('0.1.1');
use 5.00600;

use Carp            qw(carp croak);
use PadWalker       ();
use Params::Util    qw(_SCALAR _ARRAY);

BEGIN {
    use English qw(-no_match_vars);
    my $find_best_say = sub {
        eval q{use Perl6::Say}; ## no critic
        return if not $EVAL_ERROR;
        no warnings 'once'; ## no critic
        *say = sub { print @_, "\n" };
    };
    $find_best_say->();
}

my $DEFAULT_FORMAT = q{%s};
my $DEFAULT_MODE   = '-python';

my %EXPORT_OK      = (
    sayx     => \&sayx,
    printx   => \&printx,
    sprintx  => \&sprintx,
    locals   => \&locals,
    globals  => \&globals,
    lexicals => \&lexicals,
);

my %EXPORT_TAGS    = (
    ':all'  => [ keys %EXPORT_OK ],
);

my %MODES = (
    '-python' => qr/\%\( (.+?) \)(\w)?/xms,
    '-ruby'   => qr/\#\{ (.+?) \}/xms,
);

my %mode_for_class;

sub sayx        ($@); ## no critic
sub printx      ($@); ## no critic
sub sprintfx    ($@); ## no critic

sub import {
    my ($this_class, @tags) = @_;
    my $call_class = caller 0;

    my @to_export;
    for my $tag (@tags) {
        if ($tag =~ m/^:/xms) {
            croak __PACKAGE__, " does not support the tag  $tag"
                if not exists $EXPORT_TAGS{$tag};

            push @to_export, @{ $EXPORT_TAGS{$tag} };
        }
        elsif ($tag =~ m/^-/xms) {
            $mode_for_class{$call_class} = $tag;
        }
        else {
            push @to_export, $tag;
        }
    }
    $mode_for_class{$call_class} ||= $DEFAULT_MODE;
    if (not exists $MODES{ $mode_for_class{$call_class} }) {
        my $cur_mode = $mode_for_class{$call_class};
        carp "Unknown mode $cur_mode. Switching to default mode $DEFAULT_MODE";
        $mode_for_class{$call_class} = $DEFAULT_MODE;
    }

    no strict 'refs'; ## no critic
    for my $export_sub (@to_export) {
        croak __PACKAGE__, " does not export $export_sub"
            if not exists $EXPORT_OK{$export_sub};
    
        *{ $call_class . "::$export_sub" } = $EXPORT_OK{$export_sub};
    }

    return;
}

sub sayx ($@){ ## no critic
    say sprintx([caller 0], @_);
}

sub printx ($@) { ## no critic

lib/Acme/Locals.pm  view on Meta::CPAN

}

sub sprintx ($@) { ## no critic
    my $peek_level = 1;
    my $call_class;
    if (_ARRAY( $_[0] )) {
        $call_class = shift->[0];
        $peek_level++;
    }
    $call_class ||= caller 0;

    my ($fmt, %bind_vars) = @_;

    my @binds;
    my $map_bind_var = sub {
        my ($bind_var_name, $format_char) = @_;
        local *__ANON__ = 'map_bind_var'; ## no critic

        my $internal_name = $bind_var_name;
        if (exists $bind_vars{$internal_name}) {
            # pass
        }
        elsif (exists $bind_vars{q{$}.$internal_name}) {
            $internal_name = q{$}.$internal_name;
        }
        else {
            croak "No such bind var: $bind_var_name";
        }

        my $value_ref = $bind_vars{$internal_name};
        croak 'Bind var must be scalar'
            if not _SCALAR($value_ref);

        push @binds, ${ $value_ref };

        return defined $format_char ? q{%} . $format_char
                                    : $DEFAULT_FORMAT;
    };

    my $mode = $mode_for_class{$call_class} || $DEFAULT_MODE;
    my $re   = $MODES{ $mode };

    if ($mode eq '-ruby' && !scalar keys %bind_vars) {
        %bind_vars = %{ PadWalker::peek_my($peek_level) };
    }
    
    $fmt =~ s/$re/$map_bind_var->($1, $2)/xmseg;

    return sprintf $fmt, @binds;
}

sub lexicals {
    goto &locals;
}

sub locals {
    return wantarray ? %{ PadWalker::peek_my(1) }
                     :    PadWalker::peek_my(1);
}

sub globals {
    return wantarray ? %{ PadWalker::peek_our(1) }
                     :    PadWalker::peek_our(1);
}

1;

__END__

=begin wikidoc

= NAME

Acme::Locals - Interpolate like Python/Ruby.

= VERSION

This document describes Acme::Locals version v0.0.1

= SYNOPSIS

    # Python mode

    use Acme::Locals qw(-python :all);

    sub foo {
        my $x   = 10;
        my $y   = 100;
        my $who = "world";
        sayx '%(x)d %(y)d hello %(who)s!', locals();
    }


    # Ruby mode

    use Acme::Locals qw(-ruby :all);

    sub bar {
        my $x   = 10;
        my $y   = 100;
        my $who = "world";

        sayx '#{x} #{y} hello #{who}';
    }


= DESCRIPTION

This module let's you interpolate like Python and Ruby.

= SUBROUTINES/METHODS

== CLASS METHODS

=== {sayx $format @fmt_vars}

print/puts like python/ruby.

=== {printx $format @fmt_vars}

printf like python/ruby.

=== {sprintx $format @fmt_vars}

sprintf like python/ruby.

=== {locals()}

Return a hash of all lexical variables in the current scope. (Using
PadWalker).

=== {globals()}

Return a hash of all global variables. (Using PadWalker).

=== {lexicals()}

Alias to {locals()}

== PRIVATE CLASS METHODS


= DIAGNOSTICS


= CONFIGURATION AND ENVIRONMENT

This module requires no configuration file or environment variables.

= DEPENDENCIES

* [version]

* [PadWalker]

* [Params::Util]

= INCOMPATIBILITIES

None known.

= BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
[bug-acme-locals@rt.cpan.org|mailto:bug-acme-locals@rt.cpan.org], or through the web interface at
[CPAN Bug tracker|http://rt.cpan.org].

= SEE ALSO

= AUTHOR

Ask Solem, [ask@0x61736b.net].

with thanks to sverrej for inspiration :)

= LICENSE AND COPYRIGHT

Copyright (c), 2007 Ask Solem [ask@0x61736b.net|mailto:ask@0x61736b.net].

{Acme::Locals} is distributed under the Modified BSD License.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products derived



( run in 0.435 second using v1.01-cache-2.11-cpan-5a3173703d6 )