mister_t has asked for the wisdom of the Perl Monks concerning the following question:

Greetings... Novice Perl person here... I need to perform a few string manipulations involving environment variables... basically first I need to evaluate that a string contains a variable positioned bewtween $ delimeters. I then need to substitute the actual value of the variable into the string to replace the variable in the string... in a nutshell, I have to convert: string myfile$BASE_ID$.xxx to myfile5.21.xxx or myfile$REVISION_ID$.xxx to myfile77N-13.xxx where BASE_ID and REVISION_ID are environment variables. thanks for any help...

Replies are listed 'Best First'.
Re: substitute the value of a variable
by particle (Vicar) on May 21, 2002 at 17:12 UTC
    the %ENV hash contains environment variables. check it with something like if( defined $ENV{BASE_ID} ) { ...do something... }. you'll also need to set up a list of tags you're looking for, like my @tags = qw/ $BASE_ID$ $REVISION_ID$ /;. search the file and match against your tags (you'll probably need quotemeta for that. if you find a valid tag, and the environment variable exists, replace it with the value of the environment variable.

    i hope that helps. if you have trouble coding a solution, post your code, and we'll gladly help.

    ~Particle *accelerates*

Re: substitute the value of a variable
by Coplan (Pilgrim) on May 21, 2002 at 17:20 UTC
    I am first curious as to where the information you have provided came from. If you have already extracted just that string, then it might be a simple regexp to extract that information.

    Second, I would like to know whether you already have determined that the two file names are using said identifiers. Namely, I would like to know how you're going to plan on determining the difference between a BASE_ID and a REVISION_ID.

    I've had to do something similar with a parsing program that I have. The details are not important, but I had it set up like this (I'll use you're info). You'll notice that in this situation, I didn't need a regexp -- but you might need to (and thus the reason why the above information might be important). If you're creating these file names, just use the joining functions.

    Template: myfile!BASE_ID!.xxx

    #!/usr/bin/perl use strict; my $Base_ID = getBaseID(); # some subroutine that gets this my $filename = "myfile" . $Base_ID . ".xxx"; print $filename; # eof

    --Coplan

Re: substitute the value of a variable
by educated_foo (Vicar) on May 21, 2002 at 17:09 UTC
    What have you tried so far? If you could post the offending code, it would make it easier to see what's going wrong.

    /s