Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: source a program

by etcshadow (Priest)
on Nov 19, 2004 at 03:53 UTC ( #408939=note: print w/replies, xml ) Need Help??


in reply to source a program

The nearest perl equivalent to "sourcing" in shell is do. In shell, "sourcing" is different from executing... that is: when you execute a program (whether another shell script or a binary executable or whatever) it executes in a separate process, with it's own environment and variables and so on. When you source another shell script (you can only source other shell scripts, you cannot source a binary executable) it is just treated as though the code were dropped right into your shell (or shell script) right there. Thus, any changes to the shell's environment and variables and so on which are made by the source-ee take effect in the source-or (big difference from when another command is executed in a subprocess/subshell).

Anyways, likewise in perl. You can execute any arbitrary command (a shell script, perl script, binary executable, whatever... see perldoc -f system), but it will occur in its own subprocess. You can also "source" a file full of other perl code directly into your perl script, affecting your scripts environment, variables, and so on. You do that like:

do "script_name.pl";
just like you would say:
source script_name.sh
or (the same thing)
. script_name.sh
(the . is just a shortcut for writing the command "source" in shell). There are some minor differences between perl's "do" and shell's "source". The biggest difference is probably that perl's "do" will return the last value computed in the called script. For more info, check out perldoc -f do.
------------ :Wq Not an editor command: Wq

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2019-10-14 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?