Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Perl software on multiple files

by Perlbotics (Abbot)
on Sep 25, 2011 at 10:09 UTC ( #927731=note: print w/ replies, xml ) Need Help??


in reply to Perl software on multiple files

Question A:

    I guess, you want to call external X.pl scripts from your main.pl 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?


Comment on Re: Perl software on multiple files
Select or Download Code
Re^2: Perl software on multiple files
by testerG (Initiate) on Sep 25, 2011 at 11:02 UTC
    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 'main.pl' where there is the basic structure .. and put some functions in this case subroutines in another file called 'oths.pl'. Then, should i include the 'oth.pl' file into main.pl as a module, with require ? Then, from main.pl :
    &function();
    having in oth.pl
    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.) (...as 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.pl ($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).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-07-12 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls