Sourcing Things in Perl ? 
Author Message
 Sourcing Things in Perl ?

I trying to source some stuff in a Perl Tk script.

Basically I have a string setup somewhere else in the program ;
$sourceme which contains the full path of the source file.

And then I'm using the system command to source it like so :

system ( "source $sourceme" );

I don't get anything written to the console and the file is definately
not sourced.

Any ideas ?



Mon, 15 Mar 2004 09:51:24 GMT  
 Sourcing Things in Perl ?
On Thu, 27 Sep 2001 09:51:24 +0100,

Quote:
> I trying to source some stuff in a Perl Tk script.

What does that mean? Are you trying to use the source command of csh?

Read Perl FAQ section 8, question

       I {changed directory, modified my environment} in a perl
       script.  How come the change disappeared when I exited the
       script?  How do I get my changes to be visible?

Quote:
> Basically I have a string setup somewhere else in the program ;
> $sourceme which contains the full path of the source file.

You will really need to be much, much more specific about what exactly
you're trying to achieve. Is that 'source file' perl code? Then look
into require and do (see perlfunc)

Quote:
> And then I'm using the system command to source it like so :

> system ( "source $sourceme" );

Some shells have that as a builtin command, but I haven't seen it as an
external command on any system I've worked on. In either case, it won't
'source' anything into the current process.

Quote:
> I don't get anything written to the console and the file is definately
> not sourced.

It probably was sourced, or maybe not. Depends on the shell that got
fired up. However, you won't really know, since you don't check the
return value of system, which you should do.

Quote:
> Any ideas ?

Lots. But the first thing _you_ need to do is explain what you want. Not
what you think you need to do.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Think of the average person. Half of
Commercial Dynamics Pty. Ltd.   | the people out there are dumber.
NSW, Australia                  |



Mon, 15 Mar 2004 10:32:04 GMT  
 Sourcing Things in Perl ?

Quote:
> I trying to source some stuff in a Perl Tk script.

> Basically I have a string setup somewhere else in the program ;
> $sourceme which contains the full path of the source file.

> And then I'm using the system command to source it like so :

> system ( "source $sourceme" );

> I don't get anything written to the console and the file is definately
> not sourced.

> Any ideas ?

Is the file you're sourcing a shell script?  Is it for the same shell
that the perl script is running under? Is it the for the same shell that
an individual user of your script uses? Can you guarantee that this is
always the case?

Are you checking the return value of system?  Was it successful?

Of course, even if all the above is acceptable, it still won't work.
Assuming the file is sourced:
    system() creates a child process
    the child process sources the file
    the child process terminates
    all results of sourcing are lost
    period

If I understand your situation, this is one of the oldest questions
asked, and the answer is:
    No, a child process cannot alter it's parents environment except by
    very complicated, increasingly dangerous (not to mention just
    plain "bad") hacks.  This was intended as a feature.

Try rethinking your problem. I've actually recently sought a Perl module
for parsing shell scripts.  It could return a hash of variables set
within the script (since you probably don't want an arbitrary shell
script to mess with your %ENV directly) and possibly taint it.  I
searched on cpan but didn't turn up anything (but then, I could've been
blind). Anyone know of anything?  Even python has an shutil script.

Of course, if you're trying to "source" a perl script, you probably want
to 'require' or 'use' it...not system().

HTH

--
A child of five would understand this.
Send someone to fetch a child of five.
    -- Groucho Marx



Mon, 15 Mar 2004 10:55:42 GMT  
 Sourcing Things in Perl ?

Quote:
> Of course, if you're trying to "source" a perl script, you probably want
> to 'require' or 'use' it...not system().

Of course, 'use' would only work if it were a module.

Also, 'do' is a valid option.

Tim
--
I have never let my schooling interfere with my education.
    -- Mark Twain



Mon, 15 Mar 2004 10:57:06 GMT  
 Sourcing Things in Perl ?
