Trying to parse a variable name

Discussion in 'Perl' started by Aaron, Aug 17, 2004.

  1. Aaron

    Aaron Guest

    Basically I'm reading a SQL predicate from an XML file. The predicate
    contains a perl variable name.

    I have a sub called parseValue that takes a literal string and then
    returns the value.

    Now, if I pass in a qq{} or ' ' quoted string it works fine, but when
    I pass in the value from the XML file it doesn't work.

    I should get the following output:

    PARSED STRING: ENGINE_ID = 1
    PARSED STRING: ENGINE_ID = 1

    But I get the following:

    PARSED STRING: ENGINE_ID = 1
    PARSED STRING: ENGINE_ID = &parseValue($engine_id)

    What the heck is going on?

    I've included the code and the XML file below.

    #!/usr/bin/perl

    use XML::Simple;
    use Data::Dumper;

    $engine_id = 1;

    sub parseValue
    {
    my $value = shift;

    $value =~ s/\$(\w+)/${$1}/;

    return $value;
    }

    print "PARSED VALUE: ",&parseValue('$engine_id'),"\n";

    sub BySequence
    {
    $transforms->{Transform}->{$a}->{Sequence} <=>
    $transforms->{Transform}->{$b}->{Sequence}
    }

    $transforms = XMLin("test.xml",forcearray => ["Transform"]);

    foreach $transform (sort BySequence keys %{$transforms->{Transform}})
    {
    %transform = %{$transforms->{Transform}->{$transform}};

    $delete_predicate = $transform{DeletePredicate};

    print "$delete_predicate\n";
    $delete_predicate =~ s/(\$\w+)/&parseValue($1)/g;
    print "$delete_predicate\n";

    %transform = ();
    }

    <?xml version="1.0" ?>
    <Transforms>
    <Transform Sequence="1" name="Planned Order">
    <DeletePredicate>ENGINE_ID =
    $engine_id</DeletePredicate>
    </Transform>
    </Transforms>
     
    Aaron, Aug 17, 2004
    #1
    1. Advertising

  2. Gunnar Hjalmarsson, Aug 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Chamness
    Replies:
    2
    Views:
    2,402
    Joe Smith
    Apr 22, 2007
  2. mfglinux
    Replies:
    11
    Views:
    710
    Roberto Bonvallet
    Sep 12, 2007
  3. Jesse P.
    Replies:
    0
    Views:
    181
    Jesse P.
    Jan 4, 2008
  4. David Filmer
    Replies:
    19
    Views:
    248
    Kevin Collins
    May 21, 2004
  5. Aaron

    Trying to parse a variable name

    Aaron, Aug 17, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    107
    Eric Bohlman
    Aug 18, 2004
Loading...

Share This Page