| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | # NOTE: this does syntax checking and has problems on MS Windows. | ||
| 4 | # More specific context checking can be had in DB::Eval::eval_not_ok() | ||
| 5 | # and that is gnereally (I think) we will be using. | ||
| 6 | use strict; use warnings; | ||
| 7 | package Devel::Trepan::Condition; | ||
| 8 | use English qw( -no_match_vars ); | ||
| 9 | use vars qw(@EXPORT @ISA); | ||
| 10 | @EXPORT = qw( is_valid_condition ); | ||
| 11 | @ISA = qw(Exporter); | ||
| 12 | |||
| 13 | sub is_valid_condition($) { | ||
| 14 | my ($expr) = @_; | ||
| 15 | return 1 if ($OSNAME eq 'MSWin32'); | ||
| 16 | my $pid = fork(); | ||
| 17 | if ($pid) { | ||
| 18 | waitpid($pid, 0); | ||
| 19 | return $CHILD_ERROR == 0; | ||
| 20 | } else { | ||
| 21 | close STDERR; | ||
| 22 | if ($OSNAME eq 'MSWin32') { | ||
| 23 | system ($EXECUTABLE_NAME, '-c', '-e', $expr); | ||
| 24 | exit $?; | ||
| 25 | } else { | ||
| 26 | exec($EXECUTABLE_NAME, '-c', '-e', $expr); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | # Demo code | ||
| 32 | unless (caller) { | ||
| 33 | for my $expr ('$a=2', '1+', "join(', ', \@ARGV)", 'join(", ", @ARGV)') { | ||
| 34 | my $ok = is_valid_condition($expr); | ||
| 35 | printf("$expr is %sa valid_condition\n", $ok ? '' : 'not '); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | 1; |