Okay, forgive my ignorance.
I have a program called ModelSim that I execute from a Perl Tk script.
It needs certain environment variables set up before it is run.
There are two files that need sourcing.  One a one point in time and the other later.
I have limited knowledge of the source files, they have lots of 'setenv' commands in
them and various 'echos' to let you know whats going on but thats all.
Using a Solaris Unix system, I usually type :
source <filename> in a terminal when needed and away we go.
This process needs to be automated into the Perl script.
As explained I have just tried to use perls 'system' command to perform the operation
that I type at the prompt and this did not have the desired affect - whether it did
source the file or not.
I'd assume any changes need to be visible when ModelSim is run.

Does this help ?

Quote:
> I trying to source some stuff in a Perl Tk script.

What does that mean? Are you trying to use the source command of csh?

Read Perl FAQ section 8, question

       I {changed directory, modified my environment} in a perl
       script.  How come the change disappeared when I exited the
       script?  How do I get my changes to be visible?

Quote:
> Basically I have a string setup somewhere else in the program ;
> $sourceme which contains the full path of the source file.

You will really need to be much, much more specific about what exactly
you're trying to achieve. Is that 'source file' perl code? Then look
into require and do (see perlfunc)

Quote:
> And then I'm using the system command to source it like so :

> system ( "source $sourceme" );

Some shells have that as a builtin command, but I haven't seen it as an
external command on any system I've worked on. In either case, it won't
'source' anything into the current process.

Quote:
> I don't get anything written to the console and the file is definately
> not sourced.

It probably was sourced, or maybe not. Depends on the shell that got
fired up. However, you won't really know, since you don't check the
return value of system, which you should do.

Quote:
> Any ideas ?

Lots. But the first thing _you_ need to do is explain what you want. Not
what you think you need to do.


Mon, 15 Mar 2004 10:57:32 GMT  
 Sourcing Things in Perl ?
Is the file you're sourcing a shell script?

Quote:
>I'm not sure, from what I can see it looks like a shell script.
>It contains a load of 'setenv' and 'echo' commands and thats about it.

Is it for the same shell that the perl script is running under?

Quote:
>Yes.

Is it the for the same shell that an individual user of your script uses?

Quote:
>Yes.

Can you guarantee that this is always the case?

Quote:
>Yes.

Are you checking the return value of system?  Was it successful?

Quote:
>I don't know how.  Would a 'die' command surfice ?

Of course, even if all the above is acceptable, it still won't work.
Assuming the file is sourced:
    system() creates a child process
    the child process sources the file
    the child process terminates
    all results of sourcing are lost
    period

Quote:
>Aahh.

If I understand your situation, this is one of the oldest questions
asked, and the answer is:
    No, a child process cannot alter it's parents environment except by
    very complicated, increasingly dangerous (not to mention just
    plain "bad") hacks.  This was intended as a feature.

Try rethinking your problem. I've actually recently sought a Perl module
for parsing shell scripts.  It could return a hash of variables set
within the script (since you probably don't want an arbitrary shell
script to mess with your %ENV directly) and possibly taint it.  I
searched on cpan but didn't turn up anything (but then, I could've been
blind). Anyone know of anything?  Even Python has an shutil script.

Of course, if you're trying to "source" a perl script, you probably want
to 'require' or 'use' it...not system().

Okay, here's what  I'm trying to do.....

I have a program called ModelSim that I execute from a Perl Tk script.
It needs certain environment variables set up before it is run.
There are two files that need sourcing.  One a one point in time and the
other later.
Using a Solaris Unix system, I usually type :
source <filename> in a terminal when needed and away we go.
This process needs to be automated into the Perl script.

Does this help ?



Mon, 15 Mar 2004 11:05:52 GMT  
 Sourcing Things in Perl ?

[ snip ]

Quote:
> Are you checking the return value of system?  Was it successful?

> >I don't know how.  Would a 'die' command surfice ?

Well, system()'s backwards on return value, but the following should do
the trick (if you need system()):

system("shell command") and die "shell command failed: $!\n";

