Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Check remote file for size?

by pjf (Curate)
on Oct 13, 2001 at 12:22 UTC ( #118642=note: print w/replies, xml ) Need Help??

in reply to Check remote file for size?

G'day Mosley,

Firstly, I'd strongly suggest you consider using the CGI perl module, which will do QUERY_STRING decoding for you. It comes standard with perl, provides many very useful features, and will do a better job than your home-grown encoding (particularly for things like parameters with multiple values).

To answer your question about checking a file's size, you can use the -s operator, like this:

$file = "/path/to/file"; if (-s $file) { # file has non-zero size. } else { # File is empty or does not exist. }
-s returns the file size in bytes if you need that much information.

In your particular example, I would suggest opening your file for writing using open(FILE,">$filename"). The ">" will clobber the contents of the file if it exists, as opposed to ">>" which will append to the file. Clobbering the contents means you shouldn't need to care about the file size at all.

Also, there's nothing wrong with using perl modules. In fact, it's very highly recommended. More often than not, re-inventing the wheel results in something which isn't quite as round.

If you're looking at stripping out HTML tags, I'd suggest looking at HTML::Parser, which will do most of the work for you.

If you want your script to die automatically after a certain period of time, you can set an alarm. Unfortunately I don't know how portable this is across non-UNIX operating systems.

$SIG{ALRM} = sub { die "Script took too long.\n" }; alarm(60); # Create alarm signal in 60 seconds. # ... do long-running stuff. alarm(0); # Cancel the alarm
If you want your code to always clean up after itself, you can do so with an END block.
END { unlink($tmpfile); }
An END block runs just before the perl interpretor exits.

Hope that you find all of the above useful.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://118642]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-01-21 16:50 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.