Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Anyway, after compile-time the calls to the functions are already bound in the code and the actual symbol table entries aren't needed anymore.

    I don't buy it:

    $ perl -MO=Concise,new
    sub double {
        my $number = shift;
        return $number * 2;
    }

    sub new {
        my ($class, $number) = @_;
        bless {number => double($number)}, $class;
    }

    main::new:
    k  <1> leavesub[1 ref] K/REFC,1 ->(end)
    -     <@> lineseq KP ->k
    1     

    • Hrm, point for you. Bad wording on my side. What's the thing called methods are looked up from then? :)

      I honestly thought a delete $Foo::{bar} will remove the symbol table entry.

      Thanks again!

      --
      Ordinary morality is for ordinary people. -- Aleister Crowley
      • You misunderstood what chromatic said. He demonstrated that Perl does not have compile-time binding, contrary to what you are saying. Decompiling shows that the invocation of double in the new method is indirected via the package. If you remove the entry for double from the package, the function call will NOT work.

        Sorry, but your approach won’t work.

        Here’s a pattern for you to read carefully and chew on:

        package Foo::Bar::Internals;

        use Carp qw( croak );

        sub double { ... }

        sub Foo::Bar::new

        • So, you're saying what? I can understand that as either

          • Bad wording on my side, and it's not removed from the symbol table, but from some other table used for method lookups.
          • I'm doing voodoo, because the things I do in the test cases can't work.

          And btw: You call that a pattern? I call that a work-around :) And, just FYI, you might want to stay away from patronising phrases like "Here’s a pattern for you to read carefully and chew on." Because it really decreases my motivation to answer.

          --
          Ordinary morality is for ordinary people. -- Aleister Crowley
          • You said:

            So, you’re saying what?

            It’s bad wording on your part to say the functions are bound in the code, because they’re not; they’re always looked up from the symbol table.

            Interestingly, what you’re doing shouldn’t work – but it does! Apparently the %main::-type hashes aren’t actually an interface to the symbol table, they’re just a one-way mirror:

            sub xx {
                my $in_stbl = *main::foo{CODE} ? 1 : 0;
                my $in_hash = main->can("foo") ? 1 : 0;
                print "$in_stbl $in_hash\n";
            }

            xx();

            eval "sub foo {1}";
            xx();

            delete $main::{foo};
            xx();

            eval "sub foo {1}";
            xx();

            __END__
            0 0
            1 1
            1 0
            1 1

            In fact you won’t even get the Subroutine %s redefined warning if you have deleted the entry from the hash and redefine the routine.

            But this is clearly inconsistent with the documentation. perlmod [perl.org] says:

            The symbol table for a package happens to be stored in the hash of that name with two colons appended. […] The value in each entry of the hash is what you are referring to when you use the *name typeglob notation.

            Obviously not. Smells strongly like a bug to me (at the very least like a doc bug), not like behaviour that should be relied on. Someone alert the porters…

            You said:

            You call that a pattern? I call that a work-around :)

            Errm, that’s what patterns are: formulaic workarounds for deficiencies in a language [plover.com].

            And hey:

            • It doesn’t add yet another dependency to my code.
            • It is obvious, self-documenting. You don’t need to read module docs to understand what’s going on.
            • That it works does not contradict any Perl 5 docs.

            Besides, if you call that a workaround, then pretty nearly every way of doing OO in Perl 5 is a workaround of some sort. (More precisely, you need some form of pattern for even the simplest OOP approach in Perl 5.) We’ve just gotten so used to it by now that we don’t notice.

            You said:

            you might want to stay away from patronising phrases

            Maybe I wrote that just because the example I gave is less than obvious in my opinion. I could say you might want to stay away from reading too much into others’ utterances, but that shall be your call.

            • Interestingly, what you’re doing shouldn’t work – but it does!

              Of course. Did you honestly think I sent some code off to CPAN without even _trying_ first? :)

              Obviously not. Smells strongly like a bug to me (at the very least like a doc bug), not like behaviour that should be relied on. Someone alert the porters…

              Maybe I will post it there if nobody else does.

              Errm, that’s what patterns are []

              That might be what you (and MJD) think, but I clearly see a difference betw

              --
              Ordinary morality is for ordinary people. -- Aleister Crowley
              • Every programm is a pattern and a sum of patterns.

                No, it’s not. A pattern is a common, complex arrangement of language primitives that has to be aligned just so in order to work; which arrangement addresses a particular problem commonly encountered when using the language.

                Far from everything in a program does meets this definition. Most notably, the solution to the problem addressed by the program in its essence is not a pattern, by definition, although if it’s non-trivial you will ofte

                • Well, everything's said I guess and it would be useless to repeat it, especially since we're both drifting off in the ad-hominem area.
                  --
                  Ordinary morality is for ordinary people. -- Aleister Crowley
            • Replace delete by undef in your example, and it works. You can't delete subs from a stash in Perl 5.8, only undef them. If you use delete, the sub still exists (you can use "exists &foo" to test for it). That can be considered as a bug.