Description: | Suppose you have a string with variable names in it. You want them to be expanded when you print it, but you're not sure how. Never fear, you can abuse eval()! |
my $color = "red"; my $fruit = "apple"; my $name = "chromatic"; my $string = 'Hi, my name is $name. Please hand me a $color $fruit.'; print ">>$string<<\n"; # demonstrate what we have my $s2; eval "\$s2 = qq/$string/"; # the real magic print "->$s2<-\n"; # demonstrate the result
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: Double Interpolation of a String
by davorg (Chancellor) on Jun 22, 2000 at 14:30 UTC | |
by tadman (Prior) on Feb 07, 2001 at 00:30 UTC | |
by Fastolfe (Vicar) on Feb 07, 2001 at 00:34 UTC | |
Re: Double Interpolation of a String
by nontrivial (Novice) on Aug 21, 2001 at 06:20 UTC | |
by chromatic (Archbishop) on Aug 21, 2001 at 07:47 UTC | |
Re: Double Interpolation of a String
by 5mi11er (Deacon) on Feb 01, 2007 at 22:26 UTC |