| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # Copyright (C) 2011-2012, 2014-215 Rocky Bernstein <rocky@cpan.org> | ||
| 3 | use warnings; no warnings 'redefine'; | ||
| 4 | use rlib '../../../../..'; | ||
| 5 | |||
| 6 | package Devel::Trepan::CmdProcessor::Command::Set::Variable; | ||
| 7 | |||
| 8 | use Devel::Trepan::CmdProcessor::Command::Subcmd::Core; | ||
| 9 | use PadWalker qw(peek_our peek_my); | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use vars qw(@ISA @SUBCMD_VARS); | ||
| 13 | @ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd); | ||
| 14 | # Values inherited from parent | ||
| 15 | use vars @Devel::Trepan::CmdProcessor::Command::Subcmd::SUBCMD_VARS; | ||
| 16 | |||
| 17 | ## FIXME: do automatically. | ||
| 18 | our $CMD = "set variable"; | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head2 Synopsis: | ||
| 23 | |||
| 24 | =cut | ||
| 25 | our $HELP = <<'HELP'; | ||
| 26 | =pod | ||
| 27 | |||
| 28 | B<set variable> I<variable-name> = I<value> | ||
| 29 | |||
| 30 | Set a I<my> or I<our> variable; I<value> must be a constant. | ||
| 31 | |||
| 32 | =head2 Examples: | ||
| 33 | |||
| 34 | set variable $foo = 20 | ||
| 35 | set variable @ARY = (1,2,3) | ||
| 36 | |||
| 37 | =head2 See also: | ||
| 38 | |||
| 39 | L<C<eval>|Devel::Trepan::CmdProcessor::Command::Eval> | ||
| 40 | |||
| 41 | =cut | ||
| 42 | HELP | ||
| 43 | |||
| 44 | our $SHORT_HELP = "Set a 'my' or 'our' variable"; | ||
| 45 | |||
| 46 | our $MIN_ABBREV = length('var'); | ||
| 47 | our $MIN_ARGS = 2; | ||
| 48 | our $MAX_ARGS = undef; | ||
| 49 | our $NEED_STACK = 1; | ||
| 50 | |||
| 51 | sub set_var($$$) | ||
| 52 | { | ||
| 53 | my ($var_name, $ref, $value) = @_; | ||
| 54 | my $type = substr($var_name, 1, 1); | ||
| 55 | if ('$' eq $type) { | ||
| 56 | ${$ref->{$var_name}} = $value; | ||
| 57 | } elsif ('@' eq $type) { | ||
| 58 | @{$ref->{$var_name}} = @{$value}; | ||
| 59 | } elsif ('%' eq $type) { | ||
| 60 | %{$ref->{$var_name}} = %{$value}; | ||
| 61 | } else { | ||
| 62 | ${$ref->{$var_name}} = $value; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | sub run($$) | ||
| 67 | { | ||
| 68 | my ($self, $args) = @_; | ||
| 69 | my $proc = $self->{proc}; | ||
| 70 | my @args = @$args; | ||
| 71 | shift @args; shift @args; | ||
| 72 | |||
| 73 | my $var_name = shift @args; | ||
| 74 | shift @args if $args[0] eq '='; | ||
| 75 | my $value = join(' ', @args); | ||
| 76 | |||
| 77 | my $i; | ||
| 78 | while (my ($pkg, $file, $line, $fn) = caller($i++)) { ; }; | ||
| 79 | no warnings 'once'; | ||
| 80 | my $diff = $i - $DB::stack_depth; | ||
| 81 | # FIXME: 4 is a magic fixup constant, also found in DB::finish. | ||
| 82 | # Remove it. | ||
| 83 | my $our_hash = peek_our($diff + $proc->{frame_index} + 4); | ||
| 84 | my $my_hash = peek_my($diff + $proc->{frame_index} + 4); | ||
| 85 | |||
| 86 | if (exists($my_hash->{$var_name})) { | ||
| 87 | set_var($var_name, $my_hash, $value); | ||
| 88 | } elsif (exists($our_hash->{$var_name})) { | ||
| 89 | set_var($var_name, $my_hash, $value); | ||
| 90 | } else { | ||
| 91 | $proc->errmsg("Can't find $var_name as a 'my' or 'our' variable"); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | unless (caller) { | ||
| 96 | require Devel::Trepan; | ||
| 97 | # Demo it. | ||
| 98 | # FIXME: DRY with other subcommand manager demo code. | ||
| 99 | require Devel::Trepan::CmdProcessor::Mock; | ||
| 100 | my ($proc, $cmd) = | ||
| 101 | Devel::Trepan::CmdProcessor::Mock::subcmd_setup(); | ||
| 102 | Devel::Trepan::CmdProcessor::Mock::subcmd_demo_info($proc, $cmd); | ||
| 103 | #my @args = (@{$cmd->{prefix}}, '$foo', '=', '20'); | ||
| 104 | # $cmd->run(\@args); | ||
| 105 | } | ||
| 106 | |||
| 107 | # Suppress a "used-once" warning; | ||
| 108 | $HELP || scalar @SUBCMD_VARS; |