scalar value to be printed on the file using perl (last modified value)?
open (OUT,">new.txt");
my $var = "test-1";
print OUT "$var";
$var = "test-2";
print OUT "$var";
$var = "test-3";
print OUT "$var";
close开发者_开发问答(OUT);
Output gives:
test-1
test-2
test-3
I am tring to get the Output as:
test-3
test-3
test-3
- to make the scalar value printed on the file after final assignment/modification in perl script for that file handle (OUT).
Please guide me if it has any way to perform like this....
Thanks in Advance to all
Here's a way to do it.
use strict;
use warnings;
use autodie;
my @print;
my $var = "test-1";
push @print, \$var;
$var = "test-2";
push @print, \$var;
$var = "test-3";
push @print, \$var;
open my $out, '>', "new.txt";
print $out map { $$_ } @print;
By using a reference to the variable instead of the value of the variable, and storing the printed lines until the end, you will do the prints with the last value stored in the variable.
精彩评论