Solved

syntax

Posted on 2006-11-30
22
296 Views
Last Modified: 2008-02-01
I have a perl script that calls the following function, passing in an array by reference.

my @array;
func(\@array);

Inside func, it calls another function which again passes this array by reference into another function.  I want the original array to be modified by func.

sub func
{
      my @array_local = $_[0] ;
        func2( \@{$array_local[0]} );
}

The problem is that I think that Perl doesn't like my handling of the array this way - I see an error in the Apache2 error log like:

Win32::OLE(0.17): GetOleObject() Not a Win32::OLE object at c:/woc/Perl/site/lib//Win32/OLE/Lite.pm line 154 during global destruction.

My question is: is there a friendlier way to do what I need to do, that Perl won't get upset over?

Thanks!
0
Comment
Question by:kesea
  • 9
  • 7
  • 2
  • +3
22 Comments
 

Author Comment

by:kesea
ID: 18050559
In case the problem is inside func2, here it is:

sub func2
{
      my @array_local = $_[0] ;

      # reset/zero input array
      @$array_local = ();

      my $sth_pm = $dbh_pm->prepare(qq(SOME QUERY IN HERE)) ;
      $sth_pm->execute or die "Can't execute: $DBI::errstr\n";
      @{$array_local[0]} = $sth_pm->fetchrow_array();
}
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 18050584
You are turning the ref back into an unreferecned array, try

sub func
{
     my $array_local_ref = $_[0] ;
        func2( $array_local_ref );
}

sub func2
{
     my $array_local_ref = $_[0] ;

    # and now say something like
   $array_local_ref->[0] = $sth_pm->fetchrow_array();

0
 
LVL 84

Expert Comment

by:ozo
ID: 18050600
Did you mean either
my @array_local = @{$_[0]} ;
or
my $array_local = $_[0];
?
0
 

Author Comment

by:kesea
ID: 18050608
Not sure who you're asking Ozo?  Me or Teraplane?  If for me, can you clarify, I'm not sure what you're asking.
0
 

Author Comment

by:kesea
ID: 18050625
teraplane,

The code I posted does work to modify the orginal array (besides getting this error in the Apache log file), so I'm not sure that I turned the reference into an unreferenced array or it wouldn't have kept the reference and modified the original array.

kesea

0
 
LVL 84

Expert Comment

by:ozo
ID: 18050667
Actually, the way you are doing it
$array_local[0] is the array ref, (and $array_local[1] etc. are unused, which makes it useless and possibly deceptive to use an array there instead of a scalar)
fetchrow_array() returns a list in list context, calling it in scalar context like
 $array_local_ref->[0] = $sth_pm->fetchrow_array();
may be undefined whether it returns the first column or the last, so that's probably not a good idea
0
 
LVL 84

Expert Comment

by:ozo
ID: 18050676
  my @array_local = $_[0] ;

     # reset/zero input array
     @$array_local = ();
$array_local is different from @array_local

my @array_local = $_[0] ;
is the same as
my @array_local;
$array_local[0] = $_[0] ;
0
 

Author Comment

by:kesea
ID: 18050680
Ozo,

Can you post the proper working syntax for func and func2?  Would really appreciate it.  This reference stuff is really confusing to me.

Thanks,
kesea
0
 
LVL 84

Expert Comment

by:ozo
ID: 18050696
Although there seems to be a confusion between arrays, scalar, references,
the original code looks like it should work, except that @$array_local = (); does nothig useful, unless $array_local was defined  elsewhere

the problem may be when you exit func2 and $sth_pm goes out of scope
0
 
LVL 84

Expert Comment

by:ozo
ID: 18050720
You might try doing
  $sth_pm->finish();
before exiting func2
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:kesea
ID: 18050758
am using mod_perl and the database connection should be persistent, so not sure if this would accomplish anything?

do you still think it might be contributing to the error in the log file?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18050784
$dbh_pm may be persistent, but my $sth_pm  is destroyed when the function exits.
If $sth_pm does not know how to finish itself when it is destroyed, you may need to do it explicitly.
I'd suggest at least trying it.
0
 

Author Comment

by:kesea
ID: 18050929
Hi Ozo,

Adding that line unfortunately did not get rid of that log in the error file.

Any other suggestions?

kesea
0
 
LVL 84

Expert Comment

by:ozo
ID: 18050978
can we verify whether the fetchrow_array line really is the one it is dieing on?
can you add
$|=1;
print "before fetchrow_array\n";
@{$array_local[0]} = $sth_pm->fetchrow_array();
print "after fetchrow_array\n";
and see if any of them print?
0
 

Author Comment

by:kesea
ID: 18051077
Hi Ozo,

Those lines print out, and it is not that the code ever dies or that I or a user would ever see an indication that it didn't work.  I just see that error in the log file.  Seems like it works, but then when it's done and goes to clean something up, is when it complains.

Also, the problem is that sometimes it prints that error when running a script, sometimes it doesn't when running the same script.  I will try to isolate what line exactly is having the problem and report back.

kesea
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18052018
> .. am using mod_perl and the database connection should be persistent,
silly questiuion: have you designed you perl code with mod_perl in mind? Means, does you perl code ensure that global varaibles are really global to all functions and calls, nevertheless how many connections you have. Do you ensure that such global variables are *never* changed within any function?
I guess you just saw a single instance of doing things wrong with mod_perl, while the problem is buried deep in the design of your code. Please correct me if my guess is wrong.
0
 

Author Comment

by:kesea
ID: 18054598
Want to clarify where my use of mod_perl for a persistent db connection is wrong?
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 18055495
What exactly is in your array?  Why do you think that's where you're getting your error?  The error is being thrown by Win32::OLE.  And the code you seem to be messing with is DBI-- completely unrelated to Win32::OLE.  Does your array contain references to Win32::OLE objects?  

I would bet that you're implicitly releasing some COM objects somewhere that you still have a perl reference to and when perl goes to garbage collect them it finds out that they no longer point to valid COM objects.  I would concentrate on your Win32::OLE use.  Or at least post the portion of your code that deals with it, as that is where it looks like your error (probably actually a carp'd warning) appears to be coming from.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 18055946
>  Want to clarify where my use of mod_perl for a persistent db connection is wrong?
you're right, that should be done in mod_perl
But my qustion was about *all* your variables, not just the db handle.
0
 

Author Comment

by:kesea
ID: 18390973
ok I found out what the problem was.  Because I am using mod_perl, it doesn't like connecting to the MS SQL database with ADO, I guess something in there is not multi-threaded-friendly.

I guess I will close the question...
0
 

Accepted Solution

by:
AnnieMod earned 0 total points
ID: 19388466
PAQed with points refunded (500)

AnnieMod
Cleanup Admin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 184
Parse csv file and generate graphs in HTML in bash 8 138
File Find regex problem 4 58
Perl Frameworks 1 56
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now