How does this bizarre script work? - Page 2

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Re: How does this bizarre script work?

On 2/24/2015 1:46 PM, Rainer Weikusat wrote:

Quoted text here. Click to load it

Ok, that meshes with what I was able to determine while traveling to work
on a bus today:

#! /usr/bin/perl

use v5.14;
no strict;
no warnings;

*main::pi = .1415926535;

say ( "pi SCALAR = ", $} ) if defined *main::pi;
say ( "pi ARRAY  = ", @ } ) if defined *main::pi;
say ( "pi HASH   = ", %  } ) if defined *main::pi;
say ( "pi CODE   = ", &  } ) if defined *main::pi;

*main::e = sub();

say ( "e SCALAR  = ", $ } ) if defined *main::e;
say ( "e ARRAY   = ", @  } ) if defined *main::e;
say ( "e HASH    = ", %   } ) if defined *main::e;
say ( "e CODE    = ", &   } ) if defined *main::e;

say ( "pi * e = ", $pi * &e );


pi SCALAR = 3.1415926535
e CODE    = 2.71828182845905
pi * e = 8.5397342224295


So the pi is a scalar, the e is code, and a mysterious defined but
uninitialized scalar $e also exists.

Seems that I misread the paragraph in The Camel Book that deals with
these two kinds of "constant" (SCALAR ref to string/number, and CODE
ref to subroutine returning string/number) on page 391 (Ch 10 "Packages",
section on "Symbol Tables"). The wording is somewhat fuzzy.

Quoted text here. Click to load it

Looks to me like that's doing pretty much the same thing as
*main::A = .23;
I believe both versions put a ref to 1.23 into the 'SCALAR' slot
in the 'A' typeglob in the 'main' package, no?

Let me test that......

HMMMMMM....  no...... actually, it's storing the 1.23 in
So it's a subroutine. Weird. Why is it a subroutine?

Quoted text here. Click to load it

That much I've learned in the past few days in my reading.

Quoted text here. Click to load it

Ah, I see you're electing to B::Concise here. :-)  Perl internal OP
codes, eh? I can't understand most of that, but I see we enter,
then do some stuff involving 1.23, then make like a tree and leave.
I'm not fully getting what "inlining" would mean here. That the
number 1.23 is substituted at any point in the code which calls A() ?

Robbie Hatley
Midway City, CA, USA
perl -le 'print "4o6e7o4f0w5llc7m"'

Re: How does this bizarre script work?

Quoted text here. Click to load it


Quoted text here. Click to load it

It actually isn't (I couldn't resist the temptation using 3.14 instead
of 2.72):

[rw@doppelsaurus]/tmp#perl -MDevel::Peek -le 'BEGIN { $:: = .14; } print e + 1; Dump($::)'
SV = IV(0x623940) at 0x623950
  REFCNT = 1
  RV = 0x623908
  SV = PVNV(0x605ed0) at 0x623908
    REFCNT = 1
    IV = 3
    NV = 3.14
    PV = 0

This symbol table entry doesn't even store a glob but just a reference
to a scalar and for as long as no other properties of e are accessed, it
remains in this way, otherwise, the reference to a scalar is replaced
with a GV (could be referred to as 'reference to a glob) and a
subroutine returning the value is created instead:

[rw@doppelsaurus]/tmp#perl -MDevel::Peek -le 'BEGIN { $:: = .14; } $s = *e; print $s->() + 1; Dump($::)'
SV = PVGV(0x65f570) at 0x623980
  REFCNT = 2
  MAGIC = 0x62b2f0
    MG_VIRTUAL = &PL_vtbl_backref
    MG_TYPE = PERL_MAGIC_backref(<)
    MG_OBJ = 0x623968
  NAME = "e"
  GvSTASH = 0x606800    "main"
  GP = 0x62b680
    SV = 0x0
    REFCNT = 1
    IO = 0x0
    FORM = 0x0  
    AV = 0x0
    HV = 0x0
    CV = 0x623968
    CVGEN = 0x0
    LINE = 1
    FILE = "-e"
    FLAGS = 0xa
    EGV = 0x623980      "e"
Quoted text here. Click to load it

That's more or less accurate. The actual procedure is more like this:
The compiler builds an op-tree from the source code. In case some
subtree of this op-tree can be evaluated at compile time, this is done
and the subtree replaced with a single op pusing the resulting value
onto the stack.

[rw@doppelsaurus]/tmp#perl -MO=Concise,-exec -e 'print 1 + 1.45 / length("AAA")'
1  <0> enter  
2  <;> nextstate(main 1 -e:1) v:{
3  <0> pushmark s
4  <$> const[NV 1.48333333333333] s

At least in theory as this doesn't work for all operators (eg,
apparently, perl 5.14.2 can't deal with x operators at compile time).

Re: How does this bizarre script work?

On 24/02/15 20:58, Robbie Hatley wrote:
Quoted text here. Click to load it

If you're going to write stuff as obscure as this, you really should be  
coding in APL.


Henry Law            Manchester, England

Site Timeline