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

Re^6: How to write testable command line script?

by thechartist (Monk)
on Nov 21, 2018 at 03:31 UTC ( [id://1226100]=note: print w/replies, xml ) Need Help??


in reply to Re^5: How to write testable command line script?
in thread How to write testable command line script?

You last reply was very helpful! Running the test code manually, my debug print statements in the *.pl file show that the @answer array continues to contain the values from previous calls:

  • Test 1. @answer array returns 180 59 58 (test passes).
  • Test 2. @answer array returns 179 1 9 180 59 58 (test fails).
  • Test 3. @answer array returns 180 0 0 179 1 9 180 59 58 (test fails).
  • I tried adding
    my @answer = undef;
    to the test file main sub definition (thinking that this would get cleared on every call) but that did not work. What do you suggest now?

    Replies are listed 'Best First'.
    Re^7: How to write testable command line script?
    by AnomalousMonk (Archbishop) on Nov 21, 2018 at 03:46 UTC
      my @answer = undef;

      General note: I'm not sure what you intend by the quoted statement, but you should realize that it initializes the (newly created) array with a single element, the undef value, which in Perl is a well-defined value! (The  @answer array thus created is also entirely and absolutely independent of any other array with the same name.)

      c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; my @answer = undef; dd \@answer; " [undef]


      Give a man a fish:  <%-{-{-{-<

        I am trying to clear an array via assignment in the test file that is already defined in the script file. It appears that command line usage differs from how the subroutine is called in the test environment.

        On the command line, once the subroutine prints the results and exits, the variables are cleared, so when I enter another expression manually, things work. With the test environment, those values do not get cleared, but remain in the @answer array, and appear to be causing the failure.

        There might be other problems causing the failure too, but the @answer array should have 3 or fewer values in it when running any tests.

        What I thought was going to be a rather simple problem turned out to be much more involved than I thought.

    Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2024-04-24 11:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found