Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Unreferenced scalar?

by HamNRye (Monk)
on May 19, 2008 at 16:45 UTC ( [id://687415]=perlquestion: print w/replies, xml ) Need Help??

HamNRye has asked for the wisdom of the Perl Monks concerning the following question:

UPDATED:

Well, it's not perfect, but by breaking out the "Word" portion to it's own subroutine, I was able to make this work.

I'm guessing somehow two variables were stomping on each other...

Final code:

use strict; # import OLE use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # report OLE runtime errors my $pickupDir = 'C:\\p2exe\\word2txt'; my @ads = getAdNums(); collectAds(@ads); #For the time being, assume that a list of ad numbers will be gathered + as an array. sub getAdNums { my @AdNums = ("test"); return @AdNums; } ###################################################### # Read in word files and export as text docs ###################################################### sub collectAds { foreach my $ad (@_) { # Need docname and savename my $doc = "$pickupDir\\$ad.doc"; my $savename = "$pickupDir\\$ad.txt"; # Add check to make sure .doc file exists if (-e $doc) { wordToText($doc, $savename); my @info = get_file($savename); printArr(@info); } } } sub wordToText { my $input = shift; my $output = shift; print "Starting word\n"; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win3 +2::OLE->new('Word.Application', 'Quit'); $word->{Visible}= 0; # we don't need to see Word in an active wind +ow # open the specified Word doc print "Opening $input\n"; $word->Documents->Open($input) or die("Unable to open document ", Win32::OLE->LastError()); # save the file as a text file $word->ActiveDocument->SaveAs({ FileName => $output, FileFormat => wdFormatDOSTextLineBreaks}); # close document and Word instance print "Closing document and Word\n"; $word->ActiveDocument->Close(); $word->Quit; } ######################################################## # Standard subs below # Need to redo to get rid of unreferenced Scalar. sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return <IN>; } else { return join '', <IN>; } } sub printArr { my $n = 0; foreach (@_) {print $n++.": $_\n"}; }

Final output:

Starting word
Opening C:\p2exe\word2txt\test.doc
Closing document and Word
0: test

I have changed some of the code to make it easier for others to follow along... I am trying to change word docs to text... The output I get says:

Attempt to free unreferenced scalar at C:\p2exe\word2txt\debug_word2tx +t.pl line 28. Can't call method "Dispatch" on unblessed reference at (eval 1) line 1 +. END failed--call queue aborted.

Line 28 is where the if clause starts checking the existence of the doc I am trying to convert. if (-e $doc) { I started testing chunks of code, and the word conversion works up until the point where I read in the text document with my "get_file" routine. (AKA if I comment out that line I get no error) But that is the same "get_file" routine I have used for years... And reading in a text doc without doing the Word part works fine...

I am stumped... I have tried changing variables, etc. Can anyone point me as to what I'm doing wrong? Thanks in advance for any help.

Code is below... "Test.doc" is a Word 2003 doc with just the word "test" in it.

Current error:

Attempt to free unreferenced scalar: SV 0x1ab2744, Perl interpreter: 0x2243f4 at C:\p2exe\word2txt\debug_word2txt.pl line 28.
use strict; # import OLE use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # report OLE runtime errors my $pickupDir = 'C:\\p2exe\\word2txt'; my @ads = getAdNums(); collectAds(@ads); #For the time being, assume that a list of ad numbers will be gathered + as an array. sub getAdNums { my @AdNums = ("test"); return @AdNums; } ###################################################### # Read in word files and export as text docs ###################################################### sub collectAds { foreach my $ad (@_) { # Need docname and savename my $doc = "$pickupDir\\$ad.doc"; my $savename = "$pickupDir\\$ad.txt"; # Add check to make sure .doc file exists if (-e $doc) { print "Starting word\n"; my $word = Win32::OLE->GetActiveObject('Word.Application') + || Win32::OLE->new('Word.Application', 'Quit'); $word->{Visible}= 0; # we don't need to see Word in an act +ive window # open the specified Word doc print "Opening $doc\n"; $word->Documents->Open($doc) or die("Unable to open document ", Win32::OLE->LastErr +or()); # save the file as a text file $word->ActiveDocument->SaveAs({ FileName => \$savename, FileFormat => wdFormatDOSTextLineBreaks}); # close document and Word instance print "Closing document and Word\n"; $word->ActiveDocument->Close(); $word->Quit; my @info = get_file($savename); printArr(@info); } } } ######################################################## # Standard subs below # Need to redo to get rid of unreferenced Scalar. sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return <IN>; } else { return join '', <IN>; } } sub printArr { my $n = 0; foreach (@_) {print $n++.": $_\n"}; }

Commenting out the "get_file" call outputs:

Starting word
Opening C:\p2exe\word2txt\test.doc
Closing document and Word

Commenting out the Word portion outputs:

0: test

Running the whole thing gives me:

Attempt to free unreferenced scalar: SV 0x1ab2744, Perl interpreter: 0x2243f4 at C:\p2exe\word2txt\debug_word2txt.pl line 28.

Replies are listed 'Best First'.
Re: Unreferenced scalar?
by moritz (Cardinal) on May 19, 2008 at 16:52 UTC
    C:\p2exe\word2txt\debug_word2txt.pl

    You aren't using perlcc by any chance? (just guessing from the path name). It's deprecated and never worked properly.

    In general it helps to provide the version number of the perl you run and which distribution it is (ActivePerl, Strawberry, Whatever...).

      N:\>perl -v
      
      This is perl, v5.6.1 built for MSWin32-x86-multi-thread
      (with 1 registered patch, see perl -V for more detail)
      
      Copyright 1987-2001, Larry Wall
      
      Binary build 638 provided by ActiveState Corp. http://www.ActiveState.com
      ActiveState is a division of Sophos.
      Built Apr 13 2004 19:24:21
      
      And I generally use perlapp from Active State for deployment.
        perl 5.6.1 is ancient. Seven years is a lot of time in software land. If you can, try to upgrade to 5.8.8 or even 5.10.0.

        There were many fixes for memory handling since 5.6.1, and I wouldn't bother chasing bugs in perl that might have been resolved years ago.

        I honestly don't know if that will fix your problem, but I still consider upgrading worth the effort.

Re: Unreferenced scalar?
by apl (Monsignor) on May 19, 2008 at 16:59 UTC
    None of this will give you the fix. You shouldn't preface procedure names with '&'. One practice to attempt is to bracket
    if (-e $doc) {
    with
    } else { print STDERR "File '$doc' does not exist!\n"; }
    Then in the future you can manually check for the existance of the file, see if the proper directory is being used, etc.

    As far as your root problem, I won't shoot my foot off...

      I left the else clause off in an attempt to make the code more straightforward. If the file doesn't exist, the code doesn't run, and I don't get the error. I modified the sub calls to be:
      my @ads = getAdNums(); collectAds(@ads);
      I also upgraded to the latest Active State version:
      N:\>perl -v
      
      This is perl, v5.10.0 built for MSWin32-x86-multi-thread
      (with 5 registered patches, see perl -V for more detail)
      
      Copyright 1987-2007, Larry Wall
      
      Binary build 1003 285500 provided by ActiveState http://www.ActiveState.com
      Built May 13 2008 16:52:49
      
      Still getting complaints about Unreferenced Scalar:
       Attempt to free unreferenced scalar: SV 0x1ab2e84, Perl interpreter: 0x2243f4 at C:\p2exe\word2txt\debug_word2txt.pl line 28.
      

      Still hoping someone can point me in the right direction.

Re: Unreferenced scalar?
by psini (Deacon) on May 19, 2008 at 17:19 UTC

    A (probably useless) question: do you still get that error if @AdNums has only one element?

    Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

      Yes, I've tried only including one element, and I still get the same error.

        Can you try substituting my @info = get_file($savename); with print $savename;?

        I'm sorry but I can't duplicate your environment and so I can't do any check by myself :(

        Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://687415]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found