Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

the Magic Box system? (use require)

by Buckaroo Buddha (Scribe)
on Jun 29, 2000 at 00:28 UTC ( #20247=perlquestion: print w/replies, xml ) Need Help??

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

 
i've got a short program that is fully functional

it opens a (comma delimited) file from the command line
and puts it into a multi-dimentional hash of arrays

  $DATA                       = HASH
  $DATA{$key1}                = HASH  
  $DATA{$key1}{$key2}         = ARRAY
  $DATA{$key1}{$key2}$value = 'data element'
     
so what we have is one file translating into a relatively 
complex data structure.  

I have this file working and would like to 'encapsulate it'

i'd like to start the next step of the program in a blank 
file ... 

   i'm writing for advice on how to do this (although i 
have assumptions)

   the question then is: 
what conditions must be in place in order to turn a 
complete program into a 'magic box' or function that
can be included in another program?

   note: if i find a good answer first i'll fill it in 
i always ask here first 

   i'm thinking it will be along the lines of:

   require 'ReadInFromFile.pl';

one of the main things i'm worried about is compile-time.  
Because i am working on win32, writing utilities for 
non-technical workers this program MUST be executable

i've got a compiler (freeware, named perl2exe) and i 
want to make sure that when this becomes an EXE it will
incorporate the 'ReadInFromFile.pl' file.  

Replies are listed 'Best First'.
Re: the Magic Box system? (use require)
by btrott (Parson) on Jun 29, 2000 at 00:41 UTC
    You wrote:
    > what conditions must be in place in order to turn a > complete program into a 'magic box' or function that > can be included in another program?
    You need to be more careful of scoping issues, particularly if you're using lexical variables. For exmaple, if you declared your hash as a file-scoped lexical (my) variable in your code, it wouldn't be available to the script that require-d it.

    Another issue is that the script you're require-ing has to return a true value; that's why such scripts often end with

    1;
    You might also want to put the code that reads the config file into a subroutine that you can call. And you might want to read perlmod.
Re: the Magic Box system? (use require)
by davorg (Chancellor) on Jun 29, 2000 at 13:08 UTC

    I'd recommend turning the function into a module. The easiest way to do this is to create a skeleton module using h2xs and then copy your code into the skeleton.

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000
    <http://www.yapc.org/Europe/>
RE: the Magic Box system? (use require)
by pemungkah (Priest) on Jun 29, 2000 at 19:53 UTC
    I'll give you a couple answers, in order of usefulness (I hope).

    If you don't plan to reuse this code, just put it in, in-line. No packaging worries, nice and simple. Once you have the data structure, the rest of the code just diddles with it.

    You could make it nicer by turning it into a subroutine, like this:

    sub read_it_and_construct {
       # Arguments are in the @_ array here
       my ($filename) = @_;
    
       # Create variables for this function only using "my"
       my $DATA;
    
       # Open the file and read it
       open (INPUT, $filename) or die "Can't open $filename: $!\n";
    
       # ... your code here, which builds the structure ...
       # (don't forget to use 'my' on any other variables you
       # use in here! Otherwise, they're global and can clobber
       # like-named variables in the main program!)
    
       # Pass the structure back to the caller
       return $data;
    }
    
    The main program would simply say:
    my $structure = read_it_and_construct($from_this_filename);
    
    The subroutine gets the file to read as its argument, and returns the structure as its value.

    If you want to make it really black-box, you can go object-oriented; I can talk to you about that if you like. Essentially, you write some more subroutines that know how to access the data structure, and return (to the caller) a special kind of reference that knows about these functions, so you can do stuff like

    my $filetree = ReadAndConstruct->new($file);
    my $value = $filetree->lookup($key1,$key2,$key3);
    
    and so on. Let me know if this sounds useful for you. I'd recommend it only if you plan to use the tree builder in a number of programs.
RE: the Magic Box system? (use require)
by Buckaroo Buddha (Scribe) on Jun 29, 2000 at 21:22 UTC
    UPDATE:  
    
        based on the advice i read, i decided to simply 
        enclose it in a sub-function (thanks to all for 
        the advice)
    
        perhaps i could save that sub function in a file
        and add the line 'include myFunctions.pl' at the 
        beginning of the new document?
    
        i can deal with it the way it is, 
        (i've done it before, i can do it again ;)
        but it got me thinking... 
    
       what i really want is a good text-editor that has
       "block support" (along with regular colour and 
       tab support)
    
       the idea is that code could be displayed as a tree
       where blocks of it could be expanded and collapsed
       with the click of a toggle box
    
       ... navigation would be greatly improved (for 
       me at least) if after writing a complete and working 
       sub-function i could collapse it into one line 
       of code 
    
       i'll put it on my 'to do' list ... but that's under
       'learn how to program visual C++' so if someone else 
       knows of a text editor that does that already, 
       let me know  ;)
      
       (and pemungkah: you can bet that i'll be asking you 
          about coding in OO soon enough <g> )
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://20247]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2019-08-21 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?