Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: system and &>/dev/null

by broomduster (Priest)
on Oct 09, 2008 at 23:24 UTC ( #716338=note: print w/replies, xml ) Need Help??

in reply to system and &>/dev/null

Does someone know how to create sparse files with Perl without using a system command?
How about this:
open my $fh, '>', 'zeros' or die "error opening fh: ($!)"; binmode $fh; print $fh pack "x512"; #512 bytes-- season to taste, loop as needed

Replies are listed 'Best First'.
Re^2: system and &>/dev/null
by duelafn (Vicar) on Oct 10, 2008 at 02:21 UTC

    Doesn't seem to create a sparse file:

    [tmp]$ cat open my $fh, '>', 'zeros' or die "error opening fh: ($!)"; binmode $fh; print $fh pack "x".(1024*1024*10); [tmp]$ rm zeros; perl; ls -lh zeros; du -h zeros -rw-rw-r-- 1 duelafn 10M 2008-10-09 22:17 zeros 11M zeros

    However, the following works:

    [tmp]$ cat open my $fh, '>', 'zeros' or die "error opening fh: ($!)"; seek $fh, 1024*1024*10 - 1, 0; print $fh "\0"; [tmp]$ rm zeros; perl; ls -lh zeros; du -h zeros -rw-rw-r-- 1 duelafn 10M 2008-10-09 22:17 zeros 12K zeros

    Good Day,

      This would not create a 100% sparse file because the last block would be all zero (even you print only one zero). Fletch was right in his above post, the combination of seek and truncate must be used.

      Here my first attempt from yesterday evening to create a halfway secure sub function which doesn't mess-up existing files:

      sub create_sparse { my ($file, $size) = @_; return if -e $file && ! -f _; return -2 if -e _ && -s _ >= $size; open my $fh, '+>', $file or return; eval { seek $fh, $size, 0 or die; truncate $fh, $size or die; } or do { close $fh; return; }; close $fh or return; return -1; }
        Just curious:
        • Why the need for seek when you're just going to close $fh after truncate?
        • Please expain the eval - close $fh - return logic you're using.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716338]
[perldigious]: I watched some video on YT awhile back with auto-subtitle on and the speaker had a very thick cockney sort of English accent... hillarity ensued in the subtitles.
[Discipulus]: IHAA=I hate acronyms anyway
[perldigious]: If you want a linguistic adventure...
[perldigious]: No offense to any Scotsman, I love Scots. Well actually, I love Scotch, but I'm sure the people are great too. :-P
[Discipulus]: perldigious i understand i word on ten, to be optimistic..

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-06-23 16:47 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (552 votes). Check out past polls.