Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: Perl Internals - references and symbol table

by shotgunefx (Parson)
on Nov 17, 2002 at 23:53 UTC ( [id://213621]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Perl Internals - references and symbol table
in thread Perl Internals - references and symbol table

Thanks Elian, I'm trying to improve me understanding of internals (Just got Embedding and Extending Perl). In the following example where do the lexicals for the bare block live then?
#!/usr/bin/perl use PadWalker qw(peek_my); use strict; use warnings; use Data::Dumper; sub peeker { my $l = shift; print Dumper(peek_my(++$l)) }; my $outmost = "outmost"; { my $inner = "outer"; print peeker(0); } print peeker(0);


-Lee

"To be civilized is to deny one's nature."

Replies are listed 'Best First'.
Re: Re: Re: Re: Perl Internals - references and symbol table
by Elian (Parson) on Nov 18, 2002 at 01:50 UTC
    They live in the pad for the anonymous sub that you don't see. Each file is, for all intents and purposes, wrapped in a big anonymous subroutine, which hold the file-scoped lexicals. I'm not sure what PadWalker's doing there--I'll have to go dig into it and see what it's looking at.
      Thanks Elian. The bare blocks have been stumping me. One of the reasons I've been so interested (and why I'm delving into internals) is that I'd like to be able to write subs that are somewhat lexically scoped for iterators and such (Without using anonymous subs, OO etc). I would like to be able to write subs that act like builtins that DWIM. I could start adding ops to the language, but I don't want to even think about writing stuff that I would need to recompile perl on a given target with my own patches.

      There are ways of doing what I want with closures and objects easily enough but I am just obsessed with doing it the way I want.

      For example ,take a hypothetical sub called elements(\@;$), that non-destructively grabs N elements of off an array lazily.

      I'd like to be able to say...
      while(my @els = elements(@array, 5) ){ # Do stuff while we have elements... }
      I can think of a couple of ways we might be able to have this DWIM.
      1. Install a localized version outside of the caller's scope or otherwise dynamically redefine it for the scope in such a way that it will be restored. (Meaning the first time it is called in a scope, the sub (which is a sub generator) redefines the subname locally and calls the newly installed version. Subsequent calls go straight to the new version until the scope ends and the original sub generator is called next time a scope is entered.
      2. Install a lexical marker in the caller's (xcv_outside?)pad so and use Padwalker to reference our current state. Now we can act lexically, though I dont' think pads are "cleared" so I don't know if this would work in all cases (like if the loop is exited prematurely and there are still elements left to iterate)
      3. Source filters. Ugh. Don't really like them. Feels wrong.


      -Lee

      "To be civilized is to deny one's nature."

      edited: Mon Nov 18 15:34:59 2002 by jeffa - s/<\/ul>/<\/ol>/

        If I understand correctly, what you really want is to have the iterator tied to the array like with each.
        { my %position; sub elements(\@;$) { my $arr = shift; my $amt = shift || 1; if(not exists $position{$arr} or $position{$arr} < $#$arr) { my ($start, $next) = ($position{$arr}, $position{$arr} += +$amt); return @$arr[$start .. $next - 1]; } else { delete $position{$arr}; return; } } }

        Makeshifts last the longest.

        I don't suppose this satisfies your criteria? It works, but would screw up if you tried to nest uses of it.

        #! perl -sw use strict; { my $next=0; sub elements(\@$) { my ($ref, $count) = @_; my $max = $#$ref; $next=0, return if $next > $max; my $first = $next; my $last = $next+$count-1; $last = $max if $max < $last; $next += $count; @{$ref}[$first .. $last]; } } my @letters = ('a'..'z'); while(my @els = elements(@letters, 5) ) { print "@els\n"; } __END__ a b c d e f g h i j k l m n o p q r s t u v w x y z

        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://213621]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found