http://www.perlmonks.org?node_id=1017523


in reply to Optimization Help on Perl Hash Traversal (eval use)

Adding to BrowserUK's suggestion, you could store your snippets into a YAML file and have them automatically eval-ed upon loading the YAML-file as part of your initialization.

Here is an example:

use Modern::Perl; use YAML qw/DumpFile LoadFile/; local $YAML::UseCode = 1; my $code = { snippet1 => sub { my $counter; for ( 1 .. 10 ) { say; $counter++ } say $counter; } }; DumpFile( './snippet.yml', $code ); my $snippets = LoadFile('./snippet.yml'); # <- Put this in the initial +ization part of your program &{ $snippets->{snippet1} }; # and run the snippet
Here is the YAML-file:
--- snippet1: !!perl/code | { use warnings; use strict; use feature 'say', 'state', 'switch', 'unicode_strings'; my $counter; foreach $_ (1 .. 10) { say $_; ++$counter; } say $counter; }
As you see that is very well readable and easily editable. Probably easier than adding the code to your database.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics