in reply to Re: Regular expression match and substitution
in thread Regular expression match and substitution

Thanks for the reply....

I will be using eval on the string later on. However, before executing the eval I ensure that all variables have been defined. Why isn't this a good idea?

Does eval() work on string's?

$b = "hello"

$strconcat = "$b_world"

after subs...

$strconcat = "$sym{'$b'}_world"

<code> $tmp = eval("$sym{'$b'}_world") print "\$strconcat = $tmp\n"; <\code>

I get the following error Use of uninitialized value in concatenation (.) or string at ./genParse.pl line 418, <DATA> chunk 1.

Thanks!

  • Comment on Re^2: Regular expression match and substitution

Replies are listed 'Best First'.
Re^3: Regular expression match and substitution
by ww (Archbishop) on Nov 09, 2004 at 19:09 UTC
    not "a good idea," cuz you'll be setting eval to work on what may be malicious input from user. read about untainting and generally, security.
Re^3: Regular expression match and substitution
by ikegami (Patriarch) on Nov 09, 2004 at 19:20 UTC

    It's not a good idea because it's too easy to inject code. Even if the current user of the code wouldn't do so, it might eventually make its way to where an untrusted user supplies the data. A parser is the easiest way to make sure the input is safe.

    You're getting "Use of uninitialized value" error because you didn't give any value to $sym{'$b'}.

    yes, eval works on strings:

    $expr = '$x + 1'; print("$expr = "); $expr =~ s/(\$\w+)/\$sym{'$1'}/g; $sym{'$x'} = 3; $val = eval $expr; die("Bad equation: $@$/") if $@; print("$val$/");

    But please don't do that!

      $sym{'$b'} is already defined.

      my $p = "$sym{'$b'}/helloworld/test.dat"; $sym{'$b'} = "my"; my $val = eval $p; print "p = $val\n";

      This doesn't work as it should.

      -P

        For starters, my/helloworld/test.dat is not valid Perl. (bare word my divided by bare word helloworld divided by bare word test concatenated with bare word dat?) So add quotes.

        Two, you want the $vars to be in $p instead of being interpolated, so use single quotes or escape the $ (and any @) in the literal.

        my $p = "\"\$sym{'\$b'}/helloworld/test.dat\""; # my $p = 'qq[$sym{'$b'}/helloworld/test.dat]'; # alternative # my $p = '"$sym{\'$b\'}/helloworld/test.dat"'; # alternative # my $p = q["$sym{'$b'}/helloworld/test.dat"]; # alternative $sym{'$b'} = "my"; my $val = eval $p; print "p = $val\n";