Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Tripwire tests and future-proofing

by BUU (Prior)
on May 25, 2004 at 09:20 UTC ( [id://356147]=note: print w/replies, xml ) Need Help??


in reply to Re: Tripwire tests and future-proofing
in thread Tripwire tests and future-proofing

It's simple. ok($$) is a subroutien exported by one of the Test modules. It takes two arguements and compares them for equality. If they're equal, the test passes, if not, the test doesn't pass.

As to the specifics of his code, theres really two main parts. The inner most part is the keys map { $_->product_name() => } @classes }. This does two things. First the map statement is evaluated and loops through the @classes array, and produces a new list, consisting of every element in @classes interspersed with 1s. So the new list looks like (foo,1,bar,1,baz,1), where foo, bar, and baz are class names in @classes. Then the keys function treats the list that map returns as a hash, and creates a list consisting of all the keys in the new hash. Note that there is some implicit magic going on here, in that a hash can not have two keys with the same name, so if two such keys exists, the first one is ignored and only the last one is returned. This has the effect of returning only a unique list of values.

Then the list is simply turned in to a number by the scalar operator and compared to the scalar value of @classes. If they are different, then @classes contains multiple items with the same name.

In bash/linux terms, you might think of it like this: (Warning, pseudo code)
$lc1 = wc -l my_file.txt; $lc2 = sort my_file.txt | uniq | wc -l; if( $lc1 != $lc2 ) { #test fails } else { #test good! }


Update:
I played around with the example code and, in the presented form, I can't get it to even compile:
>perl -e"@x=qw/foo bar/; print scalar keys map { $_ => 1 } @x;"
Produces:
Type of arg 1 to keys must be hash (not map iterator) at -e line 1, ne +ar "@x;" Execution of -e aborted due to compilation errors.


This is a trivial "bug" to fix, you simply enclose the map in %{{}} (Which first creates an anonymous hash reference out of a list, then derefences it back in to a hash, which keys will work upon), but in the presented form it doesn't appear to work. This is perl, v5.8.3 built for MSWin32-x86-multi-thread.

Replies are listed 'Best First'.
Re: Re: Re: Tripwire tests and future-proofing
by dws (Chancellor) on May 25, 2004 at 16:41 UTC

    This is a trivial "bug" to fix, you simply enclose the map in %{{}}

    Yeah. That's the problem with late-night transcriptions where proprietary bits need to be discarded. Fixed above.

Re: Re: Re: Tripwire tests and future-proofing
by zby (Vicar) on May 25, 2004 at 09:40 UTC
    From Test::More:
    ok($this eq $that, $test_name);
    This simply evaluates any expression ($this eq $that is just a simple example) and uses that to determine if the test succeeded or failed. A true expression passes, a false one fails. Very simple.
    So it's not exactly what you describe.

    Additionally a list in scalar context returns it's last value not the number of it's elements. So your explanation of what happens after the keys function is not right.

    Update:

    $ perl -e '%h = (1, 1, 1, 1); print scalar keys %h, "\n"' 1
    $ perl -e 'print scalar (1, "a"), "\n";' a
      So it's not exactly what you describe.

      As BUU said in his post this is the ok from Test, not the one from Test::More.

        Thanks. And what with the other question?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-18 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found