http://www.perlmonks.org?node_id=715135


in reply to Re: Reading "slices" out of a file at known start markers
in thread Reading "slices" out of a file at known start markers

This is an interesting solution, however... I might have some use cases where this will fail, for example if SOURCES = ... happens to be the very last block in the file (i.e. there IS no next marker, other than EOF).

Or, if the next marker is also one I might want to grab in a different pass, but I don't know the order of them to know what order to put the /BEGIN/ and /END/ range markers in.

I'll hit this with a few broad examples and see if it works when I intentionally try to break it with different constructs.

Replies are listed 'Best First'.
Re^3: Reading "slices" out of a file at known start markers
by CountZero (Bishop) on Oct 03, 2008 at 07:24 UTC
    I might have some use cases where this will fail, for example if SOURCES = ... happens to be the very last block in the file (i.e. there IS no next marker, other than EOF).
    The range operator will still work as expected since this operator returns TRUE once the first test succeeds until the second test succeeds, which by definition will never happen as there will be no next marker. Hence it will print from the first marker until EOF.

    For multiple blocks, it will be easiest to rewind the file and start again with new begin and end markers. Unless your MAKEFILE is huge that would not cost you a lot of time and will keep the logic of your program easy to understand and maintain.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^3: Reading "slices" out of a file at known start markers
by hacker (Priest) on Oct 03, 2008 at 05:54 UTC
    Responding to my own question: I think I may have figured out an easier way:
    use strict; use warnings; use Data::Dump qw(dump ddx);; use Config::General; my $conf = new Config::General("Makefile"); my %config = $conf->getall; print dump(\%config);

    This puts each item as key/value pairs in the %config hash, which I can them print out and manipulate as $config{'SOURCES'} for example.

    I guess the only thing I end up losing here, is the basic original formatting, but I can probably iterate through the hash and reconstruct that back somehow.

    Anyone familiar with the innards of Config::General that can lend a hand so I can use it correctly?