Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to create an empty file?

Reply
Thread Tools

How to create an empty file?

 
 
bill
Guest
Posts: n/a
 
      04-10-2005



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

{
my $inane;
open my $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

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-10-2005
bill <(E-Mail Removed)> wrote in news:d3a48u$o1g$1
@reader1.panix.com:

> What's the best way to create an empty file? I mean is there
> something less circuitous than
>
> {
> my $inane;
> open my $inane, '>', $empty and close $inane
> or die "Failed to create $empty: $!\n";
> }


D:\Home> perl -c t.pl
"my" variable $inane masks earlier declaration in same scope at t.pl
line 8.

Further, if we get rid of that:

D:\Home> cat t.pl
#! /usr/bin/perl

use strict;
use warnings;

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

__END__

D:\Home> t
Global symbol "$inane" requires explicit package name at D:\Home\asu1
\UseNet\clp
misc\t.pl line 7.
Execution of D:\Home\asu1\UseNet\clpmisc\t.pl aborted due to compilation
errors.

This should have given you a clue that this is not such a great idea.

I am not sure what problem you are trying to solve, but if you find
yourself doing this frequently you probably want to put the correct code
in a sub:

#! /usr/bin/perl

use strict;
use warnings;

my $r = create_empty_file('inane');
die $r if $r;


sub create_empty_file {
eval {
open my $fh, '>', $_[0]
or die "Cannot create $_[0]: $!\n";
close $fh or die "Cannot close $_[0]: $!\n";
};
return $@;
}

__END__



--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      04-10-2005
bill <(E-Mail Removed)> wrote:

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


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


Not really, although you could chop the above down by a line:

{
open TMPFILE, '>', $empty and close TMPFILE
or die "File error with $empty: $!";
}

Axel
 
Reply With Quote
 
ko
Guest
Posts: n/a
 
      04-10-2005
bill wrote:
> What's the best way to create an empty file? I mean is there
> something less circuitous than
>
> {
> my $inane;
> open my $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
>


perl -MExtUtils::Command -e touch nonexistent_file

HTH -keith
 
Reply With Quote
 
bill
Guest
Posts: n/a
 
      04-10-2005
In <d3ao8p$68g$(E-Mail Removed)> ko <(E-Mail Removed)> writes:

>bill wrote:
>> What's the best way to create an empty file? I mean is there
>> something less circuitous than
>>
>> {
>> my $inane;
>> open my $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
>>


>perl -MExtUtils::Command -e touch nonexistent_file


Way cool. Thanks!

bill

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-10-2005
bill <(E-Mail Removed)> wrote in
news:d3bs78$skh$(E-Mail Removed):

> In <d3ao8p$68g$(E-Mail Removed)> ko <(E-Mail Removed)>
> writes:
>
>>bill wrote:
>>> What's the best way to create an empty file? I mean is there
>>> something less circuitous than
>>>
>>> {
>>> my $inane;
>>> open my $inane, '>', $empty and close $inane
>>> or die "Failed to create $empty: $!\n";
>>> }
>>>

....

>>perl -MExtUtils::Command -e touch nonexistent_file

>
> Way cool. Thanks!


Of course, you should be careful using it because all the subs in
ExtUtils::Command use @ARGV rather than subroutine arguments.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      04-11-2005

"bill" <(E-Mail Removed)> wrote in message
news:d3a48u$o1g$(E-Mail Removed)...
>
>
>
> What's the best way to create an empty file? I mean is there
> something less circuitous than
>
> {
> my $inane;
> open my $inane, '>', $empty and close $inane
> or die "Failed to create $empty: $!\n";
> }
>
> and that does not use system(), as in
>
> system('touch', $empty);


Your two examples do different things (sometimes). Which is it to be?


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-11-2005
Tintin <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> "bill" <(E-Mail Removed)> wrote in message
> news:d3a48u$o1g$(E-Mail Removed)...
> >
> >
> >
> > What's the best way to create an empty file? I mean is there
> > something less circuitous than
> >
> > {
> > my $inane;
> > open my $inane, '>', $empty and close $inane
> > or die "Failed to create $empty: $!\n";
> > }
> >
> > and that does not use system(), as in
> >
> > system('touch', $empty);

>
> Your two examples do different things (sometimes). Which is it to be?


If the objective is to create an *empty* file, the "touch" example is
bogus anyway.

Anno
 
Reply With Quote
 
Heinrich Mislik
Guest
Posts: n/a
 
      04-11-2005
In article <d3a48u$o1g$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...

>What's the best way to create an empty file? I mean is there
>something less circuitous than
>
>{
> my $inane;
> open my $inane, '>', $empty and close $inane
> or die "Failed to create $empty: $!\n";
>}
>


use File::Slurp;
write_file('empty');

Cheers

Heinrich

--
Heinrich Mislik
Zentraler Informatikdienst der Universitaet Wien
A-1010 Wien, Universitaetsstrasse 7
Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments