Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl module problem

by Anonymous Monk
on Dec 21, 2003 at 12:07 UTC ( [id://316173]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
I am making a perl module .
However , I am want to use another module support in my pm file subroutine .
The subroutine is as follows :
sub xls_conv($) { use Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; my $excelfileis = $self->{filename}; my $oBook = $oExcel->Parse("$excelfileis"); for(my $iSheet=0; $iSheet < $oBook->{SheetCount}; $iSheet++) { print "$oBook->{SheetCount}"; } }
However , when I use the above module subroutine in my perl script I get an error as :
Use of uninitialized value in numeric lt (<) at /usr/lib/perl5/site_pe +rl/5.8.1/Converter.pm line 149.
Line 149 refers to the
for(my $iSheet=0; $iSheet < $oBook->{SheetCount}; $iSheet++)
.
Can you suggest me what's going wrong in my subroutine .

Replies are listed 'Best First'.
Re: Perl module problem
by liz (Monsignor) on Dec 21, 2003 at 12:25 UTC
    In short: $oBook->{SheetCount} is undefined, probably because the key "SheetCount" does not exist in the hash reference $oBook.

    Probably this line fails to find all the expected things from the Excel file referred to by $excelfileis.

    my $oBook = $oExcel->Parse("$excelfileis");
    It also could be a scoping problem: It's very odd to use a module inside a subroutine like that. And your strange indentation suggests that the for loop may actually be outside the scope of sub xls_conv in your actual program.

    Have you tried adding use strict to your code to see whether there is some scoping problem? To check whether the $oBook in the for loop is actually the one you expect (and not an unitialized package global).

    Hope this helps.

    Liz

Re: Perl module problem
by TomDLux (Vicar) on Dec 21, 2003 at 15:08 UTC
    1. use strict may reveal a nunmber of errors.
    2. use Data::Dumper; and print Dumper $oBook; just before the for loop, will show what $oBook contains.
    3. Manually debug with Perl -d. You can determine what is there, what can be done, and why.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Perl module problem
by ysth (Canon) on Dec 21, 2003 at 17:35 UTC
    I don't know what in particular is causing what you are seeing, but I do see a couple of flaws.

    The $self->{filename} makes me think you are using xls_conv as an object method but you are not setting $self anywhere in the sub. I'd expect to see a line like my $self = shift; at the top.

    In any case, you have a prototype on your sub (which shouldn't be the case for an object method) but aren't using any parameters.

    Try use strict and it will complain about any variables that you should be declaring with my in your subs but are instead using as (undeclared) globals.

Re: Perl module problem
by djantzen (Priest) on Dec 21, 2003 at 20:31 UTC

    In addition to the errors and debugging tips the others have pointed out, you should know that use is equivalent to BEGIN { require 'Module X' } which means that no matter where your use statement occurs, it will be executed during compilation of your module. It belongs therefore at the top of the file, and not inside of this subroutine.

    Also, how do you mean to call these subroutines? Like MyPackage::MySub or MyPackage->MySub? If it's the latter then you'll need to fetch the package name from @_ and you should just drop the prototypes since they'll be ignored.


    "The dead do not recognize context" -- Kai, Lexx
      I frequently prefer to put the use for a module that is used only in one sub in the body of the sub. The only problem I could see with this approach is if anything is imported; in that case I would agree that it belongs at the top, since it affects the whole package.

        There aren't any negative consequences codewise from doing it, even if you are importing stuff. For example, occasionally when debugging I'll throw use Data::Dumper; print Dumper $foo in the middle of some code. Dumper is an imported subroutine in this usage and works just fine so long as it's used in the same package it's imported into.

        The problem is not with writing functional code but with writing readable, maintainable code. Novice programmers, probably like the AnonyMonk here, might read such code and think mistakenly that use works at runtime like require or eval "use Foo";. Moreover, someone maintaining your code, even if an experienced Perl programmer, isn't generally going to scan your codebase looking for embedded use statements because it's customary to place them at the top of the file, and that could lead to confusion, for example in case of conflicts with imported symbols. In short, yeah you can get away with it, but it introduces a maintenance risk.


        "The dead do not recognize context" -- Kai, Lexx
Re: Perl module problem
by Roger (Parson) on Dec 22, 2003 at 01:25 UTC
    for(my $iSheet=0; $iSheet < $oBook->{SheetCount}; $iSheet++) { print "$oBook->{SheetCount}"; }
    You immediate problem has been identified by liz and other monks. I just have a little comment on the C style for loop you had. I wrote a little benchmark program to test the efficient of a C style for loop and a Perl style for loop with a set. It probably won't matter to your program since your sheetcount is likely to be small, but nevertheless important to know.
    use strict; use warnings; use Benchmark qw/ timethese cmpthese /; my $oBook = { SheetCount => 1000 }; cmpthese(timethese(10000, { 'For_C' => '&For_C', 'For_Set' => '&For_Set', })); sub For_C { my $var; for (my $iSheet=0; $iSheet < $oBook->{SheetCount}; $iSheet++) { $var = $iSheet; } } sub For_Set { my $var; for my $iSheet (0..$oBook->{SheetCount}-1) { $var = $iSheet; } }
    And the result -
    Benchmark: timing 10000 iterations of For_C, For_Set... For_C: 14 wallclock secs (14.33 CPU) @ 697.84/s (n=10000) For_Set: 7 wallclock secs (7.22 CPU) @ 1385.04/s (n=10000) Rate For_C For_Set For_C 698/s -- -50% For_Set 1385/s 98% --
    It shows that the Perl style for loop with a set is about twice as fast as the C style loop. This is a significant performance improvement.

    There is one more advantage of the Perl loop over the C loop - in the C style for loop, the following is interpreted for every iteration of the loop -
    $iSheet < $oBook->{SheetCount} # hash look up and comparison
    while in the Perl style for loop the hash look up is evaluated once only, and hence an increase in performance.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found