Re: [OT] Perl: exec and $variables

2001-07-22 Thread Sven Burgener
On Sat, Jul 21, 2001 at 04:53:35PM +0200, Joost Kooij wrote:
 What is the need for the seperate variable $BEGINREGEX?  It
 complicates things enormously when you want a variable $no to be
 evaluated whenever $BEGINREGEX is evaluated.  The only sane way out is
 to completely reevaluate $BEGINREGEX after each change to $no.  To do
 that successfully, you have to escape '$', '', and '\' and then
 escape some of the escapes, but others not, depending on wheter they
 should never be expanded, expanded in the eval or expanded when
 applying the regexp.  I wouldn't touch that with a ten foot pole if I
 were you.  If you succeed at it, you have great job security, and a
 maintenance nightmare.

Right, I agree.

 Easier is to not use a $BEGINREGEX at all:
 
   $line =~ m(^!-- // begin of news$no // !--$);
 
 should always work, for the current value of $no.

Which is what I ended up doing.

I made a function getBeginRegex() and getEndRegex() which return the
string I then use as a regex for searching.
This way I don't have to write the regex string many times over in the
program, which is what I was trying to avoid when I did the $BEGINREGEX
thing.

Thanks a lot for your help!

Sven



Re: [OT] Perl: exec and $variables

2001-07-22 Thread Sven Burgener
On Sat, Jul 21, 2001 at 10:36:55AM -0500, Andrew Perrin wrote:
 my $template = '^!-- // begin of news%no% // !--$';
 my $no = 99;
 my $bla = $template;
 $bla =~ s/%no%/$no/g;
 
 
 $replace{no} = 99;
 $bla =~ s/%(.+?)%/$replace{$1}/g;
 
 Disclaimer: these are trivial and not terribly robust solutions; take them
 as a starting point, not a complete solution.

I didn't do things your way, but I can always re-use your ideas sometime
later. :-)

Thanks for your help!

Sven



[OT] Perl: exec and $variables

2001-07-21 Thread Sven Burgener
Hello

I have a problem with some perl code. I know this is off-topic, but
there are numerous knowledgeable people on deb-usr, so forgive me for
posting this.

Now to my problem.

Given the following variable,

my $BEGINREGEX   = sprintf(\^!-- // begin of news\$no // !--\$\);

the following eval() call doesn't substitute $no for what it is and
return the new string with the substituted $no in it:

my $no = 1;
my $bla = eval($BEGINREGEX);
print $bla\n;

$bla is empty for some reason.

So how would I go about having $no substituted in $BEGINREGEX for
whatever it happens to be set to at a particular point in the script?

Cheers!
Sven
-- 
The best way to escape from a problem is to solve it. 



Re: [OT] Perl: exec and $variables

2001-07-21 Thread Joost Kooij
On Sat, Jul 21, 2001 at 01:04:40PM +0200, Sven Burgener wrote:
 I have a problem with some perl code. I know this is off-topic, but
 there are numerous knowledgeable people on deb-usr, so forgive me for
 posting this.
 
 Now to my problem.
 
 Given the following variable,
 
 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.

 the following eval() call doesn't substitute $no for what it is and
 return the new string with the substituted $no in it:
 
 my $no = 1;
 my $bla = eval($BEGINREGEX);
 print $bla\n;
 
 $bla is empty for some reason.
 
 So how would I go about having $no substituted in $BEGINREGEX for
 whatever it happens to be set to at a particular point in the script?

You probably do not want to use eval here, or at least not in this way.

Cheers,


Joost



Re: [OT] Perl: exec and $variables

2001-07-21 Thread Sven Burgener
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. 



Re: [OT] Perl: exec and $variables

2001-07-21 Thread Andrew Perrin
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 - [EMAIL PROTECTED] - 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 [EMAIL PROTECTED] 
 with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
 
 



Re: [OT] Perl: exec and $variables

2001-07-21 Thread Joost Kooij
On Sat, Jul 21, 2001 at 02:31:58PM +0200, 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.
 
You are still not telling really what you want to accomplish, but I infer
that you want to match lines like:

  !-- // begin of news1 // !--

To test if the entire $line matches it, you would write:

  $line =~ m(^!-- // begin of news1 // !--$);

Notice that I used the m operator explicitly, so I can use an alternate
regexp delimiter, or else I would have had to escape each of the slashes
in your pattern.

What is the need for the seperate variable $BEGINREGEX?  It complicates
things enormously when you want a variable $no to be evaluated whenever
$BEGINREGEX is evaluated.  The only sane way out is to completely reevaluate
$BEGINREGEX after each change to $no.  To do that successfully, you have
to escape '$', '', and '\' and then escape some of the escapes, but others
not, depending on wheter they should never be expanded, expanded in the 
eval or expanded when applying the regexp.  I wouldn't touch that with a 
ten foot pole if I were you.  If you succeed at it, you have great job
security, and a maintenance nightmare.

Easier is to not use a $BEGINREGEX at all:

  $line =~ m(^!-- // begin of news$no // !--$);

should always work, for the current value of $no.

   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 // !--$

Why are you putting the sprintf in the regexp at all?  The '^' and '$' anchors
only work when at the begin, resp. at the end of the whole regexp.  I think 
that the use of sprintf is unnecessary, and even complicates things enormously.
 
 But it isn't, so what am I doing wrong here?

AFAICS, you're just not doing it in the most straightforward way.
Try to use fixed regexps, and leave the $no in it, so it will be expanded
every time perl uses the regexp.

Cheers,


Joost