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 1047057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:14:38+00:00 2026-05-16T16:14:38+00:00

Quoting from PerlMonks: The difference between my and local , But in real life,

  • 0

Quoting from PerlMonks: The difference between my and local,

But in real life, they work virtually the same? Yes. Sort of. So when should you use them?

Use my when you can (it’s faster than local) …

I know the lexical vs dynamic scoping difference between my and local, as discussed in this SO thread, but I am not sure why my is "faster".

What exactly do we mean when we say that a my variable is faster than a local variable in Perl?

  • 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-16T16:14:38+00:00Added an answer on May 16, 2026 at 4:14 pm

    Using local on a variable means that its previous state needs to be pushed onto a stack somewhere and restored again when the local scope is exited. Using my on a variable simply creates an entirely new variable that shadows the previous variable with the same name — the previous one is entirely untouched, and does not need to be saved anywhere. It is simply lying in wait when the local scope is exited and it is visible again.

    This pushing/popping to a stack takes resources; there’s a lot of work under the hood to ensure that this works properly. (Consider cases such as an exception being thrown while in local scope, or a signal handler being executed. I’m sure you can think of more.)

    Besides being more efficient, using my is much more logical. As a programmer introducing a local variable $foo, you don’t need to worry about the semantic reason for the previous version of $foo, what data might already be in it, or indeed if there was a $foo already created. If sometime down the road the earlier declaration of $foo is removed, your local $foo code will break, but my $foo will be perfectly happy. Be a good programmer and keep your code in well-encapsulated pieces, which means using lexical scoping as much as you can. It’s quite possible to write a large application and never need variables of package/global scope at all, especially when you use well-designed OO classes.

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

Sidebar

Related Questions

Quoting from the MSDN Link for ValueType Class In cases where it is necessary
Quoting from the cakephp Book (ver 1.3): Note that only fields of the model
Quoting MySQL INSERT manual - same goes for UPDATE: Use the keyword DEFAULT to
I understand the benefits of cleaning all data and quoting data, etc, but I'm
Quoting from the C-std section 6.7.2.1, struct s { int n; double d[]; };
quoting from The C++ Standard Library by N M Jousttis, Section 5.9 #include <
I've been quoting this segment from Sun's document for the past few days, and
I am reading the dragon book. Quoting the text from the book (3.1.4 Lexical
Quoting the code for computing the integer absolute value (abs) without branching from http://graphics.stanford.edu/~seander/bithacks.html
Quoting from Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin Perl has a useful magic filehandle called *ARGV that checks the

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.