FAQ
I use a function to return a value:
"$ENV{HOME}/perl/qotw/qotw13/mb"; which is then assigned to $mb

But the $ENV{HOME} part is not being interpreted, so this sample:

my $mb = get_config('mb', '.get_quiz');
print "$mb\n";
exit;

produces literally: "$ENV{HOME}/perl/qotw/qotw13/mb" instead of:
/home/pfeiffer/perl/qotw/...

If I do a simple assignment:

my $mb = "$ENV{HOME}/etc..." then this works fine:
/home/pfeiffer/etc...

(As always, thanks for the tips and advice)


--
Kevin Pfeiffer
International University Bremen

Search Discussions

  • Tassilo von Parseval at May 31, 2003 at 6:16 am

    On Sat, May 31, 2003 at 01:50:51AM +0200 Kevin Pfeiffer wrote:

    I use a function to return a value:
    "$ENV{HOME}/perl/qotw/qotw13/mb"; which is then assigned to $mb

    But the $ENV{HOME} part is not being interpreted, so this sample:

    my $mb = get_config('mb', '.get_quiz');
    print "$mb\n";
    exit;

    produces literally: "$ENV{HOME}/perl/qotw/qotw13/mb" instead of:
    /home/pfeiffer/perl/qotw/...
    Can you post the (potentially) relevant bits of get_config()? Sounds as
    though this function reads in a configuration file and returns the
    requested values. Is "$ENV{HOME}/perl/qotw/qotw13/mb" one such line of
    this config file? If so, it would be data and not code and you'd have to
    eval() it or so.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
  • Kevin Pfeiffer at May 31, 2003 at 8:28 am
    Hi Tassilo (and CKC),

    In article <20030531055125.GA424@ethan>, Tassilo Von Parseval wrote:
    On Sat, May 31, 2003 at 01:50:51AM +0200 Kevin Pfeiffer wrote:

    I use a function to return a value:
    "$ENV{HOME}/perl/qotw/qotw13/mb"; which is then assigned to $mb

    But the $ENV{HOME} part is not being interpreted, so this sample:

    my $mb = get_config('mb', '.get_quiz');
    print "$mb\n";
    exit;

    produces literally: "$ENV{HOME}/perl/qotw/qotw13/mb" instead of:
    /home/pfeiffer/perl/qotw/...
    Can you post the (potentially) relevant bits of get_config()? Sounds as
    though this function reads in a configuration file and returns the
    requested values. Is "$ENV{HOME}/perl/qotw/qotw13/mb" one such line of
    this config file? If so, it would be data and not code and you'd have to
    eval() it or so.
    Sorry about leaving out the routine:

    sub get_config {
    my ($setting, $cfg_file) = @_; # setting is 'mb'

    open DATA, "< $ENV{'HOME'}/$cfg_file"
    or die "Error, could not open $cfg_file : $!";

    while (<DATA>) {
    next if (/^[#\s]*$/);
    $result = $1 if (/^$setting\s*=\s*"?([^"]*)"?\s*$/);
    }

    close DATA;
    return $result;
    }

    Data file looks like:
    mb = "$ENV{'HOME'}/perl/qotw/qotw13/mb"

    It already occurred to me that I might need an eval and I've tried using it
    in every position and way possible it seems, but the env variable still
    doesn't get interpreted.

    --
    Kevin Pfeiffer
    International University Bremen
  • Tassilo von Parseval at May 31, 2003 at 8:54 am

    On Sat, May 31, 2003 at 10:30:02AM +0200 Kevin Pfeiffer wrote:

    In article <20030531055125.GA424@ethan>, Tassilo Von Parseval wrote:
    On Sat, May 31, 2003 at 01:50:51AM +0200 Kevin Pfeiffer wrote:

    I use a function to return a value:
    "$ENV{HOME}/perl/qotw/qotw13/mb"; which is then assigned to $mb

    But the $ENV{HOME} part is not being interpreted, so this sample:

    my $mb = get_config('mb', '.get_quiz');
    print "$mb\n";
    exit;

    produces literally: "$ENV{HOME}/perl/qotw/qotw13/mb" instead of:
    /home/pfeiffer/perl/qotw/...
    Can you post the (potentially) relevant bits of get_config()? Sounds as
    though this function reads in a configuration file and returns the
    requested values. Is "$ENV{HOME}/perl/qotw/qotw13/mb" one such line of
    this config file? If so, it would be data and not code and you'd have to
    eval() it or so.
    Sorry about leaving out the routine:

    sub get_config {
    my ($setting, $cfg_file) = @_; # setting is 'mb'

    open DATA, "< $ENV{'HOME'}/$cfg_file"
    or die "Error, could not open $cfg_file : $!";

    while (<DATA>) {
    next if (/^[#\s]*$/);
    $result = $1 if (/^$setting\s*=\s*"?([^"]*)"?\s*$/);
    }

    close DATA;
    return $result;
    }

    Data file looks like:
    mb = "$ENV{'HOME'}/perl/qotw/qotw13/mb"
    Ah, there's the delinquent. Indeed, it's data and not (yet) code.
    It already occurred to me that I might need an eval and I've tried using it
    in every position and way possible it seems, but the env variable still
    doesn't get interpreted.
    In your case, you have to build an expression that, when evaluated,
    returns the expanded string. That's not so hard...all you have to watch
    is the use of quotations:

    my $val = q!$ENV{'HOME'}/perl/qotw/qotw13/mb!;
    my $mb = eval qq! "$val" !;
    print $mb;

    Bear in mind that simply evaling $ENV{HOME}/etc./.../ wont work because
    it is not a proper Perl expression. You need to add double-quotes to
    turn it into one.

    But I guess you already know that evaling strings from a config-file
    could be a problem, depending on the context in which you use it. It's
    totally ok, though, if you have control over the content of the file.

    Still, you could also put

    mb = "~/perl/quotw/qotw13/mb"

    and expand the tilde. 'perldoc -q tilde' comes up with a regex-solution.
    Using glob (which is also mentioned) is more straightforward, however:

    my $mv = glob('~/perl/qotw/qotw13/mb');

    will do the right thing.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
  • Kevin Pfeiffer at May 31, 2003 at 10:36 am
    In article <20030531084422.GA18329@ethan>, Tassilo Von Parseval wrote:
    [...]
    Still, you could also put

    mb = "~/perl/quotw/qotw13/mb"

    and expand the tilde. 'perldoc -q tilde' comes up with a regex-solution.
    Using glob (which is also mentioned) is more straightforward, however:

    my $mv = glob('~/perl/qotw/qotw13/mb');

    will do the right thing.
    Thanks, this worked like a charm (using the tilde, which I think is
    preferable):

    $result = glob($1) if (/^$setting\s*=\s*"?([^"]*)"?\s*$/);

    --
    Kevin Pfeiffer
    International University Bremen

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 30, '03 at 11:49p
activeMay 31, '03 at 10:36a
posts5
users2
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase