| 1 | #!/usr/bin/perl | ||||||||||
| 2 | #------------------------------------------------------------------------------- | ||||||||||
| 3 | # Set a switch in your script to zero after a run with the switch set to one. | ||||||||||
| 4 | # Philip R Brenan at gmail dot com, Appa Apps Ltd Inc, 2016-2017 | ||||||||||
| 5 | #------------------------------------------------------------------------------- | ||||||||||
| 6 | |||||||||||
| 7 | package Flip::Flop; | ||||||||||
| 8 | 1 | our $VERSION = 20181005; | |||||||||
| 9 | 1 | ▶ | use v5.8.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use warnings FATAL => qw(all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Data::Table::Text qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 1 | my @flipflops; # Flip::Flops encountered | |||||||||
| 16 | 1 | my $startProcess = $$; # Starting process | |||||||||
| 17 | |||||||||||
| 18 | sub AUTOLOAD # Any method will do | ||||||||||
| 19 | 2 | ▶ | {push @flipflops, $Flip::Flop::AUTOLOAD unless @_; # No parameters: flop switch, with parameters: flip switch to $[0] | ||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 20 | 2 | $_[0] | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | END | ||||||||||
| 24 | 1 | ▶ | {if ($startProcess eq $$) # Reset the flip flops once in the starting process | ||||||||
|
Subroutine
END: called CC=6 SCAR=18.7
Branch: 1 branch
| |||||||||||
| 25 | 1 | ▶ | {unless($?) # Clean run? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | {my $S = my $s = readFile($0); # Read source | |||||||||
| 27 | 1 | for my $program(@flipflops) # Each flip flop | |||||||||
| 28 | 1 | {my $f = "$program(0)"; # Regular expression to set the switch to zero | |||||||||
| 29 | 1 | ▶ | if ($s !~ m/$f/s) # Reset the switch if it is not already zero | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 1 | {my $F = "$program\\(\\d+\\)"; # Regular expression to find the switches | |||||||||
| 31 | 1 | $s =~ s($F)($f)gs; # Reset switch | |||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | 1 | ▶ | overWriteFile($0, $s) if $s ne $S; # Update source file if any switches were reset | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | } | ||||||||||
| 36 | } | ||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | # podDocumentation | ||||||||||
| 40 | |||||||||||
| 41 | =pod | ||||||||||
| 42 | |||||||||||
| 43 | =encoding utf-8 | ||||||||||
| 44 | |||||||||||
| 45 | =head1 Name | ||||||||||
| 46 | |||||||||||
| 47 | Flip::Flop - Set a switch in your script to zero after a run with the switch | ||||||||||
| 48 | set to one. | ||||||||||
| 49 | |||||||||||
| 50 | =head1 Synopsis | ||||||||||
| 51 | |||||||||||
| 52 | Some where near the top of your program you might have some variables | ||||||||||
| 53 | (illustrated below by B<uploadToCloud>) that select the actions the code is to | ||||||||||
| 54 | perform on the next run from your IDE: | ||||||||||
| 55 | |||||||||||
| 56 | my $doUpload = Flip::Flop::uploadToCloud(1); | ||||||||||
| 57 | |||||||||||
| 58 | ... | ||||||||||
| 59 | |||||||||||
| 60 | if ($doUpload) | ||||||||||
| 61 | {... | ||||||||||
| 62 | Flip::Flop::uploadToCloud(); | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | If the upload succeeds, your program source code will be modified to read: | ||||||||||
| 66 | |||||||||||
| 67 | my $doUpload = Flip::Flop::uploadToCloud(0); | ||||||||||
| 68 | |||||||||||
| 69 | so that the next time you run your program from your IDE this lengthy operation | ||||||||||
| 70 | will not be performed unless you explicitly re-request it. | ||||||||||
| 71 | |||||||||||
| 72 | If the run does not succeed the switch will be left unchanged. The switch will | ||||||||||
| 73 | only be reset if your program requests the reset and exits explicitly or | ||||||||||
| 74 | implicitly with exit(0). | ||||||||||
| 75 | |||||||||||
| 76 | You can have as many such switches as desired. | ||||||||||
| 77 | |||||||||||
| 78 | If your program L<forks|/fork>, then only the process in which Perl was started | ||||||||||
| 79 | will update the Flip::Flop switches. | ||||||||||
| 80 | |||||||||||
| 81 | This capability will only be useful to you if you are using an editor that | ||||||||||
| 82 | detects changes made independently to the file currently being edited. If you | ||||||||||
| 83 | do use such an editor this technique is surprisingly useful for simplifying, | ||||||||||
| 84 | standardizing, streamlining and supporting your edit/run cycle. | ||||||||||
| 85 | |||||||||||
| 86 | =head1 Installation | ||||||||||
| 87 | |||||||||||
| 88 | This module is written in 100% Pure Perl and, thus, it is easy to read, use, | ||||||||||
| 89 | modify and install. | ||||||||||
| 90 | |||||||||||
| 91 | Standard Module::Build process for building and installing modules: | ||||||||||
| 92 | |||||||||||
| 93 | sudo cpan install Flip::Flop | ||||||||||
| 94 | |||||||||||
| 95 | =head1 Author | ||||||||||
| 96 | |||||||||||
| 97 | L<philiprbrenan\@gmail.com|mailto:philiprbrenan\@gmail.com> | ||||||||||
| 98 | |||||||||||
| 99 | L<http://www.appaapps.com|http://www.appaapps.com> | ||||||||||
| 100 | |||||||||||
| 101 | =head1 Copyright | ||||||||||
| 102 | |||||||||||
| 103 | Copyright (c) 2016-2018 Philip R Brenan. | ||||||||||
| 104 | |||||||||||
| 105 | This module is free software. It may be used, redistributed and/or modified | ||||||||||
| 106 | under the same terms as Perl itself. | ||||||||||
| 107 | |||||||||||
| 108 | END | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | 1 | 1; | |||||||||
| 113 | # podDocumentation | ||||||||||