Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Proper way to create packages and re-usable code?

by bt101 (Acolyte)
on Feb 12, 2016 at 00:01 UTC ( #1155022=note: print w/replies, xml ) Need Help??


in reply to Re: Proper way to create packages and re-usable code?
in thread Proper way to create packages and re-usable code?

WOW, thanks everyone for the great responses. I understand what you are saying... Packages should have a narrow purpose and be self contained. I think I see the error of my ways as the code that I want to re-use does not really fit that mold. Basically I have a bunch of routines that do low level serial communication. All of the routines form a whole program that does the communication, error handling, logging etc. At the top level, there is one routine to call to send/receive a message. I then want to use this code for (let's say) 10 different programs where each program only differs by the main loop which loops and calls the re-usable routines for communication. All I "really" want is to house the re-usable code in a separate "file" so there is only one set of re-usable code to maintain. From what I read on the internets, I gathered that the way to split perl into different files was with a package. However my code doesn't really fit the mold of having singular purpose. I'll do some research on the links provided. I understand that if the whole program was destined to be a 1 million line ERP system, the it would be worth re-organizing and rewriting everything to fit into modules. However I'm aiming at just splitting things into "two" files, and I'm 1 yard from the finish line. So cut/paste into 10 scripts may be the way to go.
  • Comment on Re^2: Proper way to create packages and re-usable code?

Replies are listed 'Best First'.
Re^3: Proper way to create packages and re-usable code?
by Hadrianus (Novice) on Feb 12, 2016 at 06:40 UTC

    The following is not the proper way to modularize your code, but because you are just 'one yard before the finish line' you could use a script or Makefile to concatenate multiple files to a single program. That is easier and less error prone then the temporary "manually cut and paste" solution, that you are considering.

    Assuming you have the following files:

    -rw-r--r-- 1 hadri hadri 51 Feb 12 04:24 main_barack -rw-r--r-- 1 hadri hadri 63 Feb 12 04:15 my-serial-library.src -rw-r--r-- 1 hadri hadri 68 Feb 12 04:27 postlude_general.src

    Your general libary routines are in my-serial-library.src and postlude_general.src. The 'main' program is the only customized part and is named after your client or location: main_barack.

    With a simple Makefile you can generate your program serial_com_barak.pl:

    $ make cat my-serial-library.src main_barack postlude_general.src > serial_co +mm_barack.pl

    For another customer you create a new customized main and run 'make' again:

    $ echo "This is for Bernie" > main_bernie $ make CLIENT=bernie cat my-serial-library.src main_bernie postlude_general.src > serial_co +mm_bernie.pl

    The Makefile:

    # -- sample Makefile CLIENT = barack NAME = serial_comm PROG_NAME = ${NAME}_${CLIENT}.pl PRELUDE = my-serial-library.src MAIN = main_${CLIENT} POSTLUDE = postlude_general.src TAR_NAME = serial-comm_${CLIENT}.tgz TAR_FILES = Makefile TAR_FILES += ${PRELUDE} ${MAIN} ${POSTLUDE} ${PROG_NAME}: ${PRELUDE} ${MAIN} ${POSTLUDE} cat ${PRELUDE} ${MAIN} ${POSTLUDE} > ${PROG_NAME} clean: rm -rf ${PROG_NAME} archive: tar cvzf ${TAR_NAME} ${TAR_FILES} # --- end of Makefile

    Note: Make sure that you have a tab character (\x09, CNTRL-I or ^I) before the cat and other commands in the Makefile.Verify with 'cat -t Makefile':

    ${PROG_NAME}: ${PRELUDE} ${MAIN} ${POSTLUDE} ^Icat ${PRELUDE} ${MAIN} ${POSTLUDE} > ${PROG_NAME}
      Ah yes thanks. That will get me going for now. I'll spend some time studying the info and links provided in the replies. Thanks everyone for the help.
Re^3: Proper way to create packages and re-usable code?
by Anonymous Monk on Feb 12, 2016 at 01:07 UTC

    I understand that if the whole program was destined to be a 1 million line ERP system, the it would be worth re-organizing and rewriting everything to fit into modules. However I'm aiming at just splitting things into "two" files, and I'm 1 yard from the finish line. So cut/paste into 10 scripts may be the way to go.

    No, copy/paste isn't the answer, thats like first week of intro to programming :)

    modules are the answer, modules that are accompanied with tests, your code should live in modules :)

    Maybe

    Something like that, draw a few outlines like that on a piece of paper, putting different stuff in different places, see what makes more sense

    You can see all kinds of examples on cpan, see App::

    https://metacpan.org/release/Module-Starter

    tracker/App::TimeTracker

    booklist/App::Booklist

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1155022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2022-05-26 11:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (93 votes). Check out past polls.

    Notices?