See below
> Hi All,
>
>
> Can you please let me know the following snippset? why it is used for?
>
>
> select( STDERR );
> $| = 1;
> select( STDOUT );
> $| = 1;
> print STDERR "\nThis is india\n\n";
> print STDERR "Usage: This is build";
> print STDERR "where: base PL label\n";
>
> and second question
> ****************
> second question:- I want to open a file read+write mode and change
> the
> some content in same file without creating another file and copy to it.
>
> MY SCRIPT(Not working as i wish to )
> ********************
> #!/usr/bin/perl
>
> open(FILE,">test.txt") or die "Could not open the file: $!";
>
> @file=<FILE>;
>
> foreach (@file)
> {
> $_=~s/BLR/bangalore/g;
>
> print "$_";
> }
> close(FILE);
>
> Regards,
> Jitendra
>
Jitendra,
Please see the comments in the code below. Hopefully they will answer
your questions for you
Nathan
#!/usr/bin/perl
use strict;
use warnings;
select( STDERR ); # Makes STDERR the default output stream
$| = 1; # Turns off buffering for SELECTED stream,
# (redundant here because STDERR is not
buffered)
select( STDOUT ); # Makes STDOUT the default output stream
$| = 1; # Turns off buffering for SELECTED stream
print STDERR "\nThis is india\n\n"; # Prints three lines
print STDERR "Usage: This is build"; # to STDERR
print STDERR "where: base PL label\n";
#
# To update your file, you might try this:
#
open my $file, # Using an indirect filehandle
"+<", # Open for read/write
"test.txt" or die "Error opening test.txt read/write: $!";
my @lines = <$file>; # Read file contents into the @lines array
foreach my $line (@lines) { # Using a named variable instead of $_
$line =~ s/BLR/bangalore/g;
print $line; # This will echo each line to STDOUT
# remove the line above if you just #
want to update the file test.txt
}
seek $file, 0, 0; # The "magic": This resets the position of
# the $file filehandle back to the beginning #
of the file. See "perldoc -f seek" for more #
options.
print $file @lines; # Write the modified lines out to test.txt
close $file; # Not strictly necessary; the file will be closed
# "automagically" when the variable $file goes
# out of scope at the end of the script, but #
nice to do anyway
--
--
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/