Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: perl hash utilities

Reply
Thread Tools

Re: perl hash utilities

 
 
George Mpouras
Guest
Posts: n/a
 
      09-26-2013
# hey this is easy !


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

my %domain = %{get_config()};

use Data:umper;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',
},
}

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-26-2013
>>>>> "GM" == George Mpouras <(E-Mail Removed)> 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:umper;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
 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      09-27-2013
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:umper'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.

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      10-02-2013
> 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',
},
);

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      10-03-2013
Στις 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';
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-03-2013
On 03/10/2013 10:55, George Mpouras wrote:

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


perl -MData:umper -e'

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

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

--
Ruud

 
Reply With Quote
 
$Bill
Guest
Posts: n/a
 
      10-03-2013
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.]

 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      10-03-2013
Στις 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:umper -e'
>
> my @array = "a", "b";
>
> print Dumper( \@array );
> '
> $VAR1 = [
> 'a'
> ];
>


tippo !

my @array = ( "a", "b" );
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      10-05-2013
In article <(E-Mail Removed)> ,
Cal Dershowitz <(E-Mail Removed)> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-06-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: perl hash utilities Uri Guttman Perl Misc 2 09-27-2013 09:06 AM
Re: perl hash utilities Ben Bacarisse Perl Misc 1 09-26-2013 06:27 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Perl CGI utilities? JWhite Perl Misc 5 07-05-2008 03:39 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM



Advertisments