[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] Perl: exec and $variables



eval() doesn't do what you want - it *executes* code, as opposed to
substituting values for variables. Try something like:

my $template = '^<!-- // begin of news%no% // !-->$';
my $no = 99;
my $bla = $template;
$bla =~ s/%no%/$no/g;



You can get fancy too, if you want:

$replace{no} = 99;
$bla =~ s/%(.+?)%/$replace{$1}/g;

now anything encased in % signs will be replaced by the associated value
in the %replace hash.

Disclaimer: these are trivial and not terribly robust solutions; take them
as a starting point, not a complete solution.

----------------------------------------------------------------------
Andrew J Perrin - andrew_perrin@unc.edu - http://www.unc.edu/~aperrin
 Assistant Professor of Sociology, U of North Carolina, Chapel Hill
      269 Hamilton Hall, CB#3210, Chapel Hill, NC 27599-3210 USA


On Sat, 21 Jul 2001, Sven Burgener wrote:

> On Sat, Jul 21, 2001 at 01:46:25PM +0200, Joost Kooij wrote:
> > On Sat, Jul 21, 2001 at 01:04:40PM +0200, Sven Burgener wrote:
> > > my $BEGINREGEX   = "sprintf(\"^<!-- // begin of news\$no // !-->\$\")";
> > 
> > Please tell us what you're trying to accomplish first.  It is unclear
> > what assumptions you are making.
> 
> What I want is the variable $BEGINREGEX to contain a string like so:
> 
> ^<!-- // begin of news1 // !-->$
> 
> or
> 
> ^<!-- // begin of news2 // !-->$
> 
> The digit after the "news" should be whatever $no is set to at that
> point in the script.
> 
> > > my $no = 1;
> > > my $bla = eval($BEGINREGEX);
> > > print "$bla\n";
> > > 
> > > $bla is empty for some reason.
> > 
> > You probably do not want to use eval here, or at least not in this way.
> 
> What should I do then?
> 
> It's simple, really. I am sure I am just making a stupid mistake.
> 
> my $BEGINREGEX = "sprintf(\"^<!-- // begin of news\$no // !-->\$\")";
> my $no = 99;
> my $bla = eval($BEGINREGEX);
> print "regex string: $bla\n";
> 
> What should be printed:
> 
> regex string: ^<!-- // begin of news99 // !-->$
> 
> 
> But it isn't, so what am I doing wrong here?
> 
> Cheers,
> Sven
> -- 
> The best way to escape from a problem is to solve it. 
> 
> 
> -- 
> To UNSUBSCRIBE, email to debian-user-request@lists.debian.org 
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
> 
> 



Reply to: