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

perlfunc index name conflict

by dpath2o (Acolyte)
on Sep 23, 2012 at 22:13 UTC ( #995263=perlquestion: print w/replies, xml ) Need Help??
dpath2o has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This is somewhat basic so I apologise if this has been answered somewhere, but I had difficulty in finding the solution using some simple searches. Regardless, my problem is that I'm having a conflict in using 'PDL::index' and Perl's 'index'. Basically I have a script that is using predominantly PDL coding but I'd like to take advantage of Perl's index function, something like this:

 use PDL; ... (a bunch of PDL lines of code) ...; $tmpsub = substr( $perlstr , index( $perlstr , /myregex/ ) , $position );

The problem with the above line is that it attempts to use PDL::Index. I have tried to the following as a solution:

 use PDL; ... (a bunch of PDL lines of code) ...; $tmpsub = substr( $perlstr , $main::index( $perlstr , /myregex/ ) , $position );

But this fails because of my lack of understanding. Basically I would like to use Perl's main 'index' function in the above line and not PDL's 'index'. Hopefully this is a straightforward solution, and that I will not receive too much bludgeoning for having asked such a trivial question. Kind Regards, Dan

Replies are listed 'Best First'.
Re: perlfunc index name conflict
by ikegami (Pope) on Sep 24, 2012 at 01:10 UTC
    BEGIN { delete $main::{index} }
    will "unimport" index from main so you don't have to use CORE:: everywhere.
Re: perlfunc index name conflict
by chuckbutler (Prior) on Sep 23, 2012 at 22:50 UTC

    If you would like to use supplied Perl index function, qualify it CORE::index.

    Also, the second parameter should be a substring to match, not a regular expression. See index.

    Good luck. -c

      Thank you CORE::index has solved my problem.

      the second parameter should be a substring to match, not a regular expression

      Well, /(\w+)/ will return a substring

        In scalar context such as supplied by index, a successfully matching regex will return 1, which will be stringized to '1' for the substring for which to search:

        >perl -wMstrict -le "my $str = 'abc1def'; $_ = 'xyz'; ;; print 'index: ', index $str, /(\w+)/; print qq{captured: '$1'}; " index: 3 captured: 'xyz'

        An unsuccessful regex match returns the empty string in scalar context.

Re: perlfunc index name conflict
by Anonymous Monk on Sep 23, 2012 at 22:40 UTC
    Well, CORE::index, but FWIW, I doubt PDL exports an index function
      I doubt PDL exports an index function

      I expected the same ... but, apparently, not so:
      use warnings; use strict; use warnings; use PDL; print index('hello world', 'lo w'), "\n";
      This outputs:
      Argument "hello world" isn't numeric in subroutine entry at lin +e 7. Argument "lo w" isn't numeric in subroutine entry at line 7. 0
      If we comment out the "use PDL;" we then get the expected output of "3".
      As Anonymous Monk said, one solution is to use CORE::index() instead of index().
      Another solution is to use PDL(); instead of use PDL;

      But it's a bit rude of PDL to be exporting this function by default, isn't it ?

      Update: The PDL documentation states:
      PDL includes about a dozen perl modules that form the core of the language, plus additional modules that add further functionality. The perl module "PDL" loads all of the core modules automatically, making their functions available in the current perl namespace.
      Exports "use PDL;" exports a large number of routines into the calling namespace. If you want to avoid namespace pollution, you must instead "use PDL::Lite", and include any additional modules explicitly.
      So this *is* documented ... and it seems that the recommended solution is to "use PDL::Lite;".


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995263]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2018-07-23 02:20 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.