Dan Huston am Dienstag, 10. Januar 2006 21.40: > Greetings -- > > I am reading text sql commands) from a file, chopping it into sections > and then writing each section out to a different file. Within the text > are perl variables that I had expected to be interpolated as they were > written out to the new file but they are not. > > Here is a sample line: > > REVOKE SELECT ON $TABLE_NAME FROM PUBLIC; > > $TABLE_NAME for example might be 'STUDENTS'. > > Is there a reason that the text is not being interpolated when before it > is written out to the new file (either when it is read in or when it is > printed out)? > > Here is a piece of my code: > > > ########## CODE SNIPPET ##################### > > for $k ( @these_lines ) { > > # for debugging > print "$TABLE_NAME\n"; > print "K\t=>\t$k\n"; > > ### SQL_OUT is the filehandle that I am writing to > print SQL_OUT "$k\n";
$k _itself_ is interpolated into the double quoted string: $k contains a string with content looking like perl vars, $k is replaced by this string. But there is - luckily! - no second level interpolation included (the variable names _in_ the string _in_ variable $k). You would have to make the second level interpolation explicit my $var='hi!'; print eval "q(He said $var and disappeared)"; =>He said hi! and disappeared but this is DANGEROUS since the text could contain code instead of simple variable names. There may be a useful templating module on search.cpan.org. Or you could reinvent the wheel and parse for the variable names and replace them with values. Something along the lines use strict; use warnings; my %vars=(TABLE_NAME=>'STUDENTS'); my @these_lines=('REVOKE SELECT ON $TABLE_NAME FROM PUBLIC $UNKNOWN;'); for (@these_lines) { s/\$([_a-zA-Z]\w*)/$vars{$1} || '$'.$1/eg; } print @these_lines; > > } ### CLOSE-FOR-K > > > produces this output in STDOUT: > > TABLE_NAME => STUDENTS > K => REVOKE SELECT ON $TABLE_NAME FROM PUBLIC; > > where I want to have : > > K => REVOKE SELECT ON STUDENTS FROM PUBLIC; > > > ########## CODE SNIPPET ##################### > > > Thanks > Dan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>