0

I am not sure how to state the problem in sentences. Let me give a n instance:

There is one file

abc.pl

it's contents are as follows:

use def;
my $log = $def::logs;
my $text = "this is the text to be searched with value123";

my $var = "value123";
my $findstring = $log->{'search'};
&find("$text","findstring ");

Now file def.pm, which is used in above file:

package def;
our $logs = {
             'search' => "text to be searched with $var"
};

in this file how should I give $var so that it is interpolated in file abc.pl and not in the same file?

4
  • If I understand you correctly, you wish to pass the $ var into $logs and use it. One way is to implement $logs as a function i.e setLogs($var){$logs=$var}; Let me know if this answers your query Commented Feb 3, 2016 at 5:41
  • Actually in my case, def is a data file, which can contain only data. So I used a hash "$logs". and this hash has many such entries. The string value in this hash has to be in data file only with $var. Commented Feb 3, 2016 at 5:50
  • 1
    It's hard to tell, but it sounds like what you want is sprintf. Commented Feb 3, 2016 at 5:54
  • 1
    As Matt stated, sprintf can be used. In def file instead of $var, make it %s and when you get the hash value in abc.pl, sprintf it with $var. Commented Feb 3, 2016 at 6:00

1 Answer 1

1

String are interoperated when they are defined. In the case of a package being used this will happen at compile time. What you will need to do is to ensure that the strings definition is delayed until after $var is set.

For example this is a way.

package Def;

use strict;

our $logs = {
   'search' => sub {
        my ($var) = @_;

        return "text to be search with $var";
   }
};

Then you can use it like this.

use def;
use strict;

my $log = $def::logs;
my $text = "this is the text to be searched with value123";

my $var = "value123";
my $findstring = $log->{'search'}->($var);

find($text, $findstring);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.