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

Hi, I have a module installed in my computer (called XML::Parse), which I use for storing the recurrent functions, which I will be using in my scripts. One among the functions is thus:

sub parseinline { my $stag=0; my $etag=0; while ($_ =~ /<b>/g){$stag++} while ($_ =~ /<\/b>/g){$etag++} if ($stag != $etag){print "Mismatch between start and end element +for '<b>' in $ARGV[0]."} }

When I call this inside the script, it is working file:

use XML::parser; ... ... XML::parser->parsetag();

But, If I change this to the following to run for any tags mentioned during the run time, I am getting the output. I changed it as follows:

sub parseinline { my $stag=0; my $etag=0; while ($_ =~ /<$_[0]>/g){$stag++} while ($_ =~ /<\/$_[0]>/g){$etag++} if ($stag != $etag){print "Mismatch between start and end element +for '$_[0]' in $ARGV[0]."} }

and changed the script line to XML::parser->parsetag("b"); it is not working. Could someone suggest me a solution on how to use parameters when calling functions from the modules?

Replies are listed 'Best First'.
Re: Calling a function with parameters from a Module
by pajout (Curate) on Jul 18, 2008 at 13:21 UTC
    Try

    XML::parser::parsetag("b");

    If the subroutine is called by XML::parser->parsetag("b"), $_[0] contains 'XML::parser' string - it is widely used in object oriented modules... See, for instance, man perlmod for details.

    Other good practice is to name own modules with starting upper letter, so, XML::Parser would be better a little bit, it avoids clash with system modules, which are lowercased.

    Update: Added: It is possible that you already have CPAN module XML::Parser installed - it would be better to name your module XML::MyParser in this case.

Re: Calling a function with parameters from a Module
by Anonymous Monk on Jul 18, 2008 at 13:16 UTC