Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Optimization Help on Perl Hash Traversal (eval use)

by CountZero (Bishop)
on Feb 06, 2013 at 22:48 UTC ( #1017523=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017523]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2016-10-01 17:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (3 votes). Check out past polls.