43431

Always use my; never use local?

Question:

Implementing dynamic scoping, I was using <strong><a href="http://perldoc.perl.org/functions/local.html" rel="nofollow">local</a></strong>. Then I came across <strong><a href="http://perl.plover.com/FAQs/Namespaces.html" rel="nofollow">this</a></strong> post, which says the following:

<blockquote>

Why have local at all? The answer is 90% history. Early versions of Perl only had global variables. local was very easy to implement, and was added to Perl 4 as a partial solution to the local variable problem.

</blockquote>

<strong>...never use local.</strong>

Is its use deprecated, or discouraged? If yes, what is the alternative?

Answer1:

The post you linked to is misleading, or at least incomplete. It is true that you should never use local to create a lexical variable. It doesn't do so, and that is what my is for.

However, you <em>should</em> use local when you need its actual functionality: giving a temporary value to a global variable. This is most often used for temporarily setting Perl's special variables. A classic case is something like this:

{ local $/; $entire_file = <$filehandle>; }

In order to read an entire file at once, you need to set the record separator to undefined. But you only want to do that temporarily; hence local should be used.

This is absolutely not discouraged. It is considered good Perl code.

<strong>Update:</strong> I see that the article actually has a note which qualifies its "never use local" statement. Still, I think it is misleading to make such a blanket statement. I agree with the critics to which the note is responding. The example above is quite a common, basic case, and there are several other common uses of local in that vein, as well.

I understand that a beginners' tutorial needs to keep things simple, but simple doesn't have to mean inaccurate. <em>"For now, don't worry about local; just use my"</em> would be just as clear and simple, but wouldn't mislead someone into thinking that local should <em>never</em> be used.

Answer2:

Use my to create a local variable, which is most of the time what people want.

It's fine to use local for the thing that only local does: setting a different value to a global variable, which will be restored after the current block. That is in no way deprecated. It's stable, well-supported, and is a key Perl feature. (However, it happens not to be something people tend to want any where near as often as creating a local variable. In particular many beginners never need to do this.)

What is discouraged is using local to attempt to create a local variable, because that's what my should be used for. There is never any reason to use local for this.

Answer3:

It's not bad advice, actually. As with all programming dicta, the unspoken addendum is <a href="http://senseis.xmp.net/?ProverbsDoNotApplyToWhite" rel="nofollow">"unless you really know what you're doing"</a>. There are legitimate use cases for local. But in general, if you think local is the right tool, you should reconsider before proceeding.

Recommend

  • JNA passing the equivalent of swift pointer from Java Android to C
  • Sending configuration data to websocket
  • find distinct embedded documents from the document using Spring Data Mongo?
  • transparency issues with repeated stamping of textures on an MTKView
  • C# Polly async-await: Wait for user confirmation before retry
  • iPad launch image problem
  • Pimcore where does code go
  • slide Wipe effects with ImageMagick tool and ffmpeg
  • Getting SPI temperature data from outside of class
  • Bootstrap multi select grouping dynamicaly using array
  • SVG animation not working in Chrome (using animateTransform in GWT)
  • Firebase Notification not working in background
  • Why CurrentApp.LicenseInformation.ProductLicenses does not work?
  • How to set title name of the pdf. While viewing the Document(New Tab)
  • limit python input to int
  • Qt fresh Gui application crashes on startup, faulting at QtCore4.dll
  • How to position a Widget at the bottom of a SingleChildScrollView?
  • Project Euler -Prob. #20 (Lua)
  • Can I switch the 'connected' user within an sql script that is sourced by mysql?
  • How to smoothly connect two signals in matlab [closed]
  • Wicket - getting body of markup element
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • Unable to run testNG tests from maven
  • readmore button or show/hide for php
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • How to call jQuery function in HTML returned by AJAX
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • reshape/remould data frame to create normalized bar chart and pie chart
  • How to warp text around image in iOS?
  • Bind selectedDates Aggregation for Calendar
  • ssh remote server login script
  • Capture SIGFPE from SIMD instruction
  • Call Microservice from another Microservice within Docker
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication