Prev: FAQ 1.6 What is Perl 6?
Next: FAQ 2.11 Perl Books
From: Justin C on 11 Aug 2010 04:17 On 2010-08-10, PerlFAQ Server <brian(a)theperlreview.com> wrote: [snip] The code example in the original is over 80 columns wide and could be easier to follow - especially in ye olde slrn in an 80 column terminal. I've reformatted it, perhaps $FAQ_maintainer may consider the change? > Anyway, this is what you can do if you can't help yourself. > > use Fcntl qw(:DEFAULT :flock); > sysopen my $fh, "numfile", O_RDWR|O_CREAT > or die "can't open numfile: $!"; > flock $fh, LOCK_EX > or die "can't flock numfile: $!"; > my $num = <$fh> || 0; > seek $fh, 0, 0 > or die "can't rewind numfile: $!"; > truncate $fh, 0 > or die "can't truncate numfile: $!"; > (print $fh $num+1, "\n") > or die "can't write numfile: $!"; > close $fh > or die "can't close numfile: $!"; Justin. -- Justin C, by the sea.
From: Ben Morrow on 11 Aug 2010 06:41 Quoth Justin C <justin.1007(a)purestblue.com>: > On 2010-08-10, PerlFAQ Server <brian(a)theperlreview.com> wrote: > > The code example in the original is over 80 columns wide and could be > easier to follow - especially in ye olde slrn in an 80 column terminal. Ooh, yes, that's nasty. I rather suspect someone's gone trawling through the docs adding 'or die' everywhere without thinking too hard about the formatting. > I've reformatted it, perhaps $FAQ_maintainer may consider the change? > > > Anyway, this is what you can do if you can't help yourself. > > > > use Fcntl qw(:DEFAULT :flock); > > sysopen my $fh, "numfile", O_RDWR|O_CREAT > > or die "can't open numfile: $!"; > > flock $fh, LOCK_EX > > or die "can't flock numfile: $!"; > > my $num = <$fh> || 0; > > seek $fh, 0, 0 > > or die "can't rewind numfile: $!"; > > truncate $fh, 0 > > or die "can't truncate numfile: $!"; > > (print $fh $num+1, "\n") > > or die "can't write numfile: $!"; > > close $fh > > or die "can't close numfile: $!"; Since autodie has been core since 5.10, this would seem like a perfect place to use it. I don't know what the FAQ's policy is on things like that (myself, I'm starting to seriously consider adding 'autodie' to 'warnings' and 'strict' as something I always have on). Ben
From: David Canzi on 12 Aug 2010 15:42 In article <8bbaj7-89u1.ln1(a)osiris.mauzo.dyndns.org>, Ben Morrow <ben(a)morrow.me.uk> wrote: > >Quoth Justin C <justin.1007(a)purestblue.com>: >> On 2010-08-10, PerlFAQ Server <brian(a)theperlreview.com> wrote: >> >> The code example in the original is over 80 columns wide and could be >> easier to follow - especially in ye olde slrn in an 80 column terminal. > >Ooh, yes, that's nasty. I rather suspect someone's gone trawling through >the docs adding 'or die' everywhere without thinking too hard about the >formatting. > >> I've reformatted it, perhaps $FAQ_maintainer may consider the change? >> >> > Anyway, this is what you can do if you can't help yourself. >> > >> > use Fcntl qw(:DEFAULT :flock); >> > sysopen my $fh, "numfile", O_RDWR|O_CREAT >> > or die "can't open numfile: $!"; >> > flock $fh, LOCK_EX >> > or die "can't flock numfile: $!"; >> > my $num = <$fh> || 0; >> > seek $fh, 0, 0 >> > or die "can't rewind numfile: $!"; >> > truncate $fh, 0 >> > or die "can't truncate numfile: $!"; >> > (print $fh $num+1, "\n") >> > or die "can't write numfile: $!"; >> > close $fh >> > or die "can't close numfile: $!"; > >Since autodie has been core since 5.10, this would seem like a perfect >place to use it. I don't know what the FAQ's policy is on things like >that (myself, I'm starting to seriously consider adding 'autodie' to >'warnings' and 'strict' as something I always have on). Advice and example code in the FAQs should be useful to typical perl users, who run the version of perl that came with their operating system. A quick scan of about 100 machines in my work environment didn't find any running perl 5.10. -- David Canzi | "If the human mind were simple enough for us to understand, | we would be too simple-minded to understand it." -- anonymous
|
Pages: 1 Prev: FAQ 1.6 What is Perl 6? Next: FAQ 2.11 Perl Books |