Hi, just a quick thought.

I often end up processing files of arbitrary data in a variety of formats to extract some part or parts that is useful. Usually these snippets are just a little too long to write as one liners, but not long enough that I want them to live in their own file. Given that they are usually tailored to a specific file of data, (which i usually need to keep, perhaps for reprocessing) my preference is to actually turn these files into a little script by using the __DATA__ tag, as Im sure many of you out there do as well. eg

#mindnumbingly simple example.... while (<DATA>) { my ($x,$y,$z)=split(/,/,4); #do something weird with $x,$y,$z } __DATA__ blah,blah,blah
Now what I wish is that there was some switch like -n that worked the same way, but instead of pulling from STDIN (or filenames passed 1 at a time) it pulled from __DATA__

Sigh. Well, thats one item for my perl christmas wish list...

:-)

Yves / DeMerphq
--
This space for rent.

Replies are listed 'Best First'.
Re: A little Christmas switch wish..
by chromatic (Archbishop) on Dec 12, 2001 at 01:46 UTC
    Try this patch, which may only apply cleanly to a recent bleadperl (I'm at patchlevel 13599). Don't forget to run embed.pl after you do. The new -N switch adds this loop around your script:
    LINE: while (<DATA>) { }
    It should doesn't break any of the regression tests. It may break binary compatibility. Use at your own risk. Try this demonstration:
    #!/usr/bin/perl -wNp use strict; $_ = reverse $_; __DATA__ citamorhc si !sdrawkcab
    Merry Christmas!
    --- ~intrpvar.h Tue Dec 11 13:20:28 2001 +++ intrpvar.h Tue Dec 11 13:20:41 2001 @@ -27,6 +27,7 @@ PERLVARI(Isplitstr, char *, " ") PERLVAR(Ipreprocess, bool) PERLVAR(Iminus_n, bool) +PERLVAR(Iminus_N, bool) PERLVAR(Iminus_p, bool) PERLVAR(Iminus_l, bool) PERLVAR(Iminus_a, bool) --- ~perl.c Tue Dec 11 13:13:15 2001 +++ perl.c Wed Dec 12 11:16:14 2001 @@ -1087,6 +1087,7 @@ case 'M': case 'm': case 'n': + case 'N': case 'p': case 's': case 'u': @@ -2135,6 +2136,7 @@ "-l[octal] enable line ending processing, specifies line termin +ator", "-[mM][-]module execute `use/no module...' before executing program" +, "-n assume 'while (<>) { ... }' loop around program", +"-N assume 'while (<DATA>) { ... }' loop around program" +, "-p assume loop like -n but print line also, like sed", "-P run program through C preprocessor before compilatio +n", "-s enable rudimentary parsing for switches after progra +mfile", @@ -2361,6 +2363,11 @@ Perl_croak(aTHX_ "No space allowed after -%c", *(s-1)); return s; case 'n': + PL_minus_n = TRUE; + s++; + return s; + case 'N': + PL_minus_N = TRUE; PL_minus_n = TRUE; s++; return s; --- ~sv.c Tue Dec 11 13:19:16 2001 +++ sv.c Tue Dec 11 13:19:33 2001 @@ -9825,6 +9825,7 @@ PL_splitstr = proto_perl->Isplitstr; PL_preprocess = proto_perl->Ipreprocess; PL_minus_n = proto_perl->Iminus_n; + PL_minus_N = proto_perl->Iminus_N; PL_minus_p = proto_perl->Iminus_p; PL_minus_l = proto_perl->Iminus_l; PL_minus_a = proto_perl->Iminus_a; --- ~toke.c Tue Dec 11 13:14:33 2001 +++ toke.c Tue Dec 11 13:32:02 2001 @@ -2435,7 +2435,11 @@ PL_preambleav = NULL; } if (PL_minus_n || PL_minus_p) { - sv_catpv(PL_linestr, "LINE: while (<>) {"); + if (PL_minus_N) { + sv_catpv(PL_linestr, "LINE: while (<DATA>) {"); + } else { + sv_catpv(PL_linestr, "LINE: while (<>) {"); + } if (PL_minus_l) sv_catpv(PL_linestr,"chomp;"); if (PL_minus_a) {
    Update: (Wed Dec 12 18:20:00 UTC 2001) I missed a couple of spots in perl.c. They've been added to the patch above. They're the first two chunks in that file, and now it works from the command line.
      Thanks dude!

      I successfully added your patch (by hand :( ) to ActiveState Build 630.

      Cool!

      Although one question would be why it works with your snippet but not as just simply

      perl -N
      Anyways, that was big time cool, thanks.

      Yves / DeMerphq
      --
      This space for rent.

Re: A little Christmas switch wish..
by dragonchild (Archbishop) on Dec 12, 2001 at 01:13 UTC
    Couldn't you just pipe the file in? Maybe something like:
    % script-i-doo.pl < datafile

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      No cause then I have snippets lying around with no clear connection to the data they process.

      Like I said though its a wish...

      :-)

      Yves / DeMerphq
      --
      This space for rent.

        That's what
        • Intelligent filenaming
        • Intelligent directory structures
        • Documentation
        is for. :-)

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: A little Christmas switch wish..
by chromatic (Archbishop) on Dec 13, 2001 at 03:42 UTC
    Having thought about this a little more, would a source filter work? It's a little more fragile than the -N switch, doesn't play well with -p, but requires only the Filter::Simple module. That'll be standard in 5.8, unlike my patch. Probably. :)
    package NFlag; use Filter::Simple sub { s/^([^#].+)/LINE: while (<DATA>) {\n$1\n}\n/ms; }; 'to demerphq';
    Also save this code as 'stdinhack.pl':
    #!/usr/bin/perl -w use strict; print scalar reverse $_; __DATA__ sdrawkcab niaga tub retrohs
    Then you can call it with the command line perl -MNFlag stdinhack.pl.