Perl

Menu

Homework 4
Frequently Asked Questions

  1. Is there any way to test if the output of a method is what we expect it to be?

    Yes, but no good easy ways. One way would be to capture STDOUT, using the CPAN module IO::Capture::Stdout. Another is to run that subroutine in a child process, capturing that child's output.

    Neither of these are especially worth it. Don't bother trying - you can skip the show() and history() methods of the BankAccount class when testing

  2. How do I make perldoc find my modules once I've installed them?

    perldoc looks for module documentation in all the locations where modules are installed globally, plus any directory that the current enviroment specifies. To specify a new directory, you need to set an environment variable, specifically PERL5LIB. How to do this varies by operating system and shell, but for the default shell on rcs-sun4.rpi.edu, issue this command:
    setenv PERL5LIB $HOME/lib/perl5/site_perl
    perldoc Lallip::Averages will then correctly locate your module's documentation

  3. Do the deposits() and withdrawls() methods start with 0 or 1?

    1. If the user says $acct->deposits(3), that means the average of all deposits, starting with the 3rd transaction.

  4. After using the overloaded += or -= operators, Perl seems to think my object is no longer an object. What's going on?

    In an expression such as $foo += $bar, $foo gets assigned the return value of the += operator. Therefore, the overload handler subroutine that gets called for += needs to return what you want $foo to be. So if your code is
    $acct += 50;
    Make sure the += handler returns the actual object that you want $acct to be. (Most likely, this is simply the now-modified object that you used within the handler itself.)

  5. My tests are reporting that the numbers I'm getting for the balances aren't the same as the numbers I'm expecting - but the numbers listed are the same. What's wrong?

    Comparing floating point values for numeric equality is not something that should be done - in any language. Floating point numbers don't compare correctly due to the way they're stored.

    You have three options: 1) Compare the two values as strings instead of as numbers; 2) Compare the value against the expected value plus or minus a small 'delta'; 3) Contrive your tests to only worry about integers.

    #1 might give you the same problem, so probably shouldn't be used either. #2 is mildly complicated. #3 is probably easiest, even if it's not necessarily the best test. That's acceptable for this assignment.

  6. Can you clarify the directory structure that should be created by h2xs?

    The command h2xs -AX -n Rcsid::Averages should create the following directories and files:
    Rcsid-Averages/
    Rcsid-Averages/lib/
    Rcsid-Averages/lib/Rcsid/
    Rcsid-Averages/lib/Rcsid/Averages.pm
    Rcsid-Averages/t/
    Rcsid-Averages/t/Rcsid-Averages.t
    Rcsid-Averages/Makefile.PL
    The second command for the BankAccount class should create a similar structure, of course. If you do not see these results, please make sure you are running on rcs-sun4.rpi.edu, and please make sure you are running the correct version of perl:
    perl -v
    if that returns some version of perl 5.6, instead of perl 5.8.2, you need to remove the perl-new package:
    setup -permanent -remove perl-new (note no space in 'perl-new')
    then log out and log back in to rcs-sun4, and check the version again.

  7. My module tests all seem to pass, but I get permission errors when I try to run `make install`

    First, make sure you supplied the correct PREFIX attribute when you created the Makefile:
    perl Makefile.PL PREFIX=~

    Second, make sure you're running the correct version of perl. See the previous FAQ answer for instructiosn on fixing that.

  8. When I use carp() in one of my BankAccount methods, I get an odd error that stringification is not overloaded (Operation `""': no method found, argument in overloaded package Rcsid::BankAccount at /usr/lib/perl5/5.8.5/overload.pm line 97)

    I've seen this as well, and I honestly don't understand it. The only thing I can figure is that for some reason, carp() is trying to stringify the object. The obvious work-around is to simply create an overload handler for stringification, and add it to the use overload statement:
    use overload
      '+=' => 'add',
      '-=' => 'subtract',
      '""' => sub { "$_[0]->{name}: $_[0]->{balance}"; };

  9. When running perl Makefile.PL PREFIX=~, I get an error regarding ABSTRACT

    No you don't. You get a warning regarding ABSTRACT. It is simply telling you you didn't include an ABSTRACT section in your module's POD. You can safely ignore it, or add an ABSTRACT section to eliminate the warning.

  10. When I'm using or testing my BankAccount module, I get errors that the Averages module cannot be found.
    The BankAccount module needs to be told where the Averages module is located. First make sure you have completed the make, make test, make install procedure for Averages. Then add the same 'use lib' statement from the sample main file to your BankAccount.pm file, before the line that uses the Averages module.

Perl Quotes
Perl Quotes