Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:09:26+00:00 2026-06-10T01:09:26+00:00

This is a code lifted straight from Perl Cookbook : @colors = qw(red blue

  • 0

This is a code lifted straight from Perl Cookbook:

@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs';
*$name = sub { "<FONT COLOR='$name'>@_</FONT>" };
}

It’s intention is to form 6 different subroutines with names of different colors. In the explanation part, the book reads:

These functions all seem independent, but the real code was in fact only compiled once. This technique
saves on both compile time and memory use. To create a proper closure, any variables in the anonymous
subroutine must be lexicals. That’s the reason for the my on the loop iteration variable.

What is meant by proper closure, and what will happen if the my is omitted? Plus how come a typeglob is working with a lexical variable, even though typeglobs cannot be defined for lexical variables and should throw error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:09:27+00:00Added an answer on June 10, 2026 at 1:09 am

    As others have mentioned the cookbook is using the term “proper” to refer to the fact that a subroutine is created that carries with it a variable that is from a higher lexical scope and that this variable can no longer be reached by any other means. I use the over simplified mnemonic “Access to the $color variable is ‘closed'” to remember this part of closures.

    The statement “typeglobs cannot be defined for lexical variables” misunderstands a few key points about typeglobs. It is somewhat true it you read it as “you cannot use ‘my’ to create a typeglob”. Consider the following:

    my *red = sub { 'this is red' };
    

    This will die with “syntax error near “my *red” because its trying to define a typeglob using the “my” keyword.

    However, the code from your example is not trying to do this. It is defining a typeglob which is global unless overridden. It is using the value of a lexical variable to define the name of the typeglob.

    Incidentally a typeglob can be lexically local. Consider the following:

    my $color = 'red';
    
    # create sub with the name "main::$color". Specifically "main:red"
    *$color = sub { $color };
    
    # preserve the sub we just created by storing a hard reference to it.
    my $global_sub = \&$color;
    
    {
      # create a lexically local sub with the name "main::$color".
      # this overrides "main::red" until this block ends
      local *$color = sub { "local $color" };
    
      # use our local version via a symbolic reference.
      # perl uses the value of the variable to find a
      # subroutine by name ("red") and executes it
      print &$color(), "\n";
    
      # use the global version in this scope via hard reference.
      # perl executes the value of the variable which is a CODE
      # reference.
      print &$global_sub(), "\n";
    
      # at the end of this block "main::red" goes back to being what
      # it was before we overrode it.
    }
    
    # use the global version by symbolic reference
    print &$color(), "\n";
    

    This is legal and the output will be

    local red
    red
    red
    

    Under warnings this will complain “Subroutine main::red redefined”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at this code lifted straight from the MapViewController.m file in the
I'm writing aspect-ish code more or less lifted from the chosen solution for this
So, I lifted this nice AJAX code from another site. Problem is that I
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
I've lifted a piece of code from somewhere on the internet it looks like
Consider this following code (that retrieves the response from a HTTP request and prints
We're currently migrating from NMock2 to RhinoMocks and we're having trouble replicating this code:
The following code is lifted directly from the source of the Tie::File module. What
This code below allows me to find the word error in all my files
This code is the core of a much larger script that works great in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.