Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Perl software on multiple files

by testerG (Initiate)
on Sep 25, 2011 at 09:28 UTC ( #927727=perlquestion: print w/replies, xml ) Need Help??
testerG has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys, I'm new here and wish to say hello to everybody. I have 2 questions, and would be happy if someone could help me with that.


I'm a perl coder since some years, the point is that now i'm going to write a pretty big software. I always have been used to write the script on a single file. Now, that the software has different sections, i wanted to write it on multiple files. For example, in PHP you can write the software in a lot of pages, including them etc etc etc. How can you do the same thing in Perl ? Let's think at this : i want the file to be assigned for certain things and for other things ... how can i make them to work together ?


In a part of the software im writing, at a certain point there is a function that start to do things, is an operation that could take 10 minutes also .. if a user wants to stop this operation, for my knowledges, he should just stop the program with ctrl+c .. i dont' want that to happend because is stupid. Is there a way to let the user stop an operation ? Since the software does not have a GUI where the user can do more things at the same time .. How could I do that ? Is possible to make the script doing that function and at the same time wait for the user input, and maybe if the user press some keys on the keyboard the process just stop loading and comes back the the main part of the program ? Thank you guys, I would really appreciate if someone could help me with these problems i have.


Replies are listed 'Best First'.
Re: Perl software on multiple files
by ww (Archbishop) on Sep 25, 2011 at 10:28 UTC
    Giovanni -

    Your questions give little support to your statement that you're "a perl coder since some years" because at least one generic answer to question 1 is "write modules." The answer to question 2 may depend on the OS, tasks and user skills. Searching on terms such as "background process" "Input loops" and the like may provide illumination; lack of a GUI is no bar to anything you've described, but the description is less than complete, precise or even coherent.

    Otherwise, you'll need to refine your question -- preferably with code and data as it stands if and when you become stuck.

    How do I post a question effectively? will enhance your skills at asking a question here; you'll also find that reading other introductory information about the Monastery (in On asking for help and I know what I mean. Why don't you?) will prepare you to make it easier for us to help.

    Lastly, you'll perhaps want to freshen and deepen your knowledge of Perl by reading such standard texts as Learning Perl.

Re: Perl software on multiple files
by Perlbotics (Chancellor) on Sep 25, 2011 at 10:09 UTC

    Question A:

      I guess, you want to call external scripts from your script? That can be done using system (without capturing output) or using backticks (with capturing output). For more control about streams, see IPC::Open3 or IPC::Run (there are much more related modules on CPAN - just find one that suits you). This approach becomes awkward if you need to exchange much information (input/output).

      If you wand to #include Perl code have a look at do or maybe eval (I think there was a similar question some days ago...).

      If you strive for re-use of software components (usually recommended), have a look at perlmodlib and perlmod. Update: This is the way to go for big projects (plus version control, plus coding standards, plus testing, plus documentation, etc.).

    Question B:

      Ctrl-C can be catched using signal-handlers (e.g $SIG{INT} = sub { print "Ctrl-C pressed!\n"; ...} , see: perlipc).

      There is also Term::Readkey to handle single key events.

    It appears to me, that you want to write a shell-like environment? I've done something similar using Term::ReadLine::Gnu (there are others) which was very easy. It even gives you word-completion and a command history. Works nice if your tasks complete within acceptable time - you can still press/catch Ctrl-C or use sub-processes or threads, though.
    But maybe the REPL (Read-Eval-Print-Loop) modules are something for you?

      Thanks for the reply. QUESTION A : In PHP I usually create a file 'functions.php' where inside there are all the functions. From 'main.php' for example i call a function that is declared inside 'functions.php'. So, for you, i could create a main file called '' where there is the basic structure .. and put some functions in this case subroutines in another file called ''. Then, should i include the '' file into as a module, with require ? Then, from :
      having in
      sub function() { # do something }
      will that work ? If yes, do you know some paper or somewhere where i could i find some example of a 'multi file' software in perl ? QUESTION B: gotcha, thanks. Thank you. Gio

        Please don't do that. This approach usually becomes a great global mess before your system has a chance to grow big. Read Including files again, esp. the first section that hints not to follow the PHP path. The free Modern Perl book has specific chapters about modules (i.e.: Managing Real Programs, p. 201ff.) ( already suggested by GrandFather and AM).

        Then refactor your code into modules (*.pm not *.pl BTW) that can be re-used and properly encapsulate/abstracts functionality. Check first if the functionality required is already available as a core module or a download from CPAN. Decide if it is better to have a functional- (what you do now) or an OO-interface and what really needs to be exported into your ($main::) namespace. Since you didn't gave us much details, it is hard to give you more specific advice (see ww's comment below).

        BTW: The &function() syntax is usually/probably not what you want (circumvents prototypes; makes current @_ visible to called sub; see perlsub for details).

Re: Perl software on multiple files
by GrandFather (Sage) on Sep 25, 2011 at 11:13 UTC

    You would be well advised to spend some time looking around the Tutorials section here. The answer to Question A can be found in Including files.

    Question B doesn't have such a straight forward answer and depends somewhat on the OS you are using. However you really need to think about what you want to achieve by interrupting processing in a "nice" fashion. Perl allows you to trap Ctrl+C and handle it in a tidy fashion by assigning a sigint handler using $SIG{INT} = \&sigint_handler where sigint_handler is your handler routine.

    True laziness is hard work
Re: Perl software on multiple files
by Anonymous Monk on Sep 25, 2011 at 11:14 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927727]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-04-21 12:38 GMT
Find Nodes?
    Voting Booth?