Phil
Take a look at perldoc -f open for a complete answer to this question. This will do what you want:
open(my $handle,">>","sample.txt") or die("Cannot write to file: $!\n");Note that this uses the three-argument version of open and uses a lexical variable as a filehandle, both are recommended best practices and you should stick to them (see the perldoc for why).
And just to be different, try this:
#!/usr/bin/perl -wT use strict; use Tie::File; my @ry; tie @ry,"Tie::File","myfile.txt" or die $!; push @ry,"stuff"; untie @ry; exit(0);
In Perl there is always more than one solution to a problem.... and nobody expects the Perl Inquisition!
update: fixed minor typo pointed out by [Happy-the-monk]
You have to use operator ">>" while opening. If you want to open in R+W mode you just put the "+" operator infront as "+>> $fileName". You better go through the help pages of "open" function.
You also take a look at the "seek" function, that will help you to handle the file pointers effectively.
perlmonks.org content © perlmonks.org and blue_cowdawg, devlele, philcrow, tirwhan, vennirajan
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03