Trying to parse a variable name

Discussion in 'Perl Misc' 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. [ Did not reply to the defunct group comp.lang.perl. ]

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


    <snip>

    > sub parseValue
    > {
    > my $value = shift;
    > $value =~ s/\$(\w+)/${$1}/;
    > return $value;
    > }


    Then you don't have strictures enabled, and you are in the business of
    symbolic references. :( There are better methods to achive the same
    thing, such as plain replacements based on a set of keys/values in a
    hash.

    > when I pass in the value from the XML file it doesn't work.


    <snip>

    Is it this line you are talking about?

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


    Then you may want to check the Perl FAQ:

    perldoc -q "expand function calls"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 17, 2004
    #2
    1. Advertising

  3. Aaron

    Eric Bohlman Guest

    Gunnar Hjalmarsson <> wrote in news:2oesn4Fa2edpU1@uni-
    berlin.de:

    > Is it this line you are talking about?
    >
    >> $delete_predicate =~ s/(\$\w+)/&parseValue($1)/g;

    >
    > Then you may want to check the Perl FAQ:
    >
    > perldoc -q "expand function calls"


    Actually, I think a visit to "quotes and quote-like operators" in perlop,
    specifically the description of the s operator, would be more in order. I
    wouldn't look too kindly on any code that uses one of the methods from that
    particular FAQ in the replacement part of an s.

    Oh, and I'd also recommend the OP read the posting guidelines, particularly
    the parts about letting perl give you all the help it can and about posting
    actual code (the original post mentioned output that contained literal
    strings not present in the posted code).
    Eric Bohlman, Aug 17, 2004
    #3
  4. Eric Bohlman wrote:
    > Gunnar Hjalmarsson wrote:
    >> Is it this line you are talking about?
    >>
    >>> $delete_predicate =~ s/(\$\w+)/&parseValue($1)/g;

    >>
    >> Then you may want to check the Perl FAQ:
    >>
    >> perldoc -q "expand function calls"

    >
    > Actually, I think a visit to "quotes and quote-like operators" in
    > perlop, specifically the description of the s operator, would be
    > more in order.


    Yeah, I should have mentioned that, too.

    > I wouldn't look too kindly on any code that uses one of the methods
    > from that particular FAQ in the replacement part of an s.


    Now I'm not following you. Actually, it only mentions one method. Why
    would you object to

    s/to_be_replaced/@{ [ somefunction() ] }/

    ?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 17, 2004
    #4
  5. Aaron

    Paul Lalli Guest

    On Tue, 17 Aug 2004, Gunnar Hjalmarsson wrote:

    > Eric Bohlman wrote:
    > > Gunnar Hjalmarsson wrote:
    > >> Is it this line you are talking about?
    > >>
    > >>> $delete_predicate =~ s/(\$\w+)/&parseValue($1)/g;
    > >>
    > >> Then you may want to check the Perl FAQ:
    > >>
    > >> perldoc -q "expand function calls"

    > >
    > > Actually, I think a visit to "quotes and quote-like operators" in
    > > perlop, specifically the description of the s operator, would be
    > > more in order.

    >
    > Yeah, I should have mentioned that, too.
    >
    > > I wouldn't look too kindly on any code that uses one of the methods
    > > from that particular FAQ in the replacement part of an s.

    >
    > Now I'm not following you. Actually, it only mentions one method. Why
    > would you object to
    >
    > s/to_be_replaced/@{ [ somefunction() ] }/


    I'd object to that because 1) it's ugly, 2) it's non-intuitive, 3) there's
    a 'better' (more standard?) way.

    s/to_be_replaced/somefunction()/e

    But that's just me.

    Paul Lalli
    Paul Lalli, Aug 17, 2004
    #5
  6. Paul Lalli wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> Why would you object to
    >>
    >> s/to_be_replaced/@{ [ somefunction() ] }/

    >
    > I'd object to that because 1) it's ugly, 2) it's non-intuitive, 3)
    > there's a 'better' (more standard?) way.
    >
    > s/to_be_replaced/somefunction()/e


    Well, in that simple case, I would have done so, too.

    But let's assume that the return value from the function is only a
    part of the replacement string:

    s/to_be_replaced/start @{ [ somefunction() ] } end/

    s/to_be_replaced/'start ' . somefunction() . ' end'/e

    Which is best is no longer *that* obvious, is it? ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 17, 2004
    #6
  7. Aaron

    Eric Bohlman Guest

    Gunnar Hjalmarsson <> wrote in news:2of8kiFa6jqmU1@uni-
    berlin.de:

    > Well, in that simple case, I would have done so, too.
    >
    > But let's assume that the return value from the function is only a
    > part of the replacement string:
    >
    > s/to_be_replaced/start @{ [ somefunction() ] } end/
    >
    > s/to_be_replaced/'start ' . somefunction() . ' end'/e
    >
    > Which is best is no longer *that* obvious, is it? ;-)


    I'd prefer s/to_be_replaced/sprintf('start %s end',somefunction())/e;

    I'm just not the type who values using tricks for things that have standard
    solutions.
    Eric Bohlman, Aug 18, 2004
    #7
    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. Aaron
    Replies:
    1
    Views:
    533
    Gunnar Hjalmarsson
    Aug 17, 2004
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,390
    Joe Smith
    Apr 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    699
    Roberto Bonvallet
    Sep 12, 2007
  4. Jesse P.
    Replies:
    0
    Views:
    180
    Jesse P.
    Jan 4, 2008
  5. David Filmer
    Replies:
    19
    Views:
    240
    Kevin Collins
    May 21, 2004
Loading...

Share This Page