Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How can one re-parse(?) a string to fill in variables

by kilinrax (Deacon)
on Nov 23, 2000 at 23:07 UTC ( #43154=note: print w/ replies, xml ) Need Help??


in reply to How can one re-parse(?) a string to fill in variables

You're question is a little vague - but I'm guessing the problem is that you have code somewhat like the following (which i'll use as an example):

#!/usr/bin/perl -w use strict; my $name = 'Alex'; my $greeting = 'Hello $name!'; print $greeting;
Which you want to print 'Hello Alex', but currently it's printing 'Hello $name!'.

The following code will do this, though is perhaps a little hack-ish, and some of our more experienced monks could enlighten both of us as to a more elegant method:
#!/usr/bin/perl -w use strict; my $name = 'Alex'; my $greeting = 'Hello $name!'; $greeting =~ s|\$([\w_]+)|eval "\${$1}"|e; print $greeting;


Comment on Re: How can one re-parse(?) a string to fill in variables
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://43154]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2015-07-03 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (54 votes), past polls