How to create an empty file?

Discussion in 'Perl Misc' started by bill, Apr 10, 2005.

  1. bill

    bill Guest

    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
     
    bill, Apr 10, 2005
    #1
    1. Advertising

  2. bill <> 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 10, 2005
    #2
    1. Advertising

  3. bill

    Guest

    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";
    > }


    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
     
    , Apr 10, 2005
    #3
  4. bill

    ko Guest

    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
     
    ko, Apr 10, 2005
    #4
  5. bill

    bill Guest

    In <d3ao8p$68g$> ko <> 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
     
    bill, Apr 10, 2005
    #5
  6. bill <> wrote in
    news:d3bs78$skh$:

    > In <d3ao8p$68g$> ko <>
    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 10, 2005
    #6
  7. bill

    Tintin Guest

    "bill" <> wrote in message
    news:d3a48u$o1g$...
    >
    >
    >
    > 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?
     
    Tintin, Apr 11, 2005
    #7
  8. bill

    Anno Siegel Guest

    Tintin <> wrote in comp.lang.perl.misc:
    >
    > "bill" <> wrote in message
    > news:d3a48u$o1g$...
    > >
    > >
    > >
    > > 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
     
    Anno Siegel, Apr 11, 2005
    #8
  9. In article <d3a48u$o1g$>, 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
     
    Heinrich Mislik, Apr 11, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,061
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    864
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    610
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    680
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    580
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page