slurp hash from file
I want to define a hash reference like this: my $h={ a = 'a', b = 'aa \ bbb', }; (Note the string containing escaped newlines.) Now, the point is that I have the block a = 'a', b = 'aa \ bbb', in a text file. It would be nice to be able to just slurp the file as-is to define $h. Is there any way to do this? The best workaround I can think of is to have a modified file, with entries separated by empty lines: a = 'a', b = 'aa \ bbb', I could then slurp the file as a single string, split the string on /\n\n/, etc., because I know the hash values will not contain empty lines. But this leaves an uncomfortable feeling, since the original file contents are already as we would write them in the main program... Any idea? -- Jorge Almeida -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: slurp hash from file
On 2/27/07, Jorge Almeida [EMAIL PROTECTED] wrote: I want to define a hash reference like this: my $h={ a = 'a', b = 'aa \ bbb', }; (Note the string containing escaped newlines.) Now, the point is that I have the block a = 'a', b = 'aa \ bbb', in a text file. It would be nice to be able to just slurp the file as-is to define $h. Is there any way to do this? The best workaround I can think of is to have a modified file, with entries separated by empty lines: a = 'a', b = 'aa \ bbb', I could then slurp the file as a single string, split the string on /\n\n/, etc., because I know the hash values will not contain empty lines. But this leaves an uncomfortable feeling, since the original file contents are already as we would write them in the main program... Any idea? Let's say your data is in the file data.pl. Then my %hash = do data.pl; my $hash_ref = \%hash; -- Jorge Almeida -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: slurp hash from file
On Tue, 27 Feb 2007, Adriano Ferreira wrote: On 2/27/07, Jorge Almeida [EMAIL PROTECTED] wrote: Let's say your data is in the file data.pl. Then my %hash = do data.pl; my $hash_ref = \%hash; Great! Thanks. -- Jorge Almeida -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/