Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: List::BinarySearch and conditional tests with Test::More

by tobyink (Canon)
on Nov 30, 2014 at 01:23 UTC ( [id://1108760]=note: print w/replies, xml ) Need Help??


in reply to List::BinarySearch and conditional tests with Test::More

Don't call import because that only makes sense at compile-time. Call the binsearch function using its full name, and forcibly ignore its prototype...

SKIP: { eval { require List::BinarySearch } or skip "List::BinarySearch not installed", 2; $index = &List::BinarySearch::binsearch( sub {$a <=> $b}, 300, @num_array, ); # ... }

Alternatively, split the tests which use binsearch into a separate test file, and in that file use:

use Test::Requires "List::BinarySearch";

... which will automatically do the right thing in terms of skipping, importing at compile time, etc.

Replies are listed 'Best First'.
Re^2: List::BinarySearch and conditional tests with Test::More
by davido (Cardinal) on Nov 30, 2014 at 07:01 UTC

    That should be...

    $index = &List::BinarySearch::binsearch( sub {$a <=> $b}, 300, \@num_array, );

    The difference from your original suggestion is that one should pass the array by reference.

    Q: Why would the crazy author want the array parameter to be passed by reference? Doesn't he know this can be inconvenient? :) A: Because passing a list is an O(n) operation, whereas passing a reference is O(1). We wouldn't want the passing of parameters to have an order of growth that is worse than the order of growth of the underlying algorithm.

    Of course you already know this, but it's worth mentioning in general.


    Dave

Re^2: List::BinarySearch and conditional tests with Test::More
by glasswalk3r (Friar) on Nov 30, 2014 at 17:40 UTC

    Thank you tobyink.

    Anyway, I thought that the idiom of calling a function with ampersand as something not used anymore.

    Doing a bit of reasearch I found this blog. As I'm always calling a function with "()" I never had any problem with that.

    Anyway... why the & operator is needed in this case? Since I already imported the module with require the code of the function should be already available...

    Alceu Rodrigues de Freitas Junior
    ---------------------------------
    "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

      Prototypes alter how a function call is parsed, but need to do so at compile time. So the interpretation of the function call depends on whether List::BinarySearch has already been loaded or not at the time your call to it is compiled.

      Because you don't know whether or not List::BinarySearch, it is safer to avoid the issue by bypassing prototypes (which is what the & does).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found