in reply to Output in variable

You can open a filehandle to a scalar ref like so: my $output; open(my $fh, ">", \$output) or die $!. You would then print to the filehandle instead of STDOUT. I may not fully understand the question though.

-Paul

Replies are listed 'Best First'.
Re^2: Output in variable
by Sombrerero_loco (Beadle) on Jan 22, 2009 at 11:50 UTC
    Hi. Im not shure what you really want to load into the variable, select or all the line after the text select? Try to change $_ for $1 (this its how perl saves the matched regex after the next iteration or line) But...explain better what you want to get in the output
      hi... actually i wanna save the whole output in a variable. so that i can use it later.
        maybe try this:
        
        $PatternToMatch="^SELECT";
        $tmpfile="pattern.txt";
        open(MYFILE, "$tmpfile")|| die "Cannot create $tmpfile\n";
        while (<MYFILE>) {
        if (/$PatternToMatch/)
        {
         push (@array, $_);
          
        }
        }
        
        
        As you are reading the file, you need to push every time perl founds Select to and array, because the variable get overwritten with every success match. After that, recover all the data from the array when you need.
        foreach $control (@array) {
           print "Line -> $control\n";
          }
        
        What, you mean like ...
        $PatternToMatch="^SELECT"; $tmpfile="pattern.txt"; $var = ''; open(MYFILE, "<$tmpfile")|| die "Cannot create $tmpfile\n"; while (<MYFILE>) { $var .= $_ if /$PatternToMatch/; } . . . . eval $var;
        ??

        A user level that continues to overstate my experience :-))
      Tanx Loco... its working :)
Re^2: Output in variable
by Anonymous Monk on Jan 22, 2009 at 11:57 UTC
    Right now i am printing the output directly by print "$_". but i wanna save this output in a variable. how can i do this ???
      Uhm, you already have the output in a variable. The variable is called $_. If you don't want to print it, then, well, perhaps you should consider not asking perl to print it.
        Just use concatenation if you want to capture all the lines.
        my $output; $PatternToMatch="^SELECT"; $tmpfile="pattern.txt"; open(MYFILE, "$tmpfile")|| die "Cannot create $tmpfile\n"; while (<MYFILE>) { if (/$PatternToMatch/) { #print "$_"; $output .=$_; } }