clean programming

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I have been writing perl for about 8 years. I've never gotten too
involved with it and I've always had rather messy code.

I am working on a project which is going to involve a) performance
issues and b) mod_perl. I need to make sure that I free-up memory in my
programs as quickly as possible and I localize all of my variables. I
have turned on strict, warnings, and diagnostics.

Is there any utility which will tell me which variables I have used in
a subroutine so that I can undef them at the end of the routine
(freeing up memory)? Are there any other tools or tricks that others
use that would be useful in generating clean, efficient,
memory-conserving code?

I've read much on the internet about small coding tips along the lines
of never writing $var="$var2" and instead writing $var=$var2 and things
along those lines, but I think I need more than I've found.

I thank you in advance for your help and expertise. I appreciate it


Re: clean programming


Quoted text here. Click to load it

As long as you use lexical variables ("my" variables), they'll
automatically be released when you leave their enclosing block.

Try to use lexical variables wherever possible, and make sure you
manage your global variables carefully, and you should be OK.

I'm not aware of any good tools for profiling Perl memory use,

Quoted text here. Click to load it

Stuff like this isn't going to make a huge difference; it may save a
few bytes here and there, but your big worry is a memory leak.

As long as you make sure that whenever you create something globally,
you eventually free it, you should be OK.


Re: clean programming

Quoted text here. Click to load it

I always thought that the end of a block automatically undefs all my
variables (exceptions are closures, and if you return a ref to one, of

Quoted text here. Click to load it

Think before you program :-) Also, don't optimize too early, or

Quoted text here. Click to load it

Post some Perl code, and maybe you get some more tips (30-50 lines, not
5000 :-)

John                   Small Perl scripts:
               Perl programmer available:
                                        I ploink :-)

Re: clean programming

John Bokma wrote:
Quoted text here. Click to load it

Re: clean programming

Quoted text here. Click to load it

What kind of performance issues?  Speed performance?  Memory performance?
Correct performance under re-entrant/multi-threaded/malicious conditions?

Quoted text here. Click to load it

If you use lexical variables in the subroutine, you don't need to worry
about this.

Quoted text here. Click to load it

Come up with better algorithms, both in terms of big-O and in terms
of the memory-resident working sets of data.

Wrap a copy of the entire code (minus __END__ blocks) in a big
"sub { <entire code> }" and see if you get "variable will not
stay shared" warnings under perl -cw.

If you have a *lot* of numbers, try to ensure they are only stored as
numbers and never use them in a stringy context.  ("bfon" is a big file of
numbers). Doing this:
perl -le 'while (<>) { chomp; push @x, $_+0} ; warn; sleep;' bfon
ends using less than half the memory of this:
perl -le 'while (<>) { chomp; push @x, $_} ; warn; sleep;' bfon


-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB

Re: clean programming


I've bought a perl script a few days ago, which will not work at all.
The programmer can't or will not help me in solving the problem. Maybe
somebody here can help me a little, because I'm not experienced with

the problem: The script should load an image (onto another image) which
is temporarily stored until payment is verified. The script starts as



use DBI;
use CGI qw/:standard *table start_ul/;
use Image::Magick;
use Image::Info qw(image_info dim);
use Digest::MD5 qw(md5_hex);
use LWP::UserAgent;
require '/www/';
$src = Image::Magick->new;
$back = Image::Magick->new || die "cant create object back";
if (defined param('newpicture') and param('newpicture') ne
if (defined param('picture') and param('picture') ne ''){
    my $file=upload('picture');
    chomp $file;
    open (TMP,">$newfile")||die "cant write to $newfile $!";
    while (<$file>){print TMP $_}
    my $info = image_info($newfile);
    if ($info-> !~ /image/){
    $message.=br."Sorry your file does not appear to be an image file so
could not be used.";
    else {


On my server all required modules are installed and working with other
scripts. OS is gentoo linux.

calling the script gives the error message "Sorry your file does not
appear to be an image file so could not be used."

and an empty space instead of the TMP image is shown.

Please see:

I've seen, that nothing is stored in the tmpdir, so its normal that
image::info gives that error.

I really would appreciate your help in solving the problem. At the
moment I have no more ideas.

p.s. the CHMODS of the tempdir and the image were to store the nefile
in are set to 777.

thanks in advance


Re: clean programming

Infochannel wrote:
Quoted text here. Click to load it

Then I think you should return the script for a full refund.

Quoted text here. Click to load it

If he did not fulfill his contractual obligations then maybe you should ask
in a legal NG.
In most countries you will have to give him verifiable written notice (proof
of delivery) that the product is faulty and that you are requesting either a
fix or you will cancel the contract with a full refund. Don't forget to set
a fixed date!


Site Timeline