in reply to Re: splitting files into multiple sections to be processed as a file
in thread splitting files into multiple sections to be processed as a file

I guess I mean where subcontents of a file are held in memory and then are processed as if they were infact files.
  • Comment on Re^2: splitting files into multiple sections to be processed as a file

Replies are listed 'Best First'.
Re^3: splitting files into multiple sections to be processed as a file
by CountZero (Bishop) on Nov 29, 2011 at 23:08 UTC
    You can save the subcontents in an array, that is easy.
    use Modern::Perl; use Data::Dump qw/dump/; my @subsections; { local $/ = "\n\n"; @subsections = <DATA> } say dump(@subsections); __DATA__ subsection 1 line 1 subsection 1 line 2 subsection 1 line 3 subsection 1 line 4 subsection 2 line 1 subsection 2 line 2 subsection 2 line 3 subsection 3 line 1 subsection 3 line 2 subsection 3 line 3 subsection 4 line 1 subsection 4 line 2
    But what file-specific operations do you want to perform on the subcontents?

    Update: You can use a scalar reference as a "virtual" (or in-memory) file (since Perl v5.8.0.)

    open my $fh, '<', \$subsections[1]; print while <$fh>;

    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: splitting files into multiple sections to be processed as a file
by ww (Archbishop) on Nov 29, 2011 at 23:17 UTC

    You really need to clarify, since the answer to any plausible meaning I can infer from your question is Yes!" All that's required is that you apply yourself to specifying the steps by which you'd do this if the file were a book; searching this site (and others) for keywords growing out of that exercise (restricting the search to Perl) and learning how to tell Perl to do what you want.

    So please, expand and be explicit: does "subcontents" equal the various fragments (elements of an array) created by splitting on blank lines? If so, how do those elements, "held in memory" fail to satisfy your requirements? What processing do you have in mind that has to occur "as if they were infactfiles?" What is the desired outcome? What's the big picture on your (woefully inadequate, to date) specs?

    And what research (asking the question in the CB and repeating it as a SOPW question doesn't count) have you done to find your own solution.