Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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


Comment on Re: Optimization Help on Perl Hash Traversal (eval use)
Select or Download Code

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 romping around the Monastery: (14)
As of 2014-07-31 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls