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

  • Home
  • SEARCH
  • 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 259255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:14:55+00:00 2026-05-11T22:14:55+00:00

I’m looking at some older Perl code on Perl Monks to figure out programming

  • 0

I’m looking at some older Perl code on Perl Monks to figure out programming with Win32::OLE and MS Word. Scattered throughout the code are variables with names like $MS::Word and the like, without a ‘my‘ included in their declaration. After reading a bit on Google, I understand that these are called ‘package variables’ versus ‘lexical variables’ declared using my.

My first question is ‘What are package variables good for?‘. I (think) I understand what lexical variables are, but I don’t understand the purpose of package variables or how their use differs from lexicals, so my second question would be, ‘What is the difference between lexical and package variables?‘

  • 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-05-11T22:14:56+00:00Added an answer on May 11, 2026 at 10:14 pm

    A package variable lives in a symbol table, so given its name, it’s possible to read or modify it from any other package or scope. A lexical variable’s scope is determined by the program text. The section “Private Variables via my()” in the perlsub manpage gives more detail about defining lexicals.

    Say we have the following MyModule.pm:

    package MyModule;
    
    # these are package variables
    our $Name;
    $MyModule::calls = "I do not think it means what you think it means.";
    
    # this is a lexical variable
    my $calls = 0;
    
    sub say_hello {
      ++$calls;
    
      print "Hello, $Name!\n";
    }
    
    sub num_greetings {
      $calls;
    }
    
    1;
    

    Notice that it contains a package $calls and a lexical $calls. Anyone can get to the former, but the module controls access to the latter:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    use MyModule;
    
    foreach my $name (qw/ Larry Curly Moe Shemp /) {
      $MyModule::Name = $name;
      MyModule::say_hello;
    }
    
    print MyModule::num_greetings, "\n";
    
    print "calls = $MyModule::calls\n";
    

    The program’s output is

    Hello, Larry!
    Hello, Curly!
    Hello, Moe!
    Hello, Shemp!
    4
    calls = I do not think it means what you think it means.
    

    As you can see, package variables are globals, so all the usual gotchas and advice against apply. Unless explicitly provided access, it’s impossible for code outside the MyModule package to access its lexical $calls.

    The rule of thumb is you very nearly always want to use lexicals. Perl Best Practices by Damian Conway is direct: “Never make variables part of a module’s interface” (emphasis in original).

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

Sidebar

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.