[Nh-pm] The Perly Way

Paul L Lussier pll at lanminds.com
Wed May 14 12:27:07 CDT 2003


In a message dated: Wed, 14 May 2003 12:47:19 EDT
Ray Cote said:

># run query and output result with Oracle nulls converted to string 'NULL'.
>sub runQuery {
>	my ($sql) = @_;
>	my $aref = dbSqlReturnAllRows( $dbh, $sql ) or die;
>	my $rowref;
>
>	@$aref = map fixupNULLrow($_), @$aref;
>
>	foreach $rowref (@$aref)
>	{
>		print join( "\t", @$rowref) . "\n";
>	}
>}

How about:

  # run query and output result with Oracle nulls converted to string 'NULL'.
  sub runQuery {
    my ($sql) = @_;
    my $aref = dbSqlReturnAllRows( $dbh, $sql ) or die;
    my $rowref;

    foreach $rowref (@$aref)
    {
      map { $_ = 'NULL' if !defined($_) } @$rowref;
      print join( "\t", @$rowref) . "\n";
    }
  }

All I'm doing here is eliminating the extra traversing of each array 
here.  Since you're already traversing each array in order to print 
out the rows, there's no need to traverse each array to separately 
change the value of components of that array.

Effectively what I'm doing is collapsing those 2 extra subs into a 
single line, and placing it inside a look which already does the same 
thing.

You could also do something like:

  # run query and output result with Oracle nulls converted to string 'NULL'.
  sub runQuery {
    my ($sql) = @_;
    my $aref = dbSqlReturnAllRows( $dbh, $sql ) or die;
    my $rowref;

    foreach $rowref (@$aref)
    {
      map { $_ = 'NULL' if !defined($_); # change undef'ed elements to NULL
            print "$_\t";                # print each element as we see them
                                         # following it with a tab
          } @$rowref;
      print "\n";                        # terminate with a newline
    }
  }

The down side of this method is that you end up with an extra \t 
immediately prior to the \n which you don't get with join().

Hmmmm, how about:

  # run query and output result with Oracle nulls converted to string 'NULL'.
  sub runQuery {
    my ($sql) = @_;
    my $aref = dbSqlReturnAllRows( $dbh, $sql ) or die;
    my $rowref;

    foreach $rowref (@$aref)
    {
      map { $_ = 'NULL' if !defined($_); # change undef'ed elements to NULL
            $string .= "$_\t";           # add the element to $string
                                         # followed by a tab
          } @$rowref;
      chop $string;                      # remove trailing tab
      print "$string\n"                  # print with terminating newline
    }
  }

This should be pretty close to the original.  You could change that 
'chop()' call to something like:

	$string =~ s/\t$//;

if you want to explicitly only remove the trailing tab, rather than 
run the risk of accidently removing the last character, which is what 
chop() does.

HTH,
-- 

Seeya,
Paul
--
Key fingerprint = 1660 FECC 5D21 D286 F853  E808 BB07 9239 53F1 28EE

	It may look like I'm just sitting here doing nothing,
   but I'm really actively waiting for all my problems to go away.

	 If you're not having fun, you're not doing it right!





More information about the Nh-pm mailing list