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

Hello,
I am developing a program for me and a friend, and my friend doesn't know anything about programming.
Every time i give him a new version of a program, here is what i do. Is there a better way ?

1. i comment the "use" lines that he won't need, like Perl::Critic, Perl::Tidy, Carp::Always.
2. i turn a global variable called $I_AM_DEV to $FALSE (which is just equal to zero), which slightly impacts several subroutines in my code.
3. in my Log4perl configuration file, i indicate that the Screen appender only wants to see messages of the WARN or FATAL level.

I am sure that there is a better way (especially concerning the $I_AM_DEV global variable).
How do you deal with this?

  • Comment on How to turn a "developer code" into a "user code"

Replies are listed 'Best First'.
Re: How to turn a "developer code" into a "user code"
by Corion (Patriarch) on Mar 11, 2012 at 15:24 UTC

    Personally, I prefer not to have "developer" configuration (also passwords) in the program. I either put those into a configuration file or take them from %ENV.

    For distributing code to non-programmer friends, I usually package up the complete program together with the Perl executable etc. into one ZIP archive and mail it to them or bring it to them. If I am really, really sure they don't need additional modules, I just mail them the new main .pl file to replace the old .pl file.

      Personally, I prefer not to have "developer" configuration (also passwords) in the program. I either put those into a configuration file or take them from %ENV.
      Taking them from %ENV sounds interesting. If i understood well, it would mean that as a default, $I_AM_DEV would be set to 0, and that on my computer, it would be set to 1 through an environmental variable. Thus, i wouldn't need to change it each time i'm passing him the program (i also just send him the .pl file when there are no new prerequisites).
      Did i understand what you are doing properly ?

        Yes, or alternatively, there are some pre-set environment variables, like (on Windows), $ENV{USERNAME} (which is 'corion' on my machine), and the name of the machine. Using these, you can switch on more verbose output automatically.

Re: How to turn a "developer code" into a "user code"
by tobyink (Canon) on Mar 11, 2012 at 15:15 UTC

    Rather than using a variable, use a constant.

    use constant I_AM_DEV => 1;

    A proper constant lets the Perl compiler optimize away the devepment code when it is switched off - i.e. it runs faster.

      i do

      use Readonly 1.03; [...] Readonly my $I_AM_DEV => $FALSE;
      Is it equivalent to what you wrote ?

        No, using constants allows the Perl debugger to specifically ignore blocks of your code - it can figure out which parts it can ignore at compile-time, and avoid even compiling them. Take for example this script:

        use constant DEBUG => 0; if (DEBUG) { print "Calculator with debugging..."; } else { print "Calculator..."; } print "1 + 1 = ", (1 + 1), "\n";

        If you save that as, say debug.pl and then run the following command:

        perl -MO=Deparse tmp/constants.pl

        This uses the B::Deparse module to show you the code that Perl's runtime will actually execute. The result is:

        use constant ('DEBUG', 0); do { print 'Calculator...' }; print '1 + 1 = ', 2, "\n";

        You will notice that the print "Calculator with debugging..." part is entirely absent, because the Perl compiler knows that DEBUG is false, so that chunk of code will never be run! Perl doesn't bother compiling it. Even the if/else structure has gone.

        (You'll notice also that the Perl compiler has added 1+1 so that the Perl runtime doesn't have to. This is called constant folding. In this particular example it doesn't offer any real benefit, but if that sum was inside a loop, then calculating it once at compile time is much better than calculating it every loop at runtime!)

        So anyway, using constants for tasks like this allows the Perl compiler to optimize your code. With variables it can't.

        So what about variables that have been set using Readonly?

        use Readonly; Readonly my $DEBUG => 0; if ($DEBUG) { print "Calculator with debugging..."; } else { print "Calculator..."; } print "1 + 1 = ", (1 + 1), "\n";

        Running it through B::Deparse we get...

        use Readonly; Readonly my $DEBUG, 0; if ($DEBUG) { print 'Calculator with debugging...'; } else { print 'Calculator...'; } print '1 + 1 = ', 2, "\n";

        So the compiler has not been able to do much optimisation.

        Also it's worth noting that Readonly is implemented using Perl's tied variable magic, which imposes a considerable performance penalty - Readonly variables are actually slower than normal variables.

Re: How to turn a "developer code" into a "user code"
by duelafn (Parson) on Mar 11, 2012 at 19:19 UTC

    I tend to prefer command-line switches (I use --DEBUG). You can then conditionally load modules as appropriate using require or eval (you could of course, build your conditional using your $I_AM_DEV, but I personally find command line switches far more elegant and less annoying than modifying code to enable/disable such things). Example:

    use Getopt::Long; my %OPT; GetOptions \%OPT, qw/ help|h DEBUG / or ($OPT{help} = 1); if ($OPT{DEBUG}) { # whatever you like: require Data::Dump; eval "use Carp::Always"; } ... say "Got some data:\n", Data::Dump::pp(\%my_data) if $OPT{DEBUG};

    Good Day,
        Dean

Re: How to turn a "developer code" into a "user code"
by nemesdani (Friar) on Mar 11, 2012 at 15:15 UTC
    1. If the program is for your friend, why can't you tell him to intall the repsective packages?
    3. If he doesn't know anything about programming, why bother with the log4perl? If there's a problem, he won't read the log in the first place, just will send the log to you.
    My opinion: if he's a friend, let him deal with the developer code. Maybe your friendship will suffer, but hell, you can't have it all...:)

      I install the different packages for my friend.
      Log4perl is just for me. Putting it at FATAL or WARN is just a way for me to "almost turn it off".

      My friend doesn't want to see a source, or have to use the command line. The best i can ask him is "put your information in the configuration file, and double click the icon to start the program."
      Make him read the source code, i tried, i tried... ;o)
      Maybe i'm being too kind to him :oP
      But it's not too hard for me to make his life easy, so... =o)