# from Johan Vromans
# on Friday 18 May 2007 02:35 am:

>but it would be just
>'helloworld', not 'bin::helloworld'. And it's package, if any, would
>most likely be 'main'.

I've found that it is much easier to test and refactor programs which 
are not written against the left margin in package main.  Here's an 
example of the skeleton which I currently use.

The bit in package main decides if we're being require()d or not (though 
PAR and PerlWrapper currently both break this without a bit of code 
which is omitted for simplicity.)

The package bin::helloworld has a version number, documentation, etc.  
Everything it needs to be a module.  Again, this is all about 
refactorability (though there's some readability at play too.)

  #!/usr/bin/perl
  
  use warnings;
  use strict;
  
  package bin::helloworld;
  our $VERSION = v0.0.1;

  =head1 NAME
  
  bin::helloworld - says hi
  
  =cut
  
  =head1 Functions

  =cut

  sub main {
    my (@args) = @_;
  
    my $who = who();
    print "hello ", $who, "\n";
  }
  
  =head2 set_who

  Sets the $WHO for hello.

    set_who('universe');

  =cut

  my $WHO = 'world';
  sub set_who {
    ($WHO) = @_;
  }
  sub who {
    $WHO;
  }
  
  package main;
  
  if($0 eq __FILE__) {
    bin::helloworld::main(@ARGV);
  }
  
  # vi:ts=2:sw=2:et:sta
  my $package = 'bin::helloworld';


The distribution would be like:
  bin-helloworld/README
  bin-helloworld/MANIFEST
  bin-helloworld/META.yml
  bin-helloworld/Build.PL
  bin-helloworld/bin/helloworld
  bin-helloworld/t/00-load_bin_helloworld.t

I hope that sheds some light on my motivation.

Also, consider that this could go in the @INC tree under bin/ and be 
symlinked from there to /usr/bin/ or whatever.  And yes, we're 
currently lacking installer support for that.  (For qdos, we're missing 
symlinks, but the only executables have eXe or BaT names, so follow 
Vanilla's C:/Perl/bin/foo.bat scheme, where the bat file now does perl 
-e 'require("bin/helloworld"); bin::helloworld::main(@ARGV)' or 
thereabouts.)

It also enables the test-in-situ (retest?) scheme, makes 
bin::helloworld->VERSION work, and maybe a few other things.

Could most of that happen with qr/[Aa]pp(?:lication)?/ or whatever?  
Sure.  Would it have the same parallel to bin/?  No.

--Eric
-- 
The reasonable man adapts himself to the world; the unreasonable man
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.
--George Bernard Shaw
---------------------------------------------------
    http://scratchcomputing.com
---------------------------------------------------

Reply via email to