Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I write a new sub in a git cloned CPAN module and test it bypassing the Original Module

by yysachinyy (Novice)
on Dec 20, 2016 at 15:58 UTC ( [id://1178218]=perlquestion: print w/replies, xml ) Need Help??

yysachinyy has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I git cloned DateTime module from cpan. I already had a DateTime installed. I added a new method in DateTime.pm and worte a test for a new method added. Now when I run the script it uses the already installed DateTime module and not the one I have modified and get error "undefined sub new_subname".

I tried setting PERL5LIB and use lib pragma so that the test script finds modifed cloned DateTime.pm but din't help.

I would like to know how Perl gurus/monks solve this issue and contribute to CPAN Module.
Thanks.
  • Comment on How do I write a new sub in a git cloned CPAN module and test it bypassing the Original Module

Replies are listed 'Best First'.
Re: How do I write a new sub in a git cloned CPAN module and test it by bypassing the Original Module
by davido (Cardinal) on Dec 20, 2016 at 16:17 UTC

    DateTime is an object oriented module. If you are forking it you may be doing it wrong.

    Rather than fork DateTime, subclass it:

    package MyDateTime; use parent qw(DateTime); sub is_leap_year { my $self = shift; my $ly = $self->SUPER::is_leap_year; return ($ly, ($ly ? 'intercalary' : 'standard')); } 1; package main; use MyDateTime; my $dt = MyDateTime->new(...); my @ly = $dt->is_leap_year; print "This is ", ($ly[0] ? 'leap ' : 'a normal '), "year, otherwise k +nown as $ly[1]\n";

    The advantage to NOT modifying a locally adapted version of a CPAN module is that you won't have to keep abreast of future development for that module -- you won't be stuck either maintaining a fork of the module, or letting it grow stale.

    By subclassing you get to leave the original module alone, so that it is free to continue evolving with bugfixes. As long as your subclass doesn't muck with the parent class's internals, and as long as the authors of DateTime don't break its calling interface, your subclass will continue to work even if the parent is updated with future bugfixes. Plus you don't have to worry about which version Perl is loading (the issue you're currently dealing with, but would certainly deal with again if you go the route of maintaining a patched version of DateTime).


    Dave

      Thank you so much Dave. I am really touched that the author of the module himself replied my question. Awesome! Now understan why SawyerX had praised you in this video The Joy in What we Do

      I am looking to contribute to the module itself rather than sub classing it. I found the way in other monks reply. I will try out. Thanks again. :)

        I'm sorry that there's probably a misunderstanding on both sides. I didn't recognize that your plan was to contribute to the module, which is a different story entirely. If that's your intent, then I'm glad one of the other monks' solutions worked for you.

        I do want to set the record straight though: While it would be an honor to be mistaken for Dave Rolsky, I'm a different Dave (Oswald).


        Dave

Re: How do I write a new sub in a git cloned CPAN module and test it by bypassing the Original Module
by kcott (Archbishop) on Dec 20, 2016 at 19:10 UTC

    G'day yysachinyy,

    Welcome to the Monastery.

    "I tried setting PERL5LIB and use lib pragma ... but din't help." [sic]

    You don't show exactly what you did: here's some pointers.

    • PERL5LIB needs to be set before you call your scripts and made available to them (e.g. via export).
    • use lib should be used before use DateTime; be careful of relative paths.
    • Check that @INC has the path you set: consider doing so in a CHECK or INIT block.
    • Are you running in "taint mode"? See -T and -t in perlrun. See perlsec and, in particular, the "Taint mode and @INC" section.
    • Are you using the warnings pragma?
    "I would like to know how Perl gurus/monks solve and contribute to CPAN Module."

    My basic process would be as follows. I'll reference the current DateTime module; however, this would be what I'd generally use for any module. You may have different methods for achieving each step — I'm certainly not trying to recommend you use different tools — but the order of steps should be much the same. And, of course, the commands I've used are just examples.

    1. Set up a work area.
      $ cd /some/base/work/dir $ mkdir DateTime $ cd DateTime
    2. Get the currrent distribution.
      $ wget http://search.cpan.org/CPAN/authors/id/D/DR/DROLSKY/DateTime-1. +41.tar.gz $ tar zxvf DateTime-1.41.tar.gz > DateTime-1.41.INSTALL 2>&1 $ view DateTime-1.41.INSTALL # Check there's no errors $ cd DateTime-1.41
    3. Read any relevant documentation.
      $ view README # That's "README.md" for this distro $ view INSTALL
    4. Modify the module code.
      $ cd lib $ vi DateTime.pm
    5. Write new test(s).
      $ cd ../t $ vi new_test.t
    6. Create Makefile, build and test.
      $ cd .. $ perl Makefile.PL $ make $ make test
    7. Repeat the last three steps until the code does what you want and the tests all pass.
      $ make realclean $ # ... repeat commands already shown ...

    See also: "PAUSE: The [Perl programming] Authors Upload Server".

    — Ken

      Thanks Ken.

      This is what I am exactly looking for. I will try out.

      thank you. :)
Re: How do I write a new sub in a git cloned CPAN module and test it by bypassing the Original Module
by VinsWorldcom (Prior) on Dec 20, 2016 at 18:28 UTC

    You could run the specific test you want by:

    perl Makefile.PL make perl -Mblib t/testfile.t

    That will execute t/testfile.t 'use'-ing blib as the source for your edited DateTime.pm module. Note 'blib' and subdirectories get created when you run 'make'.

      Thanks. This works for me.

Re: How do I write a new sub in a git cloned CPAN module and test it by bypassing the Original Module
by hippo (Bishop) on Dec 20, 2016 at 16:09 UTC
    I git cloned DateTime module from cpan. I already had a DateTime installed. I added a new method in DateTime.pm and worte a test for a new method added.

    All good so far.

    Now when I tried to run the script it uses the already installed DateTime module and not the one I have modified.

    How did you run the test script? By executing make test or some other means?

      I used 'prove' to run the test.
        > I used prove

        With the -l option?

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1178218]
Approved by erzuuli
Front-paged by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found