in reply to Re: Re: Re: Re: How do I make one package use a module from another package?
in thread How do I make one package use a module from another package?

The code was not broken in several ways. It was broken in a single way; it referenced (caller)[1], the calling program name, rather than (caller)[0], the calling package name. As I said before, it was a mistype. I'm sorry to have caused you so much anger over it.

The pragmas do export lexically, this is correct. You are also correct in saying that my module doesn't import them into the current lexical scope. It imports them into the current package scope. However, based on inferences from the AM poster (and conversations that I had with his non-AM identity later), he wasn't interested in the lexical behaivor of strict. He was interested in binding several modules together in a single use statement, which is really only possible with Perl5 by using the technique above, short of XS. So, in this case, you are correct in that my solution is not perfect, but then there is no perfect solution to this scenerio.

As to whether the "use strict" and "use warnings" importation works, I *REALLY* suggest you try an example. Really. It does work. If you need help writing a test case, I can assist you.

I normally do test code, but I was in a meeting and didn't have time to. I only posted because I was sure of the solution, and that I had noticed almost a dozen incorrect or unhelpful posts in the thread. Unfortunately, I made a mistake in typing the solution, and so here we are.

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: How do I make one package use a module from another package?
by bobn (Chaplain) on Jul 23, 2003 at 13:32 UTC

    The 'use strict' and 'use warnings' are absolutely NOT 'imported' into the calling scope.

    I don't need your help writing a test case. Apparently you need mine. Here it is:

    t100.pl:

    #!/usr/bin/perl use strictandwarnings; # use strict; # use warnings; # my $x; print "$x\n"; $x = '$y'; print header(), start_html(), "\n";
    strictandwarnings.pm after "fix":
    package strictandwarnings; require Exporter; our @ISA = qw(Exporter); sub import { my ($pkg) = (caller)[0]; my $current = __PACKAGE__; eval qq[ package $pkg; use strict; use warnings; use CGI qw/:standard/; package $current; ]; # optionally, if you still need import: goto \&Exporter::import; } 1;

    t100.pl as is runs to completion with no warnings.

    Un comment 'use strict' in t100.pl and it barfs with:
    Global symbol "$x" requires explicit package name at ./tst100.pl line +9. Global symbol "$x" requires explicit package name at ./tst100.pl line +11. Execution of ./tst100.pl aborted due to compilation errors.
    showing that had your code worked, the rpgram shouldn't have.

    recomment use strict and uncommmment use warnings in t100.pl and it produces:

    Use of uninitialized value in concatenation (.) or string at ./tst100. +pl line 9.
    followed by the correct output.

    Your initial code said it did 3 things. It did none of them. 3 is more than 1. Hence 'multiply broken'.

    And the OP did in fact use 'use strict' and 'use warnings' in the oringinal post. SOrry for not being a mind reader.

    Note to whoever else keeps --'ing me: Have you tried to test the code, or do you just beleive this other guy when he says it works? Update: the original version of previous was much harsher, suspect that that is what was dissed.

    --Bob Niederman, http://bob-n.com
      Please look at the code in your module, and then look at the code in my post. I updated it about a full day ago. The fact that you are unable to correctly copy and paste the code has no bearing on its correctness. In the future, please think things through before jumping to conclusions.

      And yes, I did see your previous post before you changed it. Please do grow up.

        OK, then I don't get it. I copied your code into my module and ran again with the exact same results (after deleteing "use Whateverelseyouwant;" which caused the expected errors and adding "use CGI qw/:standard/;").

        The "use strict;" and "use warnings;" do not become effective in the calling program. As lexical pragmata, I don't see how they could be.

        Am I missing something here, or are you just ignoring this issue based on CB with the OP?

        Update: I realize I'm not helping people who come here for help by flaming those who attempt to provide solutions. So I promise not to do that anymore and I'm sorry for my previous tone. I also believe, again in the interest of helping those who come here for help, that code that is posted here should either be tested, or clearly marked as untested and I have always tried to do one or the other.

        UpUpdate: 3 more ways to see that use strict and use warnings apply by only lexicial scope and not by package:

        #!/usr/bin/perl eval qq[ use strict; use warnings; ]; { use strict; use warnings; } sub doit { use strict; use warnings; } doit(); print "$x\n";

        As shown, this code produces no warnings or errors. Adding either pragma at file scope has the expected effect.

        If you've got proof to the contrary, I'd love to see it.

        --Bob Niederman, http://bob-n.com