clean programming

clean programming

am 07.10.2005 02:31:53 von dszostek

Hi,

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
greatly.


Dave

Re: clean programming

am 07.10.2005 02:41:28 von John Bokma

"Dave" wrote:

> Hi,
>
> 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)?

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
course).

> Are there any other tools or tricks that others
> use that would be useful in generating clean, efficient,
> memory-conserving code?

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

> 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.

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

--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)

Re: clean programming

am 07.10.2005 06:14:54 von Scott W Gifford

"Dave" writes:

[...]

> 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?

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,
unfortunately.

> 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.

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.

----Scott.

Re: clean programming

am 07.10.2005 18:17:32 von xhoster

"Dave" wrote:
> Hi,
>
> 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

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

> 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)?

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

> Are there any other tools or tricks that others
> use that would be useful in generating clean, efficient,
> memory-conserving code?

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

Xho

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

Re: clean programming

am 28.10.2005 19:57:23 von Infochannel

John Bokma wrote:
> "Dave" wrote:
>
> > Hi,
> >
> > 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)?
>
> 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
> course).
>
> > Are there any other tools or tricks that others
> > use that would be useful in generating clean, efficient,
> > memory-conserving code?
>
> Think before you program :-) Also, don't optimize too early, or
> overoptimize.
>
> > 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.
>
> Post some Perl code, and maybe you get some more tips (30-50 lines, not
> 5000 :-)
>
> --
> John Small Perl scripts: http://johnbokma.com/perl/
> Perl programmer available: http://castleamber.com/
> I ploink googlegroups.com :-)

Re: clean programming

am 28.10.2005 20:08:33 von Infochannel

Hi,

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
perl

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

###################################


#!/usr/bin/perl


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;
foreach(url_param()){param($_,url_param($_))}
require '/www/infochannel.org/thepixelwars.de/admin/payperpixel.conf ';
$src = Image::Magick->new;
$back = Image::Magick->new || die "cant create object back";
$time=time();$jscript='';
if (defined param('newpicture') and param('newpicture') ne
''){$time=param('newpicture')}
if (defined param('picture') and param('picture') ne ''){
my $file=upload('picture');
chomp $file;
$newfile="$tmpdir$time";
open (TMP,">$newfile")||die "cant write to $newfile $!";
while (<$file>){print TMP $_}
close(TMP);
my $info = image_info($newfile);
if ($info->{file_media_type} !~ /image/){
$message.=br."Sorry your file does not appear to be an image file so
could not be used.";
unlink($newfile);
}
else {
param('newpicture',"$time");
}
}


##################

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: http://www.thepixelwars.de/maze/index.html

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

Heiner

Re: clean programming

am 29.10.2005 16:10:17 von jurgenex

Infochannel wrote:
> I've bought a perl script a few days ago, which will not work at all.

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

> The programmer can't or will not help me in solving the problem.

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!

jue