Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Best way to keep script with generated output

by benizi (Hermit)
on Dec 06, 2009 at 20:47 UTC ( #811370=perlquestion: print w/replies, xml ) Need Help??

benizi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, long time no see. I've been off in the desert of .NET, but daily perl from the commandline still sustained me.

Long ago I got into the habit of keeping my iteratively-developed Perl scripts as comments in the files that they generate. For example, I hate that the default X11 Compose maps force a specific order for adding accents (letter + accent), whereas I often find myself wanting the opposite (accent + letter). That way I can type either <Multi_Key> <e> <'> or <Multi_Key> <'> <e> to get . So, now my ~/.XCompose-ordering contains the following comment: (In the file it's on one line, here split for slight readability)

# perl -lanwe ' BEGIN { push @{$o{0+@$_}}, $_ for map [split//], qw/021 0132 0213 0231 + 0312 0321/; } next unless /LATIN.*LETTER...WITH/; next if /<dead/; my @keys = @F[0..(-1+(grep $F[$_] eq ":", 0..$#F)[0])]; $K=join"-",sort @keys; next if $seen{$K}; $seen{$K}++; next if $K eq "<Multi_key>-<U>-<o>"; next if /LETTER \S+ WITH \S+ AND \S+/; print "@keys[@$_] @F[@keys..$#F]" for @{$o{0+@keys}} ' /usr/share/X11/locale/${LANG}/Compose

I was wondering if any monks had clever ways to execute this code to regenerate the file while keeping the code itself as a comment in the file (using perl -x for example). Currently, I've been using the following Zsh snippet:

file=~/.XCompose-ordering line=$(head -n 1 $file) (print -lr $line ; eval ${line[3,-1]}) > $file

But it'd be much cooler to just do perl -x ~/.XCompose-ordering or even ~/.XCompose-ordering.

Replies are listed 'Best First'.
Re: Best way to keep script with generated output
by juster (Friar) on Dec 07, 2009 at 06:56 UTC

    This is a cool idea! I'm not sure if my tactic is much better but I thought of using a source filter:

    package CFGPL; use warnings; use strict; our $Comment_Start = '#'; sub import { shift; $Comment_Start = quotemeta shift if @_; # Read in only our commented code... open my $own_src_file, '<', $0; my @perl_code = grep { /^$Comment_Start/ } <$own_src_file>; close $own_src_file; die "Config file has no code in comments\n" unless @perl_code; # Write it back to config file and erase uncommented stuff! open STDOUT, '>', $0; print @perl_code; } # Filter out the leading comment and runs the code... require Filter::Simple; Filter::Simple::FILTER( sub { s/^$Comment_Start//gm; } ); 1;
    Easy to invoke like: perl -MCFGPL config.file.
Re: Best way to keep script with generated output
by ikegami (Patriarch) on Dec 07, 2009 at 07:39 UTC
    What's the format of .XCompose-ordering? Is it a sh script?

      .XCompose-ordering has the same format as .XCompose. (I include the former from the latter.) It consists of lines of usually the following format:

      <XKeySym>(?: <XKeySym>)* : (?:"string"|U[\da-fA-F]{4})(?: description)?

      String is usually a single UTF-8 char, but is a C-style string (\n \r \\, etc.). And Uxxxx represents a single Unicode char. The format also supports '#'-comments, which last to the end of the line, ala perl. And it supports 'include' lines:

      include "other file"

      Full specs

        Can you include a literal newline inside the double quotes? That would allow you to use perl -x:
        <not> <gonna> <happen> : " #!perl . . This is the script . __END__ " . . This is the actual file .

        So what would the script look like? How about

        local $^I = '~'; local @ARGV = __FILE__; my $seen_data; while (<>) { if (!$seen_data) { $seen_data = $_ eq qq{__END__\n}; next; } ... change $_ as desired ... } continue { print; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://811370]
Approved by GrandFather
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2023-12-07 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (32 votes). Check out past polls.

    Notices?