Quote:
> Okay, here's what  I'm trying to do.....

> I have a program called ModelSim that I execute from a Perl Tk script.
> It needs certain environment variables set up before it is run.
> There are two files that need sourcing.  One a one point in time and the
> other later.
> Using a Solaris Unix system, I usually type :
> source <filename> in a terminal when needed and away we go.
> This process needs to be automated into the Perl script.

> Does this help ?

Yes, though I'm not sure what you mean about the files having to be
sourced separately/at different times.

You can Write a csh script like this:

#!/bin/csh
# source_and_run.csh
source your_csh_script
Your_Perl-TK_script

Please forgive me if my csh code is bad. I've never used csh, and tcsh
only briefly.

This may be a good time to call ModelSim's tech support.  Without
your posting relavant code, we may not be able to help you much more.

If this is the ModelSim you're talking about (I'm guessing it is), the
following URL and their site sounds like a good starting point and names
a few newsgroups that are probably more familiar with ModelSim than I am
personally.
    http://www.model.com/resources/default.asp

BTW: `perldoc -f system` would've told you that "source $sourceme" was
running in a bourne shell (/bin/sh), not csh.

--
Civilization is the limitless multiplication of unnecessary necessities.
    -- Mark Twain



Mon, 15 Mar 2004 12:19:55 GMT  
 Sourcing Things in Perl ?

Quote:
> Is the file you're sourcing a shell script?

> >I'm not sure, from what I can see it looks like a shell script.
> >It contains a load of 'setenv' and 'echo' commands and thats about it.

> Is it for the same shell that the perl script is running under?

> >Yes.

> Is it the for the same shell that an individual user of your script uses?

> >Yes.

> Can you guarantee that this is always the case?

> >Yes.

> Are you checking the return value of system?  Was it successful?

> >I don't know how.  Would a 'die' command surfice ?

Your quoting is badly, badly messed up and virtually incomprehensible.

jue



Mon, 15 Mar 2004 18:20:18 GMT  
 Sourcing Things in Perl ?

on MMCMXLIX September MCMXCIII in <URL::">
{} Okay, forgive my ignorance.
{} I have a program called ModelSim that I execute from a Perl Tk script.
{} It needs certain environment variables set up before it is run.
{} There are two files that need sourcing.  One a one point in time and theSNIP
{} I have limited knowledge of the source files, they have lots of 'setenv'SNIP
{} them and various 'echos' to let you know whats going on but thats all.
{} Using a Solaris Unix system, I usually type :
{} source <filename> in a terminal when needed and away we go.
{} This process needs to be automated into the Perl script.
{} As explained I have just tried to use perls 'system' command to perform SNIP
{} that I type at the prompt and this did not have the desired affect - wheSNIP
{} source the file or not.
{} I'd assume any changes need to be visible when ModelSim is run.

Use one system command, not two.

    system "source $sourceme; modelsim";

That will start *one* shell, which first uses source (setting environment
variables), then executes modelsim.

Abigail
--
perl -we '$| = 1; $_ = "Just another Perl Hacker\n";  print
          substr  $_ => 0, 1 => "" while $_ && sleep 1 => 1'



Mon, 15 Mar 2004 23:43:07 GMT  
 Sourcing Things in Perl ?

Quote:
>> On 27 Sep 2001 22:43:07 GMT,

> Use one system command, not two.
>     system "source $sourceme; modelsim";
> That will start *one* shell, which first uses source
> (setting environment variables), then executes modelsim.

system() invokes the Bourne shell (/bin/sh).

