Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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]
[1nickt]: Although the example given does not produce an error as stated, on my perl.
[1nickt]: perl -wE 'say for map { ... } 1..3;' # "Unimplemented at -e line 1" ... <c>perlsyn says that should throw a syntax error...
[karlgoethebier]: 1nickt: "Edge cases..." Yes, it seems so. Thanks Nick

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-11-18 19:48 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.