Padre::Task::LexicalReplaceVariable - Lexically variable replace using L<PPI>


Padre documentation Contained in the Padre distribution.

Index


Code Index:

NAME

Top

Padre::Task::LexicalReplaceVariable - Lexically variable replace using PPI

SYNOPSIS

Top

  my $replacer = Padre::Task::LexicalReplaceVariable->new(
          document    => $document_obj,
          location    => [ $line, $column ], # the position of *any* occurrence of the variable
          replacement => '$foo',
  );
  $replacer->schedule();

DESCRIPTION

Top

Given a location in the document (line/column), determines the name of the variable at this position, finds where the variable was defined, and lexically replaces all occurrences with another variable.

The replacement can either be provided explicitly by the user (using the replacement option) or the user may set the to_camel_case or from_camel_case options. In that case the variable will be converted to/from camel case. With the latter options, ucfirst will force the upper-casing of the first letter (as is typical with global variables).

SEE ALSO

Top

This class inherits from Padre::Task::PPI.

AUTHOR

Top

Steffen Mueller smueller@cpan.org

COPYRIGHT AND LICENSE

Top


Padre documentation Contained in the Padre distribution.
package Padre::Task::LexicalReplaceVariable;

use 5.008;
use strict;
use warnings;
use Padre::Task::PPI ();

our $VERSION = '0.86';
our @ISA     = 'Padre::Task::PPI';

sub process {
	my $self     = shift;
	my $ppi      = shift or return;
	my $location = $self->{location};

	my %opt;
	$opt{replacement}     = $self->{replacement}     if defined $self->{replacement};
	$opt{to_camel_case}   = $self->{to_camel_case}   if defined $self->{to_camel_case};
	$opt{from_camel_case} = $self->{from_camel_case} if defined $self->{from_camel_case};
	$opt{'ucfirst'}       = $self->{'ucfirst'};
	my $munged = eval {
		require PPIx::EditorTools::RenameVariable;
		PPIx::EditorTools::RenameVariable->new->rename(
			ppi    => $ppi,
			line   => $location->[0],
			column => $location->[1],
			%opt,
		);
	};
	if ($@) {
		$self->{error} = $@;
		return;
	}

	# Save the results
	$self->{munged}   = $munged->code;
	$self->{location} = $munged->element->location;

	return;
}

1;

__END__

# Copyright 2008-2011 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.