Sourcing from within perl ? 
Author Message
 Sourcing from within perl ?

Hi all,

In a bash script I can write :

#!/usr/bin/bash
echo "before sourcing foo.sh environment variable FOO_GILLES is [$FOO_GILLES]"
# foo.sh will set and export FOO_GILLES
source foo.sh
echo "after sourcing I can play with environment variable FOO_GILLES is [$FOO_GILLES]"
# end of shell script.

Question : I want to source foo.sh from within a perl script. Looks simple
isn't it ? Any idea ?

Gilles

#################################################
PS : I gave it a try with a perl routine like

# Begining of example 1
use strict;
sub source( $ )
{
    my $script  = $_[0];
    my $source  = ($^O =~ /MSWin32/ ? "" : ".");
    my $cmd_sep = ($^O =~ /MSWin32/ ? " && " : " ; ");
    open(FOO, "$source  $script $cmd_sep"
             .'perl -e \'print "___env_start___\n", join "\n", %ENV;\'|');
    while(<FOO>){
        /___env_start___/ and last;
        print;
    }

    close(FOO);


    return 1;

Quote:
}

print("FOO_GILLES is [$ENV{FOO_GILLES}]\n");
Source("foo.sh");
print("After source FOO_GILLES is [$ENV{FOO_GILLES}]\n");
# end of example 1

Problem : open will use the system shell, was /bin/sh on my box,  which is
a problem if foo.sh is a *bash* script.

PS2 : Also tried recursive call like

# Begining of example 2
[...skiped...]


# End of example 2

but too much overhead involved.

--------------------------------------------------
Gilles Tabary
--------------------------------------------------



Tue, 13 Apr 2004 00:04:10 GMT  
 Sourcing from within perl ?
Perhaps it would be easier to parse the file yourself rather than trying to
do it using shell. It seems you are doing most of the work anyway.


Quote:

> Hi all,

> In a bash script I can write :

> #!/usr/bin/bash
> echo "before sourcing foo.sh environment variable FOO_GILLES is
[$FOO_GILLES]"
> # foo.sh will set and export FOO_GILLES
> source foo.sh
> echo "after sourcing I can play with environment variable FOO_GILLES is
[$FOO_GILLES]"
> # end of shell script.

> Question : I want to source foo.sh from within a perl script. Looks simple
> isn't it ? Any idea ?

> Gilles

> #################################################
> PS : I gave it a try with a perl routine like

> # Begining of example 1
> use strict;
> sub source( $ )
> {
>     my $script  = $_[0];
>     my $source  = ($^O =~ /MSWin32/ ? "" : ".");
>     my $cmd_sep = ($^O =~ /MSWin32/ ? " && " : " ; ");
>     open(FOO, "$source  $script $cmd_sep"
>              .'perl -e \'print "___env_start___\n", join "\n", %ENV;\'|');
>     while(<FOO>){
>         /___env_start___/ and last;
>         print;
>     }

>     close(FOO);


>     return 1;
> }

> print("FOO_GILLES is [$ENV{FOO_GILLES}]\n");
> Source("foo.sh");
> print("After source FOO_GILLES is [$ENV{FOO_GILLES}]\n");
> # end of example 1

> Problem : open will use the system shell, was /bin/sh on my box,  which is
> a problem if foo.sh is a *bash* script.

> PS2 : Also tried recursive call like

> # Begining of example 2
> [...skiped...]


> # End of example 2

> but too much overhead involved.

> --------------------------------------------------
> Gilles Tabary
> --------------------------------------------------



Sun, 18 Apr 2004 21:44:52 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. sourcing (bash) config files within perl

2. sourcing csh scripts from within perl

3. sourcing a ksh script within PERL

4. sourcing (bash) config files within perl

5. Sourcing shell from within Perl

6. Sourcing in shell scripts into perl

7. Problems with sourcing a file in perl.

8. Sourcing Things in Perl ?

9. sourcing a file in Perl

10. "Sourcing" common Perl ENV values

11. sourcing bash script w/ Perl?

12. Sourcing files from my perl script

 

 
Powered by phpBB® Forum Software