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

Hi All,
Can anyone please have a look at my code below. I don't know why its execution is showing undefined subroutine, though I have defined it.
#!/usr/bin/perl -w use Spreadsheet::ParseExcel; use strict; use warnings; use diagnostics; my $index = 1; my ($email, $token); my $filename = shift || "Test.xls"; my $e = new Spreadsheet::ParseExcel; my $eBook = $e->Parse($filename); my $sheets = $eBook->{SheetCount}; my ($eSheet, $sheetName); foreach my $sheet (0 .. $sheets - 1) { $eSheet = $eBook->{Worksheet}[$sheet]; $sheetName = $eSheet->{Name}; # print "Worksheet $sheet: $sheetName\n"; next unless (exists ($eSheet->{MaxRow}) and (exists ($eSheet->{Max +Col}))); foreach my $row ($eSheet->{MinRow} .. $eSheet->{MaxRow}) { foreach my $column ($eSheet->{MinCol} .. $eSheet->{MaxCol}) { next unless (defined $eSheet->{Cells}[$row][$column]); if ( ($index % 2) == 1 ){ $email = $eSheet->{Cells}[$row][$column]->Value; print $email; } else{ $token = $eSheet->{Cells}[$row][$column]->Value; print $token; } } getvalue1(); } } getvalue1{ print "$email : $token"; }

Error:
Undefined subroutine &main::getvalue1 called at sample.pl line 37 (#1) (F) The subroutine indicated hasn't been defined, or if it was, it has since been undefined. Uncaught exception from user code: Undefined subroutine &main::getvalue1 called at sample.pl line 37.

Thanks

Alok

Replies are listed 'Best First'.
Re: Undefined subroutine &main::
by davido (Cardinal) on Aug 24, 2015 at 04:05 UTC

    You never declare and define getvalue1. This:

    getvalue1{ print "$email : $token"; }

    ...should be this...

    sub getvalue1{ print "$email : $token"; }

    And your print statement probably would be better with a newline appended to the end:

    print "$email : $token\n";

    Another note: Though you are using lexical variables (my variables), you aren't using them effectively. One place where your use of lexicals could be improved upon is in how you pass values into subroutines. Instead of absorbing $email and $token into getvalue1(), pass values in:

    sub getvalue1{ my ($e, $t) = @_; print "$e : $t\n"; }

    ...and invoke the subroutine like this:

    getvalue1($email, $token);

    In a small script like this it's not a big win, but in larger scripts (more than, say 100 lines), it becomes untenable trying to follow how a global variable's state gets manipulated throughout the course of the script. By keeping variable scopes small, and decoupling subroutines from the environment around them, you minimize action at a distance, and make it much easier to comprehend the program's logic, flow, and state.


    Dave

Re: Undefined subroutine &main::
by james28909 (Deacon) on Aug 24, 2015 at 04:05 UTC
    On line 42, try changing
    getvalue1{ print "$email : $token"; }
    To:
    sub getvalue1{ print "$email : $token"; }
    EDIT: When I were replying, there were no responses lol
      Thanks mate.

      I think I need rest. I also don't know how can I do this? A bad day for me :(

Re: Undefined subroutine &main::
by Anonymous Monk on Aug 24, 2015 at 04:03 UTC