Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Trying to streamline repetitive code....

by kyle (Abbot)
on Oct 29, 2009 at 03:26 UTC ( #803867=note: print w/ replies, xml ) Need Help??

in reply to Trying to streamline repetitive code....

That's quite a Wall of Text. I'm guessing this doesn't run under strict.

Anyway, I wrote this just for fun:

use strict; use warnings; use 5.010; my $m8 = m8_text(); eval "no strict;sub { $m8 }"; die $@ if $@; for ($m8) { s{(\@|\$\#)(?!M8PinContentSorted)(\w*M8\w+)}{${1}{\$M->{$2}}}g; s{\$(?!M8PinContentSorted)(\w*M8\w+)\[}{\${\$M->{$1}}\[}g; s{(?<!foreach )\$(?!M8PinContentSorted)(\w*M8\w+)}{\$M->{$1}}g; } eval "no strict;sub { $m8 }"; die $@ if $@; say $m8;

The m8_text() sub returns the text from your node verbatim. I do a few replacements on it, and I confirm before and after that it compiles with eval.

So you can paste the results into a sub like this:

sub node_803848 { my ( $M, $name ) = @_; # wall of text }

Call the sub like node_803848( $big_m{$name}, $name ) with $name set to "M8", for example.

At that point, you need %big_m to have a hash ref for each of your M1–M8, and each of those hashes has an entry for each *M8* variable in the original. There appear to be no hashes in there, so it's all scalars and arrays that you could initialize like so:

my %big_m; $big_m{M8}{M8TrackSummary} = []; # array ref $big_m{M8}{M8CurrentTrackLoc} = undef;

Of course, I haven't tried to run this, but if it works, the result could run under strict with a few more changes. Each use of foreach needs a my. There are places where "M8" appears in string literals, and that needs to be $name.

If I had this in front of me, I'd be strongly tempted to rewrite it all, but I don't have time for that here and now. Instead, I wrote the cheap and evil solution you see above. I don't actually recommend it, but it might get you started.

Good luck.

Comment on Re: Trying to streamline repetitive code....
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-08-29 17:08 GMT
Find Nodes?
    Voting Booth?

    The best computer themed movie is:

    Results (283 votes), past polls