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

I want to create a multiform Perl Tk application where each form is defined by two physical files. One file controls the appearnce of the form, and the other controls the functionality.

I was thinking of having each file with the same name, but different extensions, then calling them with "require". In each file would be the "package" key word identifying the name of the form as an object.

Recently I was told not to use "require" and only use "use", but from what I can tell "use" requires a ".pm" extension and the name of the file is the name of the package. So how do I use "use" on two files that I want the same namespace?



UPDATE: Thanx for everyones help. As a matter of fact, many of the forms need only be loaded when they are required, so I may use a mix of the approaches suggested.

Replies are listed 'Best First'.
Re: 1 Object 2 Files
by eric256 (Parson) on Jan 11, 2005 at 21:56 UTC

    It sounds to me like this is a fine case for require. Since it sounds like you might want to dynamicaly load portions at run time (just a guess) then require fits the bill. Sometimes use fits, sometimes require. You can use package statments in either so the real difference comes down to if you want it included at run time, or compile time, and if you have anything that needs imported out of the package. Since it doesn't realy sound like you have anything module'ish about those files it sounds like a job for require. Of course I might be way off base, but then maybe the person who said only use "use" was confused.


    ___________
    Eric Hodges
      Agreed, this sounds like a good place for require.

      This would also let the files share common base names with different extensions, as the OP had desired.

      # In lib/MyCode/Form1.pm package MyCode::Form1; require "lib/MyCode/Form1.tkfrm"; sub handle_button { ... } 1;
      # In lib/MyCode/Form1.tkfrm # package declaration is optional sub layout_form { ... } 1;
Re: 1 Object 2 Files
by dragonchild (Archbishop) on Jan 11, 2005 at 20:50 UTC
    In File.pm
    package File; use File::Additional; # Your stuff here 1;

    In File/Additional.pm

    package File; # Your additional stuff here 1;

    In your main file

    use File; # Your stuff here

    Update: Removed unneeded package declaration as per Tanktalus's excellent notes.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      You don't really actually need the package File::Additional; line in File/Additional.pm. Perl enforces no requirements that the file name matches the package name - it's just a good convention.

        Actually, you do if you want use File::Additional; to work. Perl does enforce that if you say use Foo::Bar::Baz; that there actually exists a Foo/Bar/Baz.pm somewhere in your @INC and that it has, somewhere, a line saying package Foo::Bar::Baz;. Otherwise, it will bitch at you.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.