Re: perl hash utilities

Discussion in 'Perl Misc' started by George Mpouras, Sep 26, 2013.

  1. # hey this is easy !


    #!/usr/bin/perl
    use warnings;
    use strict;

    my %domain = %{get_config()};

    use Data::Dumper;print Dumper \%domain;

    sub get_config
    {
    my $code = '$_=';
    while(<DATA>){$code.=$_}
    eval $code;
    $_
    }






    __DATA__
    {
    my_ftp => {
    domain => 'www.merrillpjensen.com',
    username => 'u63457',
    password => 'mnbvvdfgcxz5',
    },


    site => {
    domain => 'http://translate.google.com/',
    language => 'russian',
    word => 'north',
    },
    }
    George Mpouras, Sep 26, 2013
    #1
    1. Advertising

  2. George Mpouras

    Uri Guttman Guest

    >>>>> "GM" == George Mpouras <> writes:

    GM> # hey this is easy !

    then why can't you format it well too? seriously.

    GM> #!/usr/bin/perl
    GM> use warnings;
    GM> use strict;

    GM> my %domain = %{get_config()};

    GM> use Data::Dumper;print Dumper \%domain;

    GM> sub get_config
    GM> {
    GM> my $code = '$_=';
    GM> while(<DATA>){$code.=$_}
    GM> eval $code;
    GM> $_
    GM> }

    fugly in every possible dimension. slow. poorly formatted. uses $_ for
    no reason to hold the result.

    use File::Slurp ;
    my %domain = %{ eval read_file( \*DATA ) } ;

    even better, drop the outer {} in DATA as it is not needed. then you
    don't need the %{} dereference.

    uri
    Uri Guttman, Sep 26, 2013
    #2
    1. Advertising

  3. George Mpouras

    J. Gleixner Guest

    On 09/27/13 15:59, Cal Dershowitz wrote:
    [...]
    > $ cat template_stuff/ident1.txt
    > {
    > my_ftp = {
    > domain => 'fakename',
    > username => 'fakeuser',
    > password => 'fakepass',
    > },
    >
    > yoursky = {
    > domain => 'http://www.fourmilab.ch/yoursky/',
    > language => 'russian',
    > word => 'north',
    > },
    > }
    >
    > $
    >
    > If I drop the braces, I get a syntax error. If I replace them with
    > parens, I get a syntax error.



    What's creating ident1.txt???.. Why does it have to be in that format????

    It seems that you are complicating things and wasting a lot of time
    by coming up with your own format and then trying to shoehorn that
    format into something usable.

    Go the other way.. Change the format to something that is expected --
    well defined. There's JSON, YAML, Data::Dumper's Freeze/Thaw, XML, etc.

    Pick one and try it. Each one already has classes/methods to create data
    structures out of the data. This will turn into a 5-minute task.
    J. Gleixner, Sep 27, 2013
    #3
  4. > I finally have something usable:


    #!/usr/bin/perl
    use strict;
    use warnings;
    use Config1;


    foreach my $family (keys %config)
    {
    print "$family:\n";

    foreach my $role (keys %{$config{$family}})
    {
    print "\t$role=$config{$family}->{$role}\n"
    }
    }






    Config1.pm :



    package Config1;
    require Exporter;

    our @ISA = qw(Exporter);
    our @EXPORT = '%config';


    our %config = (
    my_ftp => {
    domain => 'fakename',
    username => 'fakeuser',
    password => 'fakepass',
    },

    yoursky => {
    domain => 'http://www.fourmilab.ch/yoursky/',
    language => 'russian',
    word => 'north',
    },
    );
    George Mpouras, Oct 2, 2013
    #4
  5. Στις 3/10/2013 05:34, ο/η Cal Dershowitz έγÏαψε:
    > On 10/2/2013 2:46 PM, George Mpouras wrote:
    >
    >>
    >>
    >> Config1.pm :
    >>
    >>
    >>
    >> package Config1;
    >> require Exporter;
    >>
    >> our @ISA = qw(Exporter);
    >> our @EXPORT = '%config';

    >
    > Why would you single-quote @EXPORT and not use the qw syntax like @ISA?
    >
    > I read a lot of this today but didn't stumble yet on an explanation of
    > what @ISA is. (?)



    @array = qw/a b/;

    is the same as

    @array = 'a' , 'b';
    George Mpouras, Oct 3, 2013
    #5
  6. George Mpouras

    Dr.Ruud Guest

    On 03/10/2013 10:55, George Mpouras wrote:

    > @array = qw/a b/;
    >
    > is the same as
    >
    > @array = 'a' , 'b';


    perl -MData::Dumper -e'

    my @array = "a", "b";

    print Dumper( \@array );
    '
    $VAR1 = [
    'a'
    ];

    --
    Ruud
    Dr.Ruud, Oct 3, 2013
    #6
  7. George Mpouras

    $Bill Guest

    On 10/2/2013 19:34, Cal Dershowitz wrote:
    > On 10/2/2013 2:46 PM, George Mpouras wrote:
    >
    >>
    >>
    >> Config1.pm :
    >>
    >>
    >>
    >> package Config1;
    >> require Exporter;
    >>
    >> our @ISA = qw(Exporter);
    >> our @EXPORT = '%config';

    >
    > Why would you single-quote @EXPORT and not use the qw syntax like @ISA?


    6 of these - half dozen of the other - they're equivalent in this case.
    qw is just a space separated bunch of ''s (uninterpolated strings).

    > I read a lot of this today but didn't stumble yet on an explanation of what @ISA is. (?)


    If you combine all of the base Perl man pages into a single
    text file, you can easily search for something like @ISA.

    Stands for 'is a' - defines an inheritance tree for the package
    in question. See perlboot - OO tutorial.

    Email me a legit addy and I'll send you a script to combine
    your pods for you.

    [Note: Not all of my posts seem to make it through using these
    flaky free servers - let's hope this does.]
    $Bill, Oct 3, 2013
    #7
  8. Στις 3/10/2013 12:11, ο/η Dr.Ruud έγÏαψε:
    > On 03/10/2013 10:55, George Mpouras wrote:
    >
    >> @array = qw/a b/;
    >>
    >> is the same as
    >>
    >> @array = 'a' , 'b';

    >
    > perl -MData::Dumper -e'
    >
    > my @array = "a", "b";
    >
    > print Dumper( \@array );
    > '
    > $VAR1 = [
    > 'a'
    > ];
    >


    tippo !

    my @array = ( "a", "b" );
    George Mpouras, Oct 3, 2013
    #8
  9. George Mpouras

    Tim McDaniel Guest

    In article <>,
    Cal Dershowitz <> wrote:
    >On 10/2/2013 2:46 PM, George Mpouras wrote:
    >> our @EXPORT = '%config';

    >
    >Why would you single-quote @EXPORT and not use the qw syntax like @ISA?


    I can't speak for him. On large Perl source files at my orkplace,
    Emacs's perl-mode to get confused sometimes by qw, q, qq, and such --
    when you get to the closing delimiter it doesn't recognize the opening
    delimiter, so following indentation gets screwed up.

    --
    Tim McDaniel,
    Tim McDaniel, Oct 5, 2013
    #9
  10. George Mpouras wrote:
    > Στις 3/10/2013 05:34, ο/η Cal Dershowitz έγÏαψε:
    >> On 10/2/2013 2:46 PM, George Mpouras wrote:
    >>
    >>>
    >>>
    >>> Config1.pm :
    >>>
    >>>
    >>>
    >>> package Config1;
    >>> require Exporter;
    >>>
    >>> our @ISA = qw(Exporter);
    >>> our @EXPORT = '%config';

    >>
    >> Why would you single-quote @EXPORT and not use the qw syntax like @ISA?
    >>
    >> I read a lot of this today but didn't stumble yet on an explanation of
    >> what @ISA is. (?)

    >
    >
    > @array = qw/a b/;
    >
    > is the same as
    >
    > @array = 'a' , 'b';


    I think you mean:

    @array = 'a' .. 'b';


    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Oct 6, 2013
    #10
    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. rp
    Replies:
    1
    Views:
    498
    red floyd
    Nov 10, 2011
  2. JWhite

    Perl CGI utilities?

    JWhite, Jul 1, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    269
    Todd Wade
    Jul 5, 2008
  3. Ben Bacarisse

    Re: perl hash utilities

    Ben Bacarisse, Sep 26, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    166
    Ben Bacarisse
    Sep 26, 2013
  4. Uri Guttman

    Re: perl hash utilities

    Uri Guttman, Sep 26, 2013, in forum: Perl Misc
    Replies:
    2
    Views:
    169
    Charles DeRykus
    Sep 27, 2013
  5. Rainer Weikusat

    Re: perl hash utilities

    Rainer Weikusat, Oct 4, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    137
    Rainer Weikusat
    Oct 4, 2013
Loading...

Share This Page