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

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

Hi, I have trouble extracting paragraph from multi line string
# the UNIX tool output is a multi line text my $var = `unix_tool file_name`; open(FH,'<', \$var) or die "ERROR\n"; $/ = ""; #This is just for example while (<FH>){ print "------------------------------------------\n"; print "$_"; print "\n"; }
Nothing gets printed. If I don’t use:
$/ = "";
All the lines get printed. And one more thing, If I first save the UNIX tool output file and open it using file handler the code read every paragraph.

Replies are listed 'Best First'.
Re: Extract paragraph from multi line string
by kennethk (Abbot) on Jan 26, 2010 at 15:37 UTC
    Once I swap my $var = `unix_tool file_name`; to my $var = `cat script.pl`;, I get the expected output. What is unix_tool?

    You should be able to split this into two steps (reading in from the backticks followed by the angle-bracket read). What happens when you copy the tool output into a DATA block and use that for your read, i.e.:

    #!/usr/bin/perl use strict; use warnings; $/ = ""; #This is just for example while (<DATA>){ print "------------------------------------------\n"; print "$_"; print "\n"; } __DATA__ This is the output From the tool.

    Assuming this fails, post the modified snippet (scrubbed if necessary) and we can work from there.

Re: Extract paragraph from multi line string
by AnomalousMonk (Archbishop) on Jan 26, 2010 at 16:24 UTC

    One 'problem' with reading input in 'paragrep' mode (i.e.,  $/ = "") is that a seemingly blank line that has any other whitespace in it (e.g.,  "\n \n") is not recognized as being a repeated newline. A repeated newline, and therefor a paragraph break, exactly matches  /\n\n+/ only.

    BTW: I get the same printed output from  `cat file` as kennethk.

Re: Extract paragraph from multi line string
by jwkrahn (Abbot) on Jan 26, 2010 at 15:58 UTC
    ( my $var = `unix_tool file_name` ) =~ s/(.+?)\n{2,}/----------------- +-------------------------\n$1\n/sg; print $var;
Re: Extract paragraph from multi line string
by ikegami (Patriarch) on Jan 26, 2010 at 16:15 UTC
    If it prints nothing, you got nothing. Check for errors.
Re: Extract paragraph from multi line string
by Krambambuli (Curate) on Jan 26, 2010 at 17:45 UTC
    Rephrasing what ikegami said already:

    the output of `unix_tool file_name` is probably _empty_.

    What if you run this very command in the shell?
    What if you use the full path to unix_tool inside the backticks, if unix_tool isn't in the current directory?

    Maybe these questions can help you in tracking the problem down.

    Update: what if you dump out $var after filling it, like

    use Data::Dumper;
    print Dumper( $var );

    Altough this sounds improbable, maybe you just falled over some bug, maybe one that appears only within your environment: what Perl do you use, on what OS?


    Krambambuli
    ---