split a big program into multiple files

Discussion in 'Perl Misc' started by xhoster@gmail.com, Jun 8, 2006.

  1. Guest

    wrote:
    > Hi,
    >
    > my program is getting big and I'd like to split it into multiple files,
    > something like C guys do.


    Why? Is it so big that it is crashing your favorite text editor?

    >
    > main.pl
    >
    > include file1.pl
    > include file2.pl
    >
    > etc
    >
    > I tried both do and require but they doesn't work as I expect:
    >
    > I can't use the global variables I declare into main for the subrutines
    > declared into other files.


    I assume that by "global" you mean file-level lexicals.

    Generally, you are supposed to pass arguments into subroutines, not pick
    them up randomly from the symbol table/pad.

    > How can I do?


    If you don't want to experience the benefits of using file-level lexicals,
    then don't use them. Use package variables instead (or much better,
    redesign your program into multiple files along lines such that you don't
    need variables to cross the boundaries.)

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 8, 2006
    #1
    1. Advertising

  2. Guest

    Hi,

    my program is getting big and I'd like to split it into multiple files,
    something like C guys do.

    main.pl

    include file1.pl
    include file2.pl

    etc

    I tried both do and require but they doesn't work as I expect:

    I can't use the global variables I declare into main for the subrutines
    declared into other files. How can I do?

    Thanks,

    Filippo
     
    , Jun 8, 2006
    #2
    1. Advertising

  3. John Bokma Guest

    wrote:

    > Hi,
    >
    > my program is getting big and I'd like to split it into multiple files,
    > something like C guys do.
    >
    > main.pl
    >
    > include file1.pl
    > include file2.pl
    >
    > etc
    >
    > I tried both do and require but they doesn't work as I expect:


    What did you expect?

    > I can't use the global variables I declare into main for the subrutines
    > declared into other files. How can I do?


    Best option would be to group functionality into Perl modules. Instead of
    global variables you might want to create objects and initialize them with
    those "global" settings.

    Might sound as a lot of extra work, but in the end you have a piece of
    work that is much easier to maintain.

    Also, while grouping functionality, you might keep a close eye on CPAN.
    Maybe someone has already written a module that can replace a part of your
    code and it might be better to use the CPAN one, especially if it's a
    module that is used by many.


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jun 8, 2006
    #3
  4. filippo Guest

    ok John and Xho, you convinced me :)

    In fact I have just a couple of global variables so I can easily
    redesign my subroutines to manage these as arguments.

    Which is the best method to have a C-like splitting method?

    Thanks for your help.

    Filippo
     
    filippo, Jun 8, 2006
    #4
  5. filippo wrote:
    > ok John and Xho, you convinced me :)
    >
    > In fact I have just a couple of global variables so I can easily
    > redesign my subroutines to manage these as arguments.
    >
    > Which is the best method to have a C-like splitting method?


    What on earth is a "C-like splitting method"?
    In C (and in Perl, and in Modula2, and in any other language that allows
    to spread source code over more than a single source file), you split by
    grouping things that belong together.

    When using #include, you put only definitions (types, structures,
    function prototypes, constants) into the files that are included.
    Otherwise you put code that belongs together into a C source file and
    compile each source file independently into object files which are then
    linked together into an executable. The .h-files included provide some
    glue between these source files.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 9, 2006
    #5
  6. filippo Guest

    Josef Moellers ha scritto:

    ok, you're right.

    My application is a Tk GUI based perl program to manage a touristic
    campsite. I have some windows:

    Main Menu
    createGroup
    manageGroup
    Bill
    others (tools)

    and I want to split such a big code (~10.000rows) into separate files.
    I have no global variables (no more), just subroutines: more or less a
    main subroutine plus a couple of service for each windows. Which is the
    'best' method to split my program?

    Thanks,

    Filippo
     
    filippo, Jun 9, 2006
    #6
  7. filippo wrote:
    > Josef Moellers ha scritto:
    >
    > ok, you're right.
    >
    > My application is a Tk GUI based perl program to manage a touristic
    > campsite. I have some windows:
    >
    > Main Menu
    > createGroup
    > manageGroup
    > Bill
    > others (tools)
    >
    > and I want to split such a big code (~10.000rows) into separate files.
    > I have no global variables (no more), just subroutines: more or less a
    > main subroutine plus a couple of service for each windows. Which is the
    > 'best' method to split my program?


    It's indeed probably the most complicated task: to decide which
    functions should be grouped together.
    I'm not sure if there is a clean-cut approach to do this.

    Maybe you could group according to "window"?

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 9, 2006
    #7
  8. filippo Guest

    I know more or less how to split. I'd like to know the best way to
    import. I know the 'do' way, are there problems to do this using do?

    Thanks,

    Filippo
     
    filippo, Jun 9, 2006
    #8
  9. filippo wrote:
    > I know more or less how to split. I'd like to know the best way to
    > import. I know the 'do' way, are there problems to do this using do?


    Oh, sorry for the misunderstanding.

    As for the technicality: I really don't know what's the best way to do.
    I usually use "use" which is based on "require" which is based on "do".

    Hopefully others know better.

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jun 9, 2006
    #9
  10. Ben Morrow Guest

    Quoth "filippo" <>:
    > I know more or less how to split. I'd like to know the best way to
    > import. I know the 'do' way, are there problems to do this using do?


    The modern way is to use 'use'. Read perlmod and perlnewmod.

    Ben

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces mollit animos, tristesque mentes erigit.|
    Musica vel ipsas arbores et horridas movet feras. |
     
    Ben Morrow, Jun 9, 2006
    #10
  11. writes:

    > Why? Is it so big that it is crashing your favorite text editor?


    I haven't seen a file so big it crashed my favorite editor.

    I *have* seen files so big they nearly crashed my favorite brain. :)

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 12, 2006
    #11
  12. Guest

    Sherm Pendley <> wrote:
    > writes:
    >
    > > Why? Is it so big that it is crashing your favorite text editor?

    >
    > I haven't seen a file so big it crashed my favorite editor.


    I haven't seen the code portion of a Perl file that was that big. Other
    file types, or __DATA__ sections, sure.

    > I *have* seen files so big they nearly crashed my favorite brain. :)


    If you had just broken it into smaller chunks at random
    (say, by using the gnu split command), would that have helped your brain?

    If not, then you don't have a technical problem about how to get perl to
    stick random chunks back together, but rather an intellectual one about how
    to take things apart.

    I think we are getting at the same thing, just with different levels of
    sarcasm :)


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jun 12, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shaguf
    Replies:
    0
    Views:
    376
    Shaguf
    Dec 24, 2008
  2. Shaguf
    Replies:
    0
    Views:
    463
    Shaguf
    Dec 26, 2008
  3. Shaguf
    Replies:
    0
    Views:
    250
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    227
    Shaguf
    Dec 24, 2008
  5. Freddie
    Replies:
    9
    Views:
    113
    ccc31807
    Sep 10, 2009
Loading...

Share This Page