Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to call perl script within perl script ?

by gasho (Beadle)
on Nov 19, 2007 at 16:35 UTC ( [id://651691]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, Is there more elegant perl way to call perl script within perl script than described bellow, Thanks
#!perl $LocationOfTheScriptsDirectory='D:\TEST'; print "Hello"; #Test1.pl is #print " World\n"; system("$LocationOfTheScriptsDirectory\\Test1.pl");
(: Life is short enjoy it :)

Replies are listed 'Best First'.
Re: How to call perl script within perl script ?
by jettero (Monsignor) on Nov 19, 2007 at 16:42 UTC

    I believe you can require them in. Personally I tend to read the code into a variable and eval it.

    sub run_it { my $filename = shift; open my $in, $filename or die $!; local $/ = undef; my $entire_script = <$in>; close $in; eval $entire_script; die $@ if $@; }

    -Paul

Re: How to call perl script within perl script ?
by Anonymous Monk on Nov 19, 2007 at 16:45 UTC
    push @INC, 'D:\TEST'; require 'Test1.pl';
      use
      do(perl script name/);
Re: How to call perl script within perl script ?
by sgt (Deacon) on Nov 19, 2007 at 22:22 UTC

    Besides all the excellent advice already given there is one important aspect: processes!

    For example on unix-like the question would be

  • Do you want to start another process and wait till it finishes?: system or even system with time-out (if you shell supports it). Still you'll need to record the status carefully to know if the execution went OK or no.
  • Do you want to run some more code in the same process?: do, require or eval "string".
  • Do you need to fire one or more processes and then communicate with them to hand out tasks or queries (worker or co-process view)?: then you have the whole spectrum of IPC.

    All of those have their use. The firing of an external process is often neglected these days but lets the OS do the safety net (restarting yourself can be a useful trick even). Choose wisely ;)

    cheers --stephan
Re: How to call perl script within perl script ?
by perlfan (Vicar) on Nov 19, 2007 at 20:16 UTC
    For maximum elegence, you should wrap it up in a *.pm.
Re: How to call perl script within perl script ?
by abachus (Monk) on Nov 19, 2007 at 19:23 UTC
    You could take a look at IPC::Open2 or IPC::Open3...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-03-28 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found