Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Equivalency of Code

by stvn (Monsignor)
on Feb 07, 2005 at 11:01 UTC ( #428624=note: print w/replies, xml ) Need Help??

in reply to Equivalency of Code

You might want to give Perl::Compare a look.


Actually it seems that Perl::Compare might not be in the best state right now (it seems he is working on updating it to the latest PPI version). A simplistic replacement is using the normalized method of the PPI::Document object and then comparing the results (the results are actually PPI::Document::Normalized objects which have their == operators overloaded). Here is some code:

use strict; use warnings; use PPI::Document; my $d1 = PPI::Document->new('$x++'); my $d2 = PPI::Document->new('$x += 1'); my $d3 = PPI::Document->new('$x = x + 1'); print((($d1->normalized() == $d2->normalized) && ($d2->normalized() == $d3->normalized) && ($d3->normalized() == $d1->normalized)) ? "they are all equivalent\n" : "they are not equvalient\n"); print(($d1->normalized() == $d2->normalized) ? "1 and 2 are equivalent\n" : "1 and 2 are not equvalient\n"); print(($d2->normalized() == $d3->normalized) ? "2 and 3 are equivalent\n" : "2 and 3 are not equvalient\n"); print(($d3->normalized() == $d1->normalized) ? "3 and 1 are equivalent\n" : "3 and 1 are not equvalient\n");
And the output:
they are not equvalient 1 and 2 are not equvalient 2 and 3 are not equvalient 3 and 1 are not equvalient


Replies are listed 'Best First'.
Re^2: Equivalency of Code
by adamk (Chaplain) on Feb 08, 2005 at 00:19 UTC
    You couldn't have picked a worse time to mention Perl::Compare really.

    The normalization overhaul has taken something that did about 10-15 normalization "things" and now only has one (removal of insignificant things like whitespace/pod/comments).

    The original (and still current to some degree) intent of PPI-style normalization is to check to see if changes to code "matter" or not.

    For example this:
    my $foo = 1; # comment
    Is "equivalent" to this:
    my $foo=1 ;
    As the library of normalization functions grows back towards (and past) what the original and now abandoned Perl::Compare implementation had, it is only really intended to factor out changes like:
    'foo' ---> "foo" "foo$bar" ---> "foo${bar}"; etc... etc...
    One idea was to let other people edit "your" code, while being certain that they haven't actually changed anything.

    I'm sure you can think of others.

    In any case, the three above example may well be different, especially if they are object with overloaded operators.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://428624]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-05-26 02:08 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (92 votes). Check out past polls.