Using "." instead of "source" would probably make more
sense (bash is not masquerading as sh everywhere, nor is
one's login shell involved).

hth
t
--
Yes way!  Mmmmkay?



Tue, 16 Mar 2004 01:22:06 GMT  
 Sourcing Things in Perl ?

Quote:
> >> On 27 Sep 2001 22:43:07 GMT,

> > Use one system command, not two.
> >     system "source $sourceme; modelsim";
> > That will start *one* shell, which first uses source
> > (setting environment variables), then executes modelsim.

> system() invokes the Bourne shell (/bin/sh).

> Using "." instead of "source" would probably make more
> sense (bash is not masquerading as sh everywhere, nor is
> one's login shell involved).

btw, how would /bin/sh handle all the setenv's in a csh script?

--
/earth: file system full.



Tue, 16 Mar 2004 02:50:30 GMT  
 Sourcing Things in Perl ?

Quote:
>> On Fri, 28 Sep 2001 01:50:30 GMT,


> dijo:

>> system() invokes the Bourne shell (/bin/sh).

>> Using "." instead of "source" would probably make more
>> sense (bash is not masquerading as sh everywhere, nor
>> is one's login shell involved).
> btw, how would /bin/sh handle all the setenv's in a csh
> script?

It wouldn't.  At least, not directly.  You could use
system() to invoke csh and call the C-shell script though.

Of course, as we all know, only nuts write C-shell
scripts (google: csh why not christiansen) :-)

--
Yes way!  Mmmmkay?



Tue, 16 Mar 2004 03:02:35 GMT  
 Sourcing Things in Perl ?

Quote:
> On Thu, 27 Sep 2001 09:51:24 +0100,


[...]

Quote:
> > system ( "source $sourceme" );

> Some shells have that as a builtin command, but I haven't seen it as an
> external command on any system I've worked on.

It can't be an external program (under Unix).  Its purpose is to
interpret commands in the current (shell-) process.  An external
command would always be run in a process of its own.

Just a side note.

Anno



Tue, 16 Mar 2004 15:24:02 GMT  
 Sourcing Things in Perl ?

Quote:
> >> On Fri, 28 Sep 2001 01:50:30 GMT,


> > dijo:

> >> system() invokes the Bourne shell (/bin/sh).

> >> Using "." instead of "source" would probably make more
> >> sense (bash is not masquerading as sh everywhere, nor
> >> is one's login shell involved).

> > btw, how would /bin/sh handle all the setenv's in a csh
> > script?

> It wouldn't.  At least, not directly.  You could use
> system() to invoke csh and call the C-shell script though.

I guess my sarcasm wasn't thick enough.  I only meant to implant the
thought that, even if you use the sh equiv of csh's 'source' (ie, '.'),
it wouldn't understand the csh-specific commands.

Quote:
> Of course, as we all know, only nuts write C-shell
> scripts (google: csh why not christiansen) :-)

Nice. =)  I'm a Bourne Again Shell man, meself.  Only messed with tcsh
briefly (my remote shell account only had that and zsh; I messed with
both).

bash++

--
M-x induce-carpal-tunnel-syndrome
    -- Greg Bacon



Tue, 16 Mar 2004 22:47:07 GMT  
 Sourcing Things in Perl ?
On 28 Sep 2001 14:24:02 GMT,

Quote:

>> On Thu, 27 Sep 2001 09:51:24 +0100,

> [...]

>> > system ( "source $sourceme" );

>> Some shells have that as a builtin command, but I haven't seen it as an
>> external command on any system I've worked on.

> It can't be an external program (under Unix).  Its purpose is to
> interpret commands in the current (shell-) process.  An external
> command would always be run in a process of its own.

> Just a side note.

I didn't think that needed explanation in the context of this thread :)

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Hi, Dave here, what's the root
Commercial Dynamics Pty. Ltd.   | password?
NSW, Australia                  |



Wed, 17 Mar 2004 00:29:08 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Things found while looking for other things

2. Sourcing from within perl ?

3. Sourcing in shell scripts into perl

4. sourcing (bash) config files within perl

5. Problems with sourcing a file in perl.

6. sourcing a file in Perl

7. sourcing csh scripts from within perl

8. "Sourcing" common Perl ENV values

9. sourcing bash script w/ Perl?

10. sourcing a ksh script within PERL

11. sourcing (bash) config files within perl

12. Sourcing files from my perl script

 

 
Powered by phpBB® Forum Software