Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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.


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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017523]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2018-05-21 23:06 GMT
Find Nodes?
    Voting Booth?