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

Hi all,
I would like to know your opinions about using packages into script. I've never seen a structure like this before (maybe it's my inexcusable omission):
#!/usr/bin/perl -w use strict; package One; sub one { #do something } package Two; sub two { #do something } package Three; sub three { #do something }
Please, tell me, does it have a sense and, if yes, when?
TIA.
      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

Replies are listed 'Best First'.
Re: Using a package into script
by broquaint (Abbot) on May 07, 2003 at 13:31 UTC
    Please, tell me, does it have a sense and, if yes, when?
    Sure, for each package declaration the rest of the lexical scope, or until another package declaration, will be in the given package's namespace. So sub one will live in One::one e.g
    ## default namespace is 'main' { package One; sub one { print "I am in ", __PACKAGE__, $/ } } ## namespace back to main as lexical scope has ended One::one(); __output__ I am in One
    This sort of thing is useful when you want to hide packages/classes (e.g under the hood tie implementations) and saves on fully declaring everything (without a package declartion sub one would have to be declared as sub One::one). See. package for more info on it's behaviour.
    HTH

    _________
    broquaint

      Thanks for response. I thought about it but, in my mind, it will be more clear to release foregoing code in the various modules, isn't it?
            
      --------------------------------
      SV* sv_bless(SV* sv, HV* stash);
      
        Yes, putting your packages into distinct modules is generally a good way to go as you get compartmentalization and all the sort of thing, and it's pretty much the done thing in the perl community (just look at the code on the CPAN). Usually when you have multiple package declarations, it's for reasons cfreak listed and others.
        HTH

        _________
        broquaint

Re: Using a package into script
by cfreak (Chaplain) on May 07, 2003 at 13:36 UTC

    If you've used modules then you've used packages, they were just inside the module file rather than inside your main script. Putting them in the main script however as you have above is legal and useful if you want to break something off into a class (basically a package is a class) but its not all that big and you don't want to keep track of two files. Although your code above wouldn't do anything until you have a package main; and the other packages would have to be called.

    The most common use of this method is when you have a tied variable or an AUTOLOAD. Also some modules make use of creating classes from the modules main classes. I've been using wxPerl lately and it makes a lot of sense to use this method there as the main window (wxPerl is a GUI library) has to have its own constructor method.

    Hope that helps
    Chris

    Lobster Aliens Are attacking the world!