Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Trying to streamline repetitive code.... by kyle
in thread Trying to streamline repetitive code.... by fiddler42

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found