Changeing Scope of a Sub Reference 
Author Message
 Changeing Scope of a Sub Reference

Hello beautiful Perl people.

I have this big lengthy program written that needs this ability.  Instead of
posting a bunch of meaningless code, here's a sample (conceptually) of what
I am trying to do:

use strict;
my $sub_ref = sub{ print $i; };
sub run_ref { my $i = 'Hello!'; &{$sub_ref}; }
run_ref();

Right now this produces the error:

Global symbol "$i" requires explicit package name at scope.pl line 3.
Execution of scope.pl aborted due to compilation errors.

Which, as from what I understand makes perfect sense.  Despite seeing the
logic in this, I want to get around this and scope a sub reference in to
whatever black/package I want to without having to re-reference the code.

Does my question make sense?

Any ideas?

Will I have to play with the internals of Perl?  (aaahahhhh!)

Thanks for any help,

Aran



Thu, 08 Sep 2005 06:51:25 GMT  
 Changeing Scope of a Sub Reference

Quote:

> I have this big lengthy program written that needs this ability.
> Instead of posting a bunch of meaningless code, here's a sample
> (conceptually) of what I am trying to do:

> use strict;
> my $sub_ref = sub{ print $i; };
> sub run_ref { my $i = 'Hello!'; &{$sub_ref}; }
> run_ref();

> Right now this produces the error:

> Global symbol "$i" requires explicit package name at scope.pl line 3.
> Execution of scope.pl aborted due to compilation errors.

So pass arguments.  Globals are a pain to manage anyway.

  #!/usr/bin/perl

  use warnings;
  use strict;

  my $sub_ref = sub { my $i = shift; print $i; };
  sub run_ref { $sub_ref->('Hello!'); }
  run_ref();

Steve
--
Steven Smolinski => http://arbiter.ca/
GnuPG Public Key => http://arbiter.ca/steves_public_key.txt
                 => or email me with 'auto-key' in the subject.
Key Fingerprint  => 08C8 6481 3A7B 2A1C 7C26  A5FC 1A1B 66AB F637 495D



Thu, 08 Sep 2005 07:47:08 GMT  
 Changeing Scope of a Sub Reference


Quote:

> > I have this big lengthy program written that needs this ability.
> > Instead of posting a bunch of meaningless code, here's a sample
> > (conceptually) of what I am trying to do:
[snip]

> So pass arguments.  Globals are a pain to manage anyway.

Steven,

Passing arguments is fine, and something any perl {*filter*} understands.  I'm
actually looking for something a little more in depth, as I described in my
original question.

As you said, globals are a pain, and thats exactly where my question is
coming from.

Thank you anyways.

Quote:
>   #!/usr/bin/perl

>   use warnings;
>   use strict;

>   my $sub_ref = sub { my $i = shift; print $i; };
>   sub run_ref { $sub_ref->('Hello!'); }
>   run_ref();

> Steve
> --
> Steven Smolinski => http://www.*-*-*.com/
> GnuPG Public Key => http://www.*-*-*.com/
>                  => or email me with 'auto-key' in the subject.
> Key Fingerprint  => 08C8 6481 3A7B 2A1C 7C26  A5FC 1A1B 66AB F637 495D



Thu, 08 Sep 2005 08:05:47 GMT  
 Changeing Scope of a Sub Reference

Quote:

> Passing arguments is fine, and something any perl {*filter*} understands.
> I'm actually looking for something a little more in depth, as I
> described in my original question.

Your original question needs more depth.  Here it is:

Quote:
> I have this big lengthy program written that needs this ability.
> Instead of posting a bunch of meaningless code, here's a sample
> (conceptually) of what I am trying to do:
> use strict;
> my $sub_ref = sub{ print $i; };
> sub run_ref { my $i = 'Hello!'; &{$sub_ref}; }
> run_ref();
[...]
> I want to get around this [scope error] and scope a sub reference in
> to whatever black/package I want to without having to re-reference the
> code.

That's not clear.  Is your question how to reference a scalar (that's
what a sub reference is) in another package?

Is your question how to reference a subroutine in another package?  

Is your question how to access the $i variable from another package?

Both your $i and your $sub_ref are lexicals anyway, not package
variables, so they aren't even in the symbol table.

You also only show one package.  So everything you reference will be
either lexical or in the same package.  And lexical variables in blocks
are not referencable outside of the block, unless you explicitly return
a ref to them.

So what are you trying to do?

Steve
--
Steven Smolinski => http://www.*-*-*.com/
GnuPG Public Key => http://www.*-*-*.com/
                 => or email me with 'auto-key' in the subject.
Key Fingerprint  => 08C8 6481 3A7B 2A1C 7C26  A5FC 1A1B 66AB F637 495D



Thu, 08 Sep 2005 09:04:41 GMT  
 Changeing Scope of a Sub Reference

Quote:



>> So pass arguments.  Globals are a pain to manage anyway.

> Passing arguments is fine, and something any perl {*filter*}
> understands.

Intriguing.

Quote:
> I'm actually looking for something a little more in depth,
> as I described in my original question.

But you can't rebind a closure.  

You might be able to kluge something together with string
eval, but until you tell us why you want an "in-depth"
solution the obvious approach -- passing arguments -- will
look like the way to go.

--
Steve

s||Just another Perl {*filter*}||tr|Nm|tr||s|ph|||print



Thu, 08 Sep 2005 09:21:03 GMT  
 Changeing Scope of a Sub Reference


Quote:



> >> So pass arguments.  Globals are a pain to manage anyway.

[snip]
> But you can't rebind a closure.

Oh ok, I think thats the answer I was looking for.  Well, I guess I'm just
going to be passing chunks of code around and eval() them, as you suggested,
so that they have scope (or closure, not sure if my terminology is correct)
in the right places.  Kinda inefficient tho.  Argh!   :-)

Thanks,

Aran

Quote:
> You might be able to kluge something together with string
> eval, but until you tell us why you want an "in-depth"
> solution the obvious approach -- passing arguments -- will
> look like the way to go.

> --
> Steve

> s||Just another Perl {*filter*}||tr|Nm|tr||s|ph|||print



Thu, 08 Sep 2005 16:43:51 GMT  
 Changeing Scope of a Sub Reference


Quote:

> > Passing arguments is fine, and something any perl {*filter*} understands.
> > I'm actually looking for something a little more in depth, as I
> > described in my original question.

> Your original question needs more depth.  Here it is:

[snip]

> That's not clear.  Is your question how to reference a scalar (that's
> what a sub reference is) in another package?

Not really.  Actually, I think my problem was terminology.  I want to change
the closure of a subroutine reference to wherever I want whenever I want.
Sounds like, from what Steve Grazzini posted, that thats not a possiblity.
:(  In any case, I'm just going to be passing around chunks of code and
eval()ing them all over the place to get the scope I'm
looking for.

So, unless you know of some magic, or if I'm just being dumb...

Thanks,
Aran

PS: I answered all your questions below as best I could.

Quote:
> Is your question how to reference a subroutine in another package?

Somewhat, I'm trying to change the closure location of a code reference at
run-time without having to re-create the reference (i.e. do an eval()( every
time I want to run the code.

Quote:
> Is your question how to access the $i variable from another package?

Nope.  I'm more trying to figure out how to create a lexical, and changing
my code ref's closure to wherever I want to make that lexical and other
things in-scope to the code ref when I call it.  And, doing all that without
passing arguments or messing with package variables.

Quote:
> Both your $i and your $sub_ref are lexicals anyway, not package
> variables, so they aren't even in the symbol table.

Right.

Quote:
> You also only show one package.  So everything you reference will be
> either lexical or in the same package.  And lexical variables in blocks
> are not referencable outside of the block, unless you explicitly return
> a ref to them.

Agreed.
Quote:
> So what are you trying to do?

> Steve
> --
> Steven Smolinski => http://www.*-*-*.com/
> GnuPG Public Key => http://www.*-*-*.com/
>                  => or email me with 'auto-key' in the subject.
> Key Fingerprint  => 08C8 6481 3A7B 2A1C 7C26  A5FC 1A1B 66AB F637 495D



Thu, 08 Sep 2005 16:56:47 GMT  
 Changeing Scope of a Sub Reference

Quote:

> Hello beautiful Perl people.

> I have this big lengthy program written that needs this ability.
> Instead of posting a bunch of meaningless code, here's a sample
> (conceptually) of what I am trying to do:

> use strict;
> my $sub_ref = sub{ print $i; };
> sub run_ref { my $i = 'Hello!'; &{$sub_ref}; }
> run_ref();

   use strict;
   use PadWalker qw(peek_my); # from CPAN.
   my $sub_ref = sub { print ${peek_my(1)->{'$i'}}; };
   sub run_ref { my $i = 'Hello!'; &$sub_ref(); }
   run_ref();

[untested]

--





Fri, 09 Sep 2005 03:44:19 GMT  
 Changeing Scope of a Sub Reference

Quote:

> > Hello beautiful Perl people.

> > I have this big lengthy program written that needs this ability.
> > Instead of posting a bunch of meaningless code, here's a sample
> > (conceptually) of what I am trying to do:

> > use strict;
> > my $sub_ref = sub{ print $i; };
> > sub run_ref { my $i = 'Hello!'; &{$sub_ref}; }
> > run_ref();

>    use strict;
>    use PadWalker qw(peek_my); # from CPAN.
>    my $sub_ref = sub { print ${peek_my(1)->{'$i'}}; };
>    sub run_ref { my $i = 'Hello!'; &$sub_ref(); }
>    run_ref();

> [untested]

AWESOME!  Thanks benjy!  :)

I'll look in to using this, or perhaps using the underlying concept.  In any
case this is what I want... I think.

Aran

- Show quoted text -



Fri, 09 Sep 2005 05:19:20 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Outer scope of a sub inside a sub

2. scope for recursive sub

3. References, Clients, and scope

4. references and scope

5. Problem: MySQL, variable scope, references

6. changeing enters into <br>'s

7. references and anon subs in pTk Help!!!

8. Sub references?

9. sub references and prototypes - getting the commas right

10. Passing a hash by reference to a sub

11. reference as a sub name

12. sub reference and name

 

 
Powered by phpBB® Forum Software