sourcing a file from perl

Discussion in 'Perl Misc' started by JAWS, Oct 29, 2003.

  1. JAWS

    JAWS Guest

    I need to use the csh source command or anything equivalent form a perl
    script but i can't access the source command because the shell fired by
    perl is sh. Ideally, i also need to be able to use the new environment
    variables in the rest of my perl program...

    thanks in advance for suggestions

    jp
     
    JAWS, Oct 29, 2003
    #1
    1. Advertising

  2. JAWS

    Ben Morrow Guest

    JAWS <> wrote:
    > I need to use the csh source command or anything equivalent form a perl
    > script but i can't access the source command because the shell fired by
    > perl is sh. Ideally, i also need to be able to use the new environment
    > variables in the rest of my perl program...


    In general what you want to do can't be done: csh and Perl are
    different languages, and perl does not understand csh. It's the same
    as if you tried to source a (complicated) sh script from in csh: it
    just wouldn't work.

    Your best bet is to read the file yourself in the Perl program, and
    extract the variable definitions out of it. If it is a complicated
    script (ie. the variable settings are not easy to parse) then you may
    be better off writing a little csh script which sources the file and
    prints out the variables, and then opening a pipe to that script (see
    perldoc -f open, the bit about opening a name ending in '|') and
    parsing the results instead.

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Oct 29, 2003
    #2
    1. Advertising

  3. JAWS

    Tony Curtis Guest

    >> On Wed, 29 Oct 2003 18:14:19 -0500,
    >> JAWS <> said:


    > I need to use the csh source command or anything
    > equivalent form a perl script but i can't access the
    > source command because the shell fired by perl is sh.
    > Ideally, i also need to be able to use the new
    > environment variables in the rest of my perl program...


    Usually the only sane solution to this is to wrap the perl
    program in a csh script that sources the (sub-)script
    first.

    If the source'd script is just a few setenv's though then
    maybe you could just read it in as data and parse it into
    $ENV assignments.

    hth
    t
     
    Tony Curtis, Oct 29, 2003
    #3
  4. JAWS () wrote:
    : I need to use the csh source command or anything equivalent form a perl
    : script but i can't access the source command because the shell fired by
    : perl is sh.

    So use that shell to fire up csh to source the file.

    Ideally, i also need to be able to use the new environment
    : variables in the rest of my perl program...

    The truth comes out, it's a faq, you can't do what you want directly (on
    unix like systems).

    o You can define the vars before you run perl. This is by far the easiest
    in the long run.

    $ . vars.sh
    $ perl my_script.pl

    o From within perl you check for the variables, and if they are not set
    then use system to run a short shell script that a) sets the variables and
    then b) (re)invokes your perl script

    # just an illustration !!

    if not $ENV{FOOBAR}
    { exit system(". stuff; export FOOBAR=1; perl $0 @ARGV");
    }

    o You can ask the shell script to print the variables and then parse the
    output

    # just an illustration

    #!/bin/sh
    # vars.sh
    MY_VAR="set its value";

    #!perl
    $values = `. vars.sh; echo MY_VAR=$MY_VAR`;
    my ($my_var)=$values=~m/MY_VAR=(.*)/;

    o You can parse the shell script yourself, it's easy for simply X=Y
    assignments.

    #!perl
    # YOU MUST TRUST THE CONFIG FILE (of course you do or you couldn't
    # source it in the shell script either)
    use strict;
    my ($MY_VAR);

    { local @ARGV=qw(vars.sh);
    # local $SIG{__WARN__}=sub{1}; # do we want to see warnings.
    map {eval "\$$_"} grep {/^\w+=/}<>;
    }

    o There are probably modules that will do variations of the above to pull
    configurations variables out of configuration files.
     
    Malcolm Dew-Jones, Oct 30, 2003
    #4
  5. JAWS

    JAWS Guest

    Thanx for all the suggestions, i'll have to call a csh script from my perl
    since i need to do some treatment before the sourcing and it is a pretty
    nasty script that sources others that sources... you get the idea...

    c-ya

    jp

    JAWS wrote:

    > I need to use the csh source command or anything equivalent form a perl
    > script but i can't access the source command because the shell fired by
    > perl is sh. Ideally, i also need to be able to use the new environment
    > variables in the rest of my perl program...
    >
    > thanks in advance for suggestions
    >
    > jp
     
    JAWS, Oct 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kartic

    Sourcing Python Developers

    Kartic, Jan 29, 2007, in forum: Python
    Replies:
    5
    Views:
    620
    John Nagle
    Jan 30, 2007
  2. Replies:
    1
    Views:
    264
    Arne Vajhøj
    Nov 4, 2009
  3. Chris Kilmer

    Sourcing bash aliases with Ruby

    Chris Kilmer, Dec 8, 2006, in forum: Ruby
    Replies:
    6
    Views:
    187
  4. Replies:
    2
    Views:
    126
    Mark Gallop
    Aug 9, 2007
  5. kc
    Replies:
    4
    Views:
    84
Loading...

Share This Page