Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can a perl variable replace a small code segment?

Status
Not open for further replies.

supernova83

Programmer
Joined
Nov 12, 2006
Messages
3
Location
RO

Consider I have a variable $address_combined to which I'd like to assign the result of a string concatenation.
To give a concrete example, let's say I also have a variable $owner_address which is the very string '$row->{situs_no}." ".$row->{situs_dir}." ".$row->{situs_name}' as read from a file.

What I like to obtain is a code equivalent to:
$address_combined = $row->{situs_no}." ".$row->{situs_dir}." ".$row->{situs_name}
by just using the $owner_address variable.

Is that possible?

Thank you for your time.
Mihai
 
You could try eval (be careful though; if the data read in could be manipulated in any way by somebody other than you, eval can be dangerous)

Code:
my $line = '$row->{situs_no}." ".$row->{situs_dir}." ".$row->{situs_name}';

eval ("my \$address_combined = $line");

-------------
Kirsle.net | Kirsle's Programs and Projects
 
I don't really understand what benefit this gives. I'm assuming that you need to obtain this value at numerous places in your program, kind of like a macro (otherwise it wouldn't be worth doing).

There are better ways to do this without resorting to eval. Even something as crude as
Code:
$row->{address_combined} = $row->{situs_no}." ".$row->{situs_dir}." ".$row->{situs_name};
would make the value accessible to any part of your program that has scope visibility on %row, which makes it at least as good as the eval solution. If your row is the result of an SQL query, you can even assemble the catenated value as an extra column with the AS clause.

Just because perl lets you do this kind of thing, doesn't necessarily mean it's a good idea...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
I agree with Kirsle and stevexff concerning the danger in using eval and the unclarity of what exactly you are trying to do, and whether it's a good idea.

Nevertheless, to implement what you ask using the framework that you predefined, eval is the simpliest method. To make this more efficient, you should create a subroutine reference so that you only have to eval the code once.

Code:
# Data
my $row = {situs_no => 'foo', situs_dir => 'bar', situs_name => 'baz'};
my $line = '$row->{situs_no}." ".$row->{situs_dir}." ".$row->{situs_name}';

# Kirsle's Method (simplified)
my $address_combined = eval $line;
print "Using repeated eval: $address_combined\n";

# Subroutine Method
my $address_combined_sub = eval "sub { $line }";
my $address_combined = $address_combined_sub->();
print "Using sub reference: $address_combined\n";

You can run $address_combined_sub->() as many times as you want and it will process the current values of the $row hash reference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top