config::inifiles and retrieval of delimiter

Discussion in 'Perl Misc' started by Domenico Discepola, Nov 28, 2003.

  1. Hi all. I'm using the Config::IniFiles module & I want to store a literal
    representation of a delimiter in an ini file. For ex, my ini file looks
    like:

    [GLOBAL]
    #double quote, backslash, n, double quote
    delimiter = "\n"

    when I retrieve the value of the parameter delimiter, my perl variable
    ${delimiter} is obviously storing the literal '\n';

    my ${my_iniinput} = Config::IniFiles->new( -file => "$file_ini", -nocase =>
    1);
    my ${delimiter} = ${my_iniinput}->val('GLOBAL', 'delimiter');

    Is there a way to assign the "interpreted" value into my variable?

    Unfortunately, I didn't know how to search for such a function in perldoc.

    TIA
    Domenico Discepola, Nov 28, 2003
    #1
    1. Advertising

  2. Domenico Discepola

    Bob Walton Guest

    Domenico Discepola wrote:

    > Hi all. I'm using the Config::IniFiles module & I want to store a literal
    > representation of a delimiter in an ini file. For ex, my ini file looks
    > like:
    >
    > [GLOBAL]
    > #double quote, backslash, n, double quote
    > delimiter = "\n"
    >
    > when I retrieve the value of the parameter delimiter, my perl variable
    > ${delimiter} is obviously storing the literal '\n';
    >
    > my ${my_iniinput} = Config::IniFiles->new( -file => "$file_ini", -nocase =>
    > 1);
    > my ${delimiter} = ${my_iniinput}->val('GLOBAL', 'delimiter');
    >
    > Is there a way to assign the "interpreted" value into my variable?



    Sure. Just eval it in an interpolated string, like [untested]:

    my $delimiter=eval "\"$my_iniinput->val('GLOBAL','delimiter')\"";

    ....

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Nov 28, 2003
    #2
    1. Advertising

  3. Domenico Discepola

    Ben Morrow Guest

    "Domenico Discepola" <> wrote:
    > Hi all. I'm using the Config::IniFiles module & I want to store a literal
    > representation of a delimiter in an ini file. For ex, my ini file looks
    > like:
    >
    > [GLOBAL]
    > #double quote, backslash, n, double quote
    > delimiter = "\n"
    >
    > when I retrieve the value of the parameter delimiter, my perl variable
    > ${delimiter} is obviously storing the literal '\n';
    >
    > my ${my_iniinput} = Config::IniFiles->new( -file => "$file_ini", -nocase =>
    > 1);
    > my ${delimiter} = ${my_iniinput}->val('GLOBAL', 'delimiter');


    <untested>

    my %backwhacked = (
    '\\n' => '"\n"',
    '\\r' => '"\r"',
    '\\t' => '"\t"',
    '\\x{([[:xdigit:]]+)} => 'chr hex $1',
    '\\x([[:xdigit:]]{2})' => 'chr hex $1',
    '\\(0\d{1,3})' => 'chr oct $1',
    etc...
    );

    $delimiter =~ s/$_/$backwhacked{$_}/ge for keys %backwhacked;

    Ben

    --
    'Deserve [death]? I daresay he did. Many live that deserve death. And some die
    that deserve life. Can you give it to them? Then do not be too eager to deal
    out death in judgement. For even the very wise cannot see all ends.'
    :-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:
    Ben Morrow, Nov 28, 2003
    #3
  4. Domenico Discepola

    Ben Morrow Guest

    d wrote:
    > Domenico Discepola wrote:
    > > Is there a way to assign the "interpreted" value into my variable?

    >
    > Sure. Just eval it in an interpolated string, like [untested]:
    >
    > my $delimiter=eval "\"$my_iniinput->val('GLOBAL','delimiter')\"";


    Better is:

    my $delimiter = $my_iniinput->val(...);
    eval "\$delimiter = <<__EOS__;\n$delimiter\n__EOS__";
    substr($delimiter, -1) = "";

    as someone (Brian?) posted here not long ago. But

    BE CAREFUL...

    While this is the Right Answer, it will also intepolate all manner of
    other things, most of which are probably undesirable. Consider
    carefully before you use this.

    (This is not to say that having the format of your config file be Perl
    code is necessarily a bad thing, just that you need to be aware of the
    implications of this.)

    Ben

    --
    I've seen things you people wouldn't believe: attack ships on fire off the
    shoulder of Orion; I've watched C-beams glitter in the darkness near the
    Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
    Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|
    Ben Morrow, Nov 28, 2003
    #4
  5. > Sure. Just eval it in an interpolated string, like [untested]:
    >
    > my $delimiter=eval "\"$my_iniinput->val('GLOBAL','delimiter')\"";
    >

    Thank you for the code - I was able to use it to come up with this example
    as well:

    #!perl

    use strict;
    use warnings;

    my $a = '\n';
    my $b = eval "\"$a\"";
    print "[$b]\n";
    ########################
    Output:
    [
    ]
    Domenico Discepola, Nov 30, 2003
    #5
  6. Ben Morrow <> writes:

    > d wrote:
    > > Domenico Discepola wrote:
    > > > Is there a way to assign the "interpreted" value into my variable?

    > >
    > > Sure. Just eval it in an interpolated string, like [untested]:
    > >
    > > my $delimiter=eval "\"$my_iniinput->val('GLOBAL','delimiter')\"";

    >
    > my $delimiter = $my_iniinput->val(...);
    > eval "\$delimiter = <<__EOS__;\n$delimiter\n__EOS__";


    BTW ISTRT on older Perls you may need another \n on the end.

    I would take the LHS of the assignment outside the eval(). Oh, and
    semicolon is redundant:

    $delimiter = eval "<<__EOS__\n$delimiter\n__EOS__";

    > substr($delimiter, -1) = "";


    There is a special Perl function to remove the last character:

    chop($delimter);

    You can even combine them:

    chop($delimiter = eval "<<__EOS__\n$delimiter\n__EOS__");

    > as someone (Brian?) posted here not long ago. But


    While I do have a bee in my bonnet about this, I have to admit that I
    didn't come up with the here-doc trick myself. I read about it here.

    > BE CAREFUL...
    >
    > While this is the Right Answer, it will also intepolate all manner of
    > other things, most of which are probably undesirable. Consider
    > carefully before you use this.
    >
    > (This is not to say that having the format of your config file be Perl
    > code is necessarily a bad thing, just that you need to be aware of the
    > implications of this.)


    Indeed - there are many times when permission to edit the config file
    already implies permission to execute arbritrary code. The most
    obvious case is when the config file contains the path of an external
    program to be run. When this condition exists anyhow there's no security
    implication in using eval().

    Consider using the String::Interpolate module. Be aware that while
    String::Interpolate provides some safety it relies on the Safe module.
    It plugs some of the known vulnerabilities of Safe but there are
    almost certainly vulnerabilities it does not.

    Oh, and the API of String::Interpolate is utterly Baroque, it was
    kinda written as an example of all the different API models available
    to a Perl module.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Dec 1, 2003
    #6
    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. George Sew
    Replies:
    2
    Views:
    484
    bruce barker
    Feb 12, 2004
  2. news.microsoft.com

    ASP.net ques: Webpage data retrieval and parsing

    news.microsoft.com, Jan 16, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,977
    Rajesh Kumar
    Feb 1, 2005
  3. =?iso-8859-1?q?J=FCrgen_Gerstacker?=

    inifiles with serialize ??

    =?iso-8859-1?q?J=FCrgen_Gerstacker?=, Jan 12, 2007, in forum: Java
    Replies:
    1
    Views:
    326
    Tom Hawtin
    Jan 12, 2007
  4. CSharpner
    Replies:
    0
    Views:
    1,015
    CSharpner
    Apr 9, 2007
  5. Thomas Watson Steen
    Replies:
    4
    Views:
    339
    Sisyphus
    Oct 4, 2004
Loading...

Share This Page