Re: Problems passing a reference to a hash between functions



>>>>> "M" == Mons <inthrax@xxxxxxxxx> writes:

M> Agree with previous posters buw want to add a general notices...
M> 1. Ok, it's kinda standart in C to return 0 on succes and -1 on
M> failure.
M> But as noticed Paul Lalli, we're programming Perl ;)
M> so
>> if(ProcessTableValues(\%results) < 0)
>> {
>> $conn->disconnect;exit(-1);
>> }
M> will look nicer such way:

M> unless (ProcessTableValues(...)) {
M> $conn->disconnect;
M> exit(-1);
M> }

that is better perl style i agree but the OP has his return value style
so ingrained i didn't want to comment on it.

M> 2.
>> print STDERR "\n", 'Failed to prepare ' , $DBI::errstr;
M> Be simplier!
M> warn 'Failed to prepare ' . $DBI::errstr;

be simpler:

warn "Failed to prepare $DBI::errstr" ;

M> 3. Surely, you don't need to do separate prepare,execute and fetch.
M> Call once
M> $conn->selectall_hashref('select tabid, tabname from systables where
M> tabid < 5','tabid')

dbi prepare is useful, especially if you use placeholders. on some db
servers it can actually be much faster since the sql is only compiled
once.

M> 4. And finally
M> For such a routines may be better to write them in this way:

M> sub some_func ($$) {

prototypes are not cool in perl in most cases.

M> my ($param1,$param2) = @_;
M> eval {
M> # do here all stuff without checking at every step
M> stuff1();
M> stuff2() or warn "noncritical IO stuff failed: $!";
M> stuff2() or die "critical stuff failed";
M> };

but you are checking the last two steps. using eval BLOCK to catch
errors is ok when you have deep nesting and you aren't sure where the
error should be handled. this style is also called exception handling
and there are modules to make it easier to do. but a clean return value
design with proper handling can do the job just as effectively.

M> With this guides, your program may be rewritten in about 20 lines.
M> While shorter code is more readable and powerful :)

that is true to a point. code written for the sake of just shortness can
be harder to maintain and debug. there is a balance that must be found
between concise vs. bloated

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.



Relevant Pages

  • Re: reg exp
    ... yes as I read in programming perl as well lead me to this solution. ... and finally found this hint in perldoc perlop: ...
    (perl.beginners)
  • Re: Question on download by LWP
    ... Programming Perl 3rd ed, p605 ... "don't use camelcase with Perl" to me over-steps the mark. ... I just passed on the advice given in Programming Perl;-) ... perldoc perlstyle. ...
    (comp.lang.perl.misc)
  • Re: Oreillys "Learning Perl 3rd Edition"
    ... Afterwards I plan on moving upward and onward in perl. ... Oreilly's "Programming Perl" or "Perl Cookbook" would be good to jump ... The old soul mate, which I ...
    (perl.beginners)
  • Re: Installing GDBM_File
    ... >> Programming Perl. ... And probably a pink cover? ... Uhmmm... ... Probably has a lot not in it, since it covers Perl 4. ...
    (comp.lang.perl.modules)
  • Re: Theres no LIMIT to my SPLITting headache.
    ... > The most enlightening experience I've had with perl was discovering that: ... > is a LOT slower than ... Not by a long shot on my machine, running Perl 5.8.7. ... Granted, that's a *very* simple pattern, but the shorter code came out ...
    (comp.lang.perl.misc)