Professional Web Applications Themes

How do you simulate "." or "source" in a perl script ? - Perl / CGI

On 7 Aug 2003 08:21:27 -0700 [email]c_j_marshallhotmail.com[/email] (Chris Marshall) wrote: > > Is there a good way of simulating the bourne shell's "." command - or > csh's "source" command inside a perl script ? > > Specifically I would like to set environment variables inside a perl > script which are stored inside a separate config file (used by many > other programs) > > At the moment I have a separate ksh script wrapper around my perl > script which simply does something like > > #!/bin/ksh > > . /dir/config.sh > > /dir/perlscript.pl > > but there's ...

  1. #1

    Default Re: How do you simulate "." or "source" in a perl script ?

    On 7 Aug 2003 08:21:27 -0700
    [email]c_j_marshallhotmail.com[/email] (Chris Marshall) wrote:
    >
    > Is there a good way of simulating the bourne shell's "." command - or
    > csh's "source" command inside a perl script ?
    >
    > Specifically I would like to set environment variables inside a perl
    > script which are stored inside a separate config file (used by many
    > other programs)
    >
    > At the moment I have a separate ksh script wrapper around my perl
    > script which simply does something like
    >
    > #!/bin/ksh
    >
    > . /dir/config.sh
    >
    > /dir/perlscript.pl
    >
    > but there's got to be a better way than that.
    >
    > Unfortunately neither "." nor "source" are particularly easy things to
    > search for and I've not spotted anything in either perldoc nor
    > googling this newsgroup.
    >
    > I guess I could have the perl script read the config file explicitly
    > and act upon what it finds there - but someone must have tackled this
    > already.
    >
    > Thanks
    > Chris
    Shell::Source
    perhaps this CPAN module will do it for you.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    drop the .thetenant to get me via mail
    John Strauss Guest

  2. #2

    Default Re: How do you simulate "." or "source" in a perl script ?

    > Specifically I would like to set environment variables inside a perl
    > script which are stored inside a separate config file (used by many
    > other programs)
    If you want to set environmental variables from within Perl, %ENV is
    the place to set such items. perldoc perlvar for more information.
    >
    > At the moment I have a separate ksh script wrapper around my perl
    > script which simply does something like
    >
    > #!/bin/ksh
    >
    > . /dir/config.sh
    >
    > /dir/perlscript.pl
    No offense, but what's the issue with this? It seems like it does
    what you wnat it to do. If you want ALL scripts to have the SAME
    environmental variables, why not set them from within .profile or
    ..bashrc or.cshrc or some variation of the three based upon your shell?
    >
    > but there's got to be a better way than that.
    >
    > Unfortunately neither "." nor "source" are particularly easy things
    > to search for and I've not spotted anything in either perldoc nor
    > googling this newsgroup.
    Because it's not portable. Perl is designed with portablity in mind.
    WIN32 systems don't use '.' or 'source' without some help (ie
    Cygwin). Might I suggest you read some more of the Perl
    doentation. Perl is not your average scripting language (like
    C-Shell, BASH, KSH, etc).
    >
    > I guess I could have the perl script read the config file explicitly
    > and act upon what it finds there - but someone must have tackled
    > this already.
    This is a workable solution if you can't keep the environmental
    variables set in the parent shell. It's also more portable. If you
    decide, one day, to run the scripts on a WIN32 machine, then you don't
    have to re-invent the wheel to do so. There are several modules to
    aid in this task -or- you could roll your own.

    HTH

    Jim
    James Willmore Guest

  3. #3

    Default Re: How do you simulate "." or "source" in a perl script ?

    Chris Marshall (c_j_marshallhotmail.com) wrote on MMMDCXXVIII September
    MCMXCIII in <URL:news:cb9c7b76.0308070721.2f4cd3f5posting.goo gle.com>:
    && Is there a good way of simulating the bourne shell's "." command - or
    && csh's "source" command inside a perl script ?
    &&
    && Specifically I would like to set environment variables inside a perl
    && script which are stored inside a separate config file (used by many
    && other programs)
    &&
    && At the moment I have a separate ksh script wrapper around my perl
    && script which simply does something like
    &&
    && #!/bin/ksh
    &&
    && . /dir/config.sh
    &&
    && /dir/perlscript.pl
    &&
    && but there's got to be a better way than that.
    &&
    && Unfortunately neither "." nor "source" are particularly easy things to
    && search for and I've not spotted anything in either perldoc nor
    && googling this newsgroup.
    &&
    && I guess I could have the perl script read the config file explicitly
    && and act upon what it finds there - but someone must have tackled this
    && already.


    An often asked question is 'how to "source" a file', like you do in the
    shell, to set one or more environment variables. The easy answer is:
    first source the file, then start the program. But what if the file
    to source isn't known in advance? Or it's impossible to write a wrapper
    script? Put the code below at or near the beginning of your program.

    The code below runs in combination with the Korn shell, and probably any
    other Bourne shell decendent, including the POSIX shell. I don't know
    whether this works in the standard Windows shell - probably not due to
    quoting issues, but getting it to run under Cygwin/bash should not be
    a problem.

    Abigail

    my $ENVIRONMENT = "/some/file/with/environment/variables/";

    if (ARGV && $ARGV [0] eq '--sourced_environment') {
    shift;
    }
    else {
    if (-f $ENVIRONMENT) {
    #
    # Now we perform a double exec. The first exec gives us a shell,
    # allowing us the source the file with the environment variables.
    # Then, from within the shell we re-exec ourself - but with an
    # argument that will prevent us from going into infinite recursion.
    #
    # We cannot do a 'system "source $ENVIRONMENT"', because
    # environment variables are not propagated to the parent.
    #
    # Note the required trickery to do the appropriate shell quoting
    # when passing ARGV back to ourselves.
    #
    # Also note that the program shouldn't normally be called with
    # '--sourced_environment' - if so, pick something else.
    #

    ARGV = map {s/'/'"'"'/g; "'$_'"} ARGV;

    exec << " --";
    source '$ENVIRONMENT'
    exec $0 --sourced_environment ARGV;
    --
    die "This should never happen.";
    }
    }
    --
    $_ = "\112\165\163\1648\141\156\157\164\150\145\1628\12 0\145"
    . "\162\1548\110\141\143\153\145\162\0128\177" and &japh;
    sub japh {print "_" and return if pop; split /\d/ and &japh}
    Abigail Guest

  4. #4

    Default Re: How do you simulate "." or "source" in a perl script ?

    James Willmore <jwillmorecyberia.com> wrote in message news:<20030808111536.71614720.jwillmorecyberia.co m>...
    > > Specifically I would like to set environment variables inside a perl
    > > script which are stored inside a separate config file (used by many
    > > other programs)
    > >
    > > At the moment I have a separate ksh script wrapper around my perl
    > > script which simply does something like
    > >
    > > #!/bin/ksh
    > >
    > > . /dir/config.sh
    > >
    > > /dir/perlscript.pl
    >
    > No offense, but what's the issue with this? It seems like it does
    > what you wnat it to do. If you want ALL scripts to have the SAME
    > environmental variables, why not set them from within .profile or
    > .bashrc or.cshrc or some variation of the three based upon your shell?
    >
    Only that it means having two scripts instead of one - and it seems a
    shame that the perl script would be reliable on a ksh script to run.
    Chris Marshall Guest

  5. #5

    Default Re: How do you simulate "." or "source" in a perl script ?

    Abigail <abigailabigail.nl> wrote in message news:<slrnbj7gfm.39l.abigailalexandra.abigail.nl> ...
    > Chris Marshall (c_j_marshallhotmail.com) wrote on MMMDCXXVIII September
    > MCMXCIII in <URL:news:cb9c7b76.0308070721.2f4cd3f5posting.goo gle.com>:
    > && Is there a good way of simulating the bourne shell's "." command - or
    > && csh's "source" command inside a perl script ?
    > &&
    > The code below runs in combination with the Korn shell, and probably any
    > other Bourne shell decendent, including the POSIX shell. I don't know
    > whether this works in the standard Windows shell - probably not due to
    > quoting issues, but getting it to run under Cygwin/bash should not be
    > a problem.
    >
    <snip most of the code example>
    > exec << " --";
    > source '$ENVIRONMENT'
    > exec $0 --sourced_environment ARGV;

    Brilliant - thank you.
    BTW I changed "source" in your script to be "." to get it working with
    ksh. Should source have worked ? I thought that source was just for
    csh.
    Chris Marshall Guest

Similar Threads

  1. Replies: 1
    Last Post: April 24th, 01:27 PM
  2. Replies: 2
    Last Post: December 8th, 02:15 AM
  3. CFINPUT type="radio" w/ "value" requires "label"
    By Iceborer in forum Macromedia ColdFusion
    Replies: 2
    Last Post: February 21st, 06:16 PM
  4. dr("field").toString returns "400.0000" instead of "400"
    By Dan C Douglas in forum ASP.NET General
    Replies: 5
    Last Post: July 22nd, 05:48 PM
  5. "Start" "Program" "Menu" list is empty
    By Pete in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 10th, 10:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139