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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:48:33+00:00 2026-05-11T12:48:33+00:00

When I read through Programming Perl , 2nd Edition, Page 51, something confuses me

  • 0

When I read through Programming Perl, 2nd Edition, Page 51, something confuses me :

sub newopen {     my $path = shift;     local *FH;    #not my!     open (FH, $path) || return undef;     return *FH; }  $fh = newopen('/etc/passwd'); 

My I know, why we are not recommenced to use my? So far, I cannot see anything will go wrong if we use my().

Thanks!

  • 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. 2026-05-11T12:48:34+00:00Added an answer on May 11, 2026 at 12:48 pm

    The trite answer is that you have to use local because my *FH is a syntax error.

    The ‘right’ (but not very enlightening) answer is that you’re doing it wrong. You should be using lexical filehandles and the three-argument form of open instead.

    sub newopen {     my $path = shift;     my $fh;     open($fh, '<', $path) or do {         warn 'Can't read file '$path' [$!]\n';         return;     }     return $fh; } 

    To really answer why requires an explanation of the difference between lexical and global variables and between a variable’s scope and its duration.

    A variable’s scope is the portion of the program where its name is valid. Scope is a static property. A variable’s duration, on the other hand, is a dynamic property. Duration is the time during a program’s execution that the variable exists and holds a value.

    my declares a lexical variable. Lexical variables have a scope from the point of declaration to the end of the enclosing block (or file). You can have other variables with the same name in different scopes without conflict. (You can also re-use a name in overlapping scopes, but don’t do that.) The duration of lexical variables is managed thorugh reference counting. So long as there is at least one reference to a variable the value exists, even if the name isn’t valid within a particular scope! my also has a runtime effect — it allocates a new variable with the given name.

    local is a bit different. It operates on global variables. Global variables have a global scope (the name is valid everywhere) and a duration of the entire life of the program. What local does is make a temporary change to the value of a global variable. This is sometimes referred to as ‘dynamic scoping.’ The change starts at the point of the local declaration and persists until the end of the enclosing block after which the old value is restored. It’s important to note that the new value is not restricted to the block — it is visible everywhere (including called subroutines). Reference counting rules still apply, so you can take and keep a reference to a localized value after the change has expired.

    Back to the example: *FH is a global variable. More accurately it’s a ‘typeglob’ — a container for a set of global variables. A typeglob contains a slot for each of the basic variable types (scalar, array, hash) plus a few other things. Historically, Perl used typeglobs for storing filehandles and local-izing them helped ensure that they didn’t clobber each other. Lexical variables don’t have typeglobs which is why saying my *FH is a syntax error.

    In modern versions of Perl lexical variables can and should be used as filehandles instead. And that brings us back to the ‘right’ answer.

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I'm trying to work out the correct way to handle populating an array with
I like to study languages outside my comfort zone, but I've had a hard
I've been looking for a decent guide to Haskell for some time, but haven't
Are there any good books for a relatively new but not totally new *nix

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.