NAME

Eval::Context - Evalute perl code in context wrapper

SYNOPSIS

use Eval::Context ;

my $context = new Eval::Context(PRE_CODE => "use strict;\nuse warnings;\n") ;

# code will be evaluated with strict and warnings loaded in the context.

$context->eval(CODE => 'print "evaluated in an Eval::Context!" ;') ;
$context->eval(CODE_FROM_FILE => 'file.pl') ;

DESCRIPTION

This module define a subroutine that let you evaluate Perl code in a specific context. The code can be passed directly as a string or as a file name to read from.

SUBROUTINES/METHODS

new(@options)

Create an Eval::Context object. The object is used as a repository of "default" values. the values can be temporarily overridden during the eval call. All arguments have default values.

  my $context = new Eval::Context() ; # default context
  
  my $context2= new Eval::Context
		(
		NAME              => 'libraries evaluation context',
		PACKAGE           => 'libraries',
		PRE_CODE          => 'use strict ;\n"
		POST_CODE         => 'some_code_automatically_run() ;'
		PERL_EVAL_CONTEXT => undef, # libraries willalways evaluated in scalar context
		
		INTERACTION =>
			{
			INFO  => \&sub_info,
			WARN  => \&sub_warn,
			DIE   => \&sub_die,
			},
			
		DISPLAY_SOURCE_IN_CONTEXT => 1, #useful when debuging
		) ;

Arguments

  • @option - setup data for the object

    • NAME - use when displaying information about the object. Set automatically if not set.

    • PACKAGE - the package the code passed to eval will be in

    • PRE_CODE - code prepended to the code passed to eval

    • POST_CODE - code appended to the code passed to eval

    • PERL_EVAL_CONTEXT - the context to eval code in (void, scalar, list). Works as wantarray

    • INTERACTION

      Lets you define subs used to interact with the user.

      INTERACTION      =>
      	{
      	INFO  => \&sub,
      	WARN  => \&sub,
      	DIE   => \&sub,
      	}
      INFO

      This sub will be used when displaying information.

      WARN

      This sub will be used when a warning is displayed.

      DIE

      Used when an error occurs.

      The functions default to:

      • INFO => CORE::print

      • WARN => Carp::carp

      • DIE => Carp::confess

    • FILE - the file where the object has been created. Set automatically if not set. this is practical if you want to wrap the object.

    • LINE - the line where the object has been created. Set automatically if not set.

    • DISPLAY_SOURCE_IN_CONTEXT - if set, the code to evaluated will be displayed before evaluation

Return

  • an Eval::Context object.

Setup

Helper sub called by new. This is considered private.

CheckOptionNames

Verifies the named options passed to the members of this class. Calls {INTERACTION}{DIE} in case of error. This shall not be used directly.

SetInteractionDefault

This shall not be used directly.

CanonizeName

This shall not be used directly.

eval(@options)

Evaluates Perl code, passed as a string or read from a file, in the context.

Evaluation context of the code (void, scalar, list) is the same as the context this subroutine was called in or in the context defined by PERL_EVAL_CONTEXT if that option is present.

my $context = new Eval::Context(PRE_CODE => "use strict;\nuse warnings;\n") ;

$context->eval(CODE => 'print "evaluated in an Eval::Context!";') ;
$context->eval(CODE_FROM_FILE => 'file.pl') ;

Arguments

The options passed to eval override the options passed to new. the override is temporary during the duration of this call.

  • @options - Any of the constructor options. one the following options (mandatory).

    • CODE - a string containing perl code (valid or you'll get errors)

    • CODE_FROM_FILE - a file containing perl code

Return

  • What the code to be evaluated returns

BUGS AND LIMITATIONS

None so far.

AUTHOR

Khemir Nadim ibn Hamouda
CPAN ID: NKH
mailto:nadim@khemir.net

LICENSE AND COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SUPPORT

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

perldoc Eval::Context

You can also look for information at: