DEV Community

Yuki Kimoto
Yuki Kimoto

Posted on

6

Is local a bad part in Perl?

Q. Is local a bad part in Perl?

A. In my opinion, Yes. local is a bad part in Perl. If I could re-implement Perl, I will not implement local.

local $foo;
Enter fullscreen mode Exit fullscreen mode

Q. Why does local a bad part?

A. This is because the variable that is the operand of local is a global variable although at a glance it looks like a local variable in C.

int32_t foo;
Enter fullscreen mode Exit fullscreen mode

Q. What should be used instead of local?

Using my in all Perl programs is recommended strongly.

my $foo;
Enter fullscreen mode Exit fullscreen mode

This is called a lexical variable. This is strange name we don't usually hear in daily life.

Q. Does local need to be used in current days.

Q. I want to say No, but in some cases, local is used in current days. One is to save and restore predefined global variable.

{
  # Save
  local @ARGV;
}

# Restore @ARGV at the end of the scope
Enter fullscreen mode Exit fullscreen mode

Tow is to get the whole content of a file.

my $content = do { local $/; <$fh>; };
Enter fullscreen mode Exit fullscreen mode

Ugly, user experience is worse than other programing languages, but most efficient way because Perl doesn't have the function to get the whole content of a file.

The efficient is sacrificed, the following code can be written.

my $content = join('', <$fh>);
Enter fullscreen mode Exit fullscreen mode

Q. Are there ways to restore lexical variables?

Yes, using Scope::Guard.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (4)

Collapse
 
drhyde profile image
David Cantrell

I generally use it if I want to temporarily over-ride a function:


{
local *Foo::Bar::baz = sub { ... };
...
}

It's far more convenient than saving, over-riding, and restoring, and also guarantees that the restore actually happens even if an exception means that the end of the block is never reached.

Collapse
 
yukikimoto profile image
Yuki Kimoto

Thank for telling a local usage!

Collapse
 
vividsnow profile image
vividsnow

You didn't mention usage like local delete $hash{key} or local $hash{key} = 'yadayada' which is convenient and more expressive than using of guards

Collapse
 
yukikimoto profile image
Yuki Kimoto

OK. About the usage, the problem seems to be only naming.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay