June 2017 Archives

Perl DBI and INTERVAL values

I was having trouble using DBI to bind an interval value. Wonder if anyone has a better solution than this:

http://openbedrock.blogspot.com/2017/06/binding-parameters-for-oracle-interval.html

i could not find any information about binding an Oracle INTERVAL LITERAL. Maybe I just missed it in the docs?

Another reason not to use each()

So I’ve learned my lesson. Do not use each().

Always good to read the blogs…

https://blogs.perl.org/users/rurban/2014/04/do-not-use-each.html

Here’s a fun one that caused perl to go into an infinite loop. I suppose being frugal and not assigning things to variables is not necessarily a good idea.

  #!/usr/bin/perl

  use strict;
  use warnings;

  my %thingies = %{{qw/a 1 b 2 c 3/}};

  my $c=0;

  #while (my ($a,$b) = each %thingies ) {
  while (my ($a,$b) = each %{{qw/a 1 b 2 c 3/}} ) {
    print "$a $b $c\n";
    last if ++$c > 4;  # stop the madness
  }

Uncommenting out the line above where each uses a pre-assigned version of the same data element works fine. Not knowing anything regarding the internals of Perl makes me scratch my head. I’m sure there is a perfectly good explanation for this and maybe it has even been corrected in a more recent version of Perl?

$ perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

Follow-up

Reading the PerlMonks helps too!

http://www.perlmonks.org/?node_id=754380

I guess perl does not read minds all of the time. In the tradition of do what I want, not what I wrote I had hoped that perl knew that I only wanted to create the data element once, not each time through the loop. My bad.

Moral: Do not use each() - unless you are aware of all of the gotchas and even them don’t use each.

About bigfoot

user-pic I blog about Perl and Bedrock.