How to create an empty file? (second attempt)

B

bill

[ My first post on this this question contained a typo that
made the code included in that post inoperative. Please disregard
it. ]

What's the best way to create an empty file? I mean is there
something less verbose and circuitous than

{
my $inane;
open $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

and that does not use system(), as in

system('touch', $empty);

?

Thanks!

bill
 
N

nobull

bill said:
[ My first post on this this question contained a typo that
made the code included in that post inoperative. Please disregard
it. ]

Please do not start a new thread to correct an existing post.

Follow-up.
What's the best way to create an empty file? I mean is there
something less verbose and circuitous than

{
my $inane;
open $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

Well since you've not written anything after the open() and before the
close() it's probably OK not to check the return value of close() so
you can use an implicit close.

{
open my $inane, '>', $empty
or die "Failed to create $empty: $!\n";
}
 
A

A. Sinan Unur

bill said:
[ My first post on this this question contained a typo that
made the code included in that post inoperative. Please disregard
it. ]

Please do not start a new thread to correct an existing post.

Follow-up.
Ditto.
What's the best way to create an empty file? I mean is there
something less verbose and circuitous than

{
my $inane;
open $inane, '>', $empty and close $inane
or die "Failed to create $empty: $!\n";
}

Well since you've not written anything after the open() and before the
close() it's probably OK not to check the return value of close() so
you can use an implicit close.

{
open my $inane, '>', $empty
or die "Failed to create $empty: $!\n";
}

I still question the OP's motive for searching for the shortest way of
doing this. If it is because there are many places in his code where
this type of a block appears, then he is better off moving the code to a
subroutine that is named explicitly after its purpose.

If, on the other hand, the code is seldom used, what is the point of
saving a few keystrokes at the expense of correctness. The close can
fail for various reasons. Without knowing the purpose of this code, it
is hard to assert that the possibility of failure should be ignored.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top