Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Introspecting function signatures

by LanX (Saint)
on Mar 06, 2021 at 15:09 UTC ( [id://11129194]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Introspecting function signatures
in thread Introspecting function signatures

I may have trouble understanding the full intention.... please help me:

You want to parse

sub test_someting($tempdir) { # do some tests }

And because you see the (registered) keyword "tempdir" your test system is supposed to call this test with a prepared temporary directory?

ok( test_something( create_tempdir() ), "something", )

Kind of a naming convention?

Did I get it right?

If yes, I could think of some alternative (and more perlish) ways to achieve this without needing to parse the signature.

PS: not sure why you call this "dependency injection" or do you plan testruns where the injected $tempdir is not a temporary dir? :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^6: Introspecting function signatures
by szabgab (Priest) on Mar 06, 2021 at 21:04 UTC
    Yes. "dependency injection" is how this is called in the case of pytest, maybe it is not a generic term. You could create your own "fixture" and then you or others could set its name as a parameter.

    I put together an example of creating and registering a fixture and then also use that fixture.

    I would love to see your suggestion for more Perlish way.

        I think this niche is mainly filled by conditional use/imports in dynamic languages (see if )

        Monkey patching is only one other possibility here (that's replacing a sub/method at runtime), we also have eval and BEGIN blocks ... etc.

        > In frustration, I searched all

        that's "terminology injection" (aka die "brain overflow" ;-)

        Like all these Java OO "design patterns" which are superfluous in Perl.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      My suggestion is to just have the test routine call back into the framework:

      sub test_something { my $tempdir = create_tempdir(); # ... }

      Then the framework only needs to iterate over the package stash and call all test_* functions. Which leads to a "is this a good idea?" question — this style of testing means that the test routines are included with the main code and will have to be compiled every time the module is loaded. Python reduces this overhead by automatically using its form of B::Bytecode/ByteLoader but Perl does not do that, so this style will add some incremental overhead to all use of any module using it. The traditional Perl style of separate test scripts completely avoids this last problem.

        Not sure what Python is implementing with that pattern, but I know it doesn't have block-scope, OTOH nesting functions is easier there.

        So many things which are just blocks in Perl become named functions there, even lamda is useless for that.

        But no need to copy their limitations.

        My suggestion would be to apply attributes or explicit use

        sub :test something { my :TEMPDIR $tempdir = shift; # or my :FIXTURE $tempdir = shift; # or use fixture qw/$tempdir/; #... }

        This offers plenty of possibilities at compile time and is more flexible than Python's approach.

        edit

        and I'd certainly put test-subs into their own package, if they lived in the same file.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        This can indeed be a good solution. Given that perl will reliably call DESTROY when an object goes out of scope, this could ensure that whatever the object holds will be cleaned up. It is a bit more code to write as we need to call the function, but might be reasonable.
        sub test_something { my $tempdir = create_tempdir(); # ... }

        I don't know where the idea came from that in Python people include the tests in the main code. Maybe that was the case 10 years ago, but these days the standard is that tests are in separate files called test_SOMETHING.py.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2025-03-20 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (61 votes). Check out past polls.