I have some code in a file which I am loading with do. It's a mixture of code to run and subroutine definitions. I'm loading this file multiple times with different package statements in effect to declare the subs in different packages.

In some debugging messages I'm trying to use __PACKAGE__ to give the current package, but it isn't working. It's always printing 'main', even though subs are ending up in the correct package. Attached below is an example case.

Can anyone figure out what I'm missing?

#### test1-do.pl $var++; print " Point A: " . __PACKAGE__ . " $var\n"; #### test.pl #!/usr/bin/perl -w $main::var = $B::var = 5; print "Point 1: " . __PACKAGE__ . " $main::var $B::var\n"; package B; $rv = do "test1-do.pl"; die $@ if $@; die $! unless defined $rv; print "Point 2: " . __PACKAGE__ . " $main::var $B::var\n"; __END__ As I understand it the code in the do should be evaluated in the same +package it was called from. This does happen for the assignment ($var++), but it doesn't seem to g +et the right __PACKAGE__ #### Actual output Point 1: main 5 5 Point A: main 6 Point 2: B 5 6 ^^ $B::var has actually been changed #### Expected: Point 1: main 5 5 Point A: B 6 Point 2: B 5 6

--
integral

In reply to 'package' and 'do' by integral

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.