Aiswarya
Aiswarya

Reputation: 49

Is it possible to know the variable that was replaced by its value inside a double quotes in Perl?

Say you've :-

my $variable = 'Value';

my $text = "$variable is stored inside scalar variable.\n";

print "$text\n";

Now, when you see the output, it will be Value is stored inside scalar variable.

I want to get the information that, here $variable was replaced with 'value'. Is it possible?

I'm actually trying to get the list of all such statements where a variable was replaced with its value. For e.g. let's say File 1 has the above piece of code. File 2 has the following:

print "Welcome to $website!!";

File 3 has - print "Output emailed..";

I'm trying to get the details that File 1 has my $text = "$variable is stored inside scalar variable.\n"; and File 2 has print "Welcome to $website!!";

File 3 is not counted.

Upvotes: 1

Views: 96

Answers (3)

帅帅耳机
帅帅耳机

Reputation: 11

you can't do that.

the code:

my $variable = 'Value'; my $text = "$variable is stored inside scalar variable.\n";

$variable will be replaced by it's value when assign $text. And the $text is a literal string.


update my answer

I think you'll like this:

#!/usr/bin/perl

use warnings;
use strict;

package myVariable;
use overload
    qw("") => \&asString,
    qw(+) => \&append;

sub new (;+[$]){
    my $class = shift;
    my $self = {
        'have_var' => 0,
        'string' => '',
    };
    &init($self, @_);

    bless $self,$class;
}   

sub init {
    my $class = shift;

    for(@_) {
        if(ref($_) eq ref(\"")) {
            $class->{'have_var'}++;
            $class->{'string'} .= $$_;
        } else {
            $class->{'string'} .= $_;
        }
    }
}
sub asString {
    my $class = shift;
    return $class->{'string'};
}

sub APPEND {
    my $class = shift;
    $class->init(@_);
}

sub HAVE_VAR {
    my $class = shift;
    return $class->{'have_var'};
}

package main;
my $variable_1 = "Value";
my $variable_2 = "newVal";
my $variable_3 = "otherVal";

my $text = myVariable->new(\$variable_1, " ", \$variable_2, " this is text! ", \$variable_3);

my $appendString = " appendString";
$text->APPEND(\$appendString);

print "text content is :  [$text]\n";

print "there are ", $text->HAVE_VAR, " variables in \$text";

you'll get

text content is :  [Value newVal this is text! otherVal appendString]
there are 4 variables in $text

Upvotes: -1

daxim
daxim

Reputation: 39158

Parsing source code gets you half-way there.

use PPI qw();

sub walk {
    my ($ref) = @_;
    if ($ref->can('children')) {
        for my $c ($ref->children) {
            if ($c->can('interpolations')) {
                if ($c->interpolations) {
                    say sprintf "Found interpolations of variables at line %d column %d and the string was:\n%s\n", $c->line_number, $c->column_number, $c->content;
                }
            }
            walk($c);
        }
    }
    return;
}

walk(PPI::Document->new(\(join '', *DATA->getlines)));


__DATA__
my $variable = 'Value';

my $text = "$variable is stored inside scalar variable.\n";

print "$text\n";

Result:

Found interpolations of variables at line 3 column 12 and the string was:
"$variable is stored inside scalar variable.\n"

Found interpolations of variables at line 5 column 7 and the string was:
"$text\n"

It is not straightforward to conclude from an interpolated expression what the variable name is. Your example only had simple scalars, but consider:

 "$foo[0]"         # @foo single lookup
 "@foo[0,1]"       # @foo slice 
 "$foo{key}"       # %foo single lookup
 "@foo{qw(k1 k2)}" # %foo slice

Variables/expressions within expressions and reference chains make things even more complicated.

Upvotes: 2

daxim
daxim

Reputation: 39158

This shows a backtrace whenever the variable is read. Close enough?

package Peekaboo;
require Tie::Scalar;
our @ISA = 'Tie::StdScalar';
use Carp 'cluck';
sub FETCH { cluck }

package main;
my $variable = 'Value';
tie $variable, 'Peekaboo';
my $text = "$variable is stored inside scalar variable.\n";

If not, then you likely need B::Utils and PadWalker. I don't know how to use them.

Upvotes: 4

Related Questions