Perl syntax usage, use strict and local - perl

I have problem with Perl local syntax.
The code is below:
#!/usr/bin/perl -w
&outer;
sub outer {
local $lvar = 'local';
&inner;
}
sub inner {
print "lvar: $lvar\n";
}
If I run the code, it outputs:
lvar: local
But if the code is modified as below:
#!/usr/bin/perl -w
use strict;
&outer;
sub outer {
local $lvar = 'local';
&inner;
}
sub inner {
print "lvar: $lvar\n";
}
Then, errors occur:
Global symbol "$lvar" requires explicit package name at ./test.pl line 7.
Global symbol "$lvar" requires explicit package name at ./test.pl line 12.
Execution of ./test.pl aborted due to compilation errors.
I am confused here. I want to minimize the scope of $lvar, and I also want to use strict directive. But it seems they can not coexist.
Can use strict; and local coexist? Or, shall I state our $lvar?

local is almost never what you want. Use my, which declares a variable that is visible throughout (the rest of) an enclosing block.
It's unclear exactly what you want to do, but if you need to share a variable between two subroutines and protect it from access elsewhere then you can write something like this. Note that this is a closure and the value of $lvar is persistent across calls to either subroutine.
Note that you shouldn't call subroutines using the &subname syntax. That has been wrong since Perl 5 was introduced in 1994. Use subname() instead.
use strict;
use warnings;
outer();
{
my $lvar;
sub outer {
$lvar = 'local';
inner();
}
sub inner {
print "lvar: $lvar\n";
}
}

Use my to declare lexical variables. How about this rewrite?
#!/usr/bin/perl
use strict;
use warnings;
outer();
sub outer {
my $lvar = 'local';
inner($lvar);
}
sub inner {
my $lvar = shift;
print "lvar: $lvar\n";
}
Resources:
Coping with Scoping (includes discussion of my versus local).
Another good resource is the book Modern Perl. Just search for "lexical".

local does not declare/create variables — it just creates an auto-restoring backup of an existing variable — so you still have to declare $lvar. Since local only works with package variables[1], this is one of the rare occasions where one needs to use our instead of my.
outter();
sub outter {
local our $lvar = 'local';
inner();
}
sub inner {
our $lvar;
print "lvar: $lvar\n";
}
You could also declare the variable "globally".
our $lvar;
outter();
sub outter {
local $lvar = 'local';
inner();
}
sub inner {
print "lvar: $lvar\n";
}
Or just once for the two subs.
outter();
{
our $lvar;
sub outter {
local $lvar = 'local';
inner();
}
sub inner {
print "lvar: $lvar\n";
}
}
But really, local is something you should only want to use when you are forced to use package variables. When creating your own variables, you want to use my instead.
outter();
{
my $lvar;
sub outter {
$lvar = 'local';
inner();
}
sub inner {
print "lvar: $lvar\n";
}
}
It also works with hash elements and array elements of both lexical and package variables.

Related

Local variable visibility in closures vs. local `sub`s

Perl 5.18.2 accepts "local subroutines", it seems.
Example:
sub outer()
{
my $x = 'x'; # just to make a simple example
sub inner($)
{
print "${x}$_[0]\n";
}
inner('foo');
}
Without "local subroutines" I would have written:
#...
my $inner = sub ($) {
print "${x}$_[0]\n";
}
$inner->('foo');
#...
And most importantly I would consider both to be equivalent.
However the first variant does not work as Perl complains:
Variable $x is not available at ...
where ... describes the line there $x is referenced in the "local subroutine".
Who can explain this; are Perl's local subroutines fundamentally different from Pascal's local subroutines?
The term "local subroutine" in the question seems to be referring to lexical subroutines. These are private subroutines visible only within the scope (block) where they are defined, after the definition; just like private variables.
But they are defined (or pre-declared) with my or state, as my sub subname { ... }
Just writing a sub subname { ... } inside of another doesn't make it "local" (in any version of Perl), but it is compiled just as if it were written alongside that other subroutine and is placed in their package's symbol table (main:: for example).
The question mentions closure in the title and here is a comment on that
A closure in Perl is a structure in a program, normally a scalar variable, with a reference to a sub and which carries environment (variables) from its scope at its (runtime) creation. See also a perlfaq7 entry on it. Messy to explain. For example:
sub gen {
my $args = "#_";
my $cr = sub { say "Closed over: $args, my args: #_" }
return $cr;
}
my $f = gen( qw(args for gen) );
$f->("hi closed");
# Prints:
# Closed over: args for gen, my args: hi closed
The anonymous sub "closes over" the variables in scope where it's defined, in a sense that when its generating function returns its reference and goes out of scope those variables still live on, because of the existence of that reference.
Since anonymous subs are created at runtime, every time its generating function is called and lexicals in it remade so is the anon sub, so it always has access to current values.
Thus the returned reference to the anon-sub uses lexical data, which would otherwise be gone. A little piece of magic.†
Back to the question of "local" subs. If we want to introduce actual closures to the question, we'd need to return a code reference from the outer subroutine, like
sub outer {
my $x = 'x' . "#_";
return sub { say "$x #_" }
}
my $f = outer("args");
$f->( qw(code ref) ); # prints: xargs code ref
Or, per the main question, as introduced in v5.18.0 and stable from v5.26.0, we can use a named lexical (truly nested!) subroutine
sub outer {
my $x = 'x' . "#_";
my sub inner { say "$x #_" };
return \&inner;
}
In both cases my $f = outer(...); has the code reference returned from outer which correctly uses the local lexical variables ($x), with their most current values.
But we cannot use a plain named sub inside outer for a closure
sub outer {
...
sub inner { ... } # misleading, likely misguided and buggy
return \&inner; # won't work correctly
}
This inner is made at compile time and is global so any variables it uses from outer will have their values baked from when outer was called the first time. So inner will be correct only until outer is called the next time -- when the lexical environment in outer gets remade but inner doesn't. As an example I can readily find this post, and see the entry in perldiag (or add use diagnostics; to the program).
† And in my view a poor-man's object in a way, as it has functionality and data, made elsewhere at another time and which can be used with data passed to it (and both can be updated)
If you want "local" subs, you can use one of the following based on the level of backward compatibility you want:
5.26+:
my sub inner { ... }
5.18+:
use experimental qw( lexical_subs ); # Safe: Accepted in 5.26.
my sub inner { ... }
"Any" version:
local *inner = sub { ... };
However, you should not, use sub inner { ... }.
sub f { ... }
is basically the same as
BEGIN { *f = sub { ... } }
so
sub outer {
...
sub inner { ... }
...
}
is basically
BEGIN {
*outer = sub {
...
BEGIN {
*inner = sub { ... };
}
...
};
}
As you can see, inner is visible even outside of outer, so it's not "local" at all.
And as you can see, the assignment to *inner is done at compile-time, which introduces another major problem.
use strict;
use warnings;
use feature qw( say );
sub outer {
my $arg = shift;
sub inner {
say $arg;
}
inner();
}
outer( 123 );
outer( 456 );
Variable "$arg" will not stay shared at a.pl line 9.
123
123
5.18 did introduce lexical ("local") subroutines.
use strict;
use warnings;
use feature qw( say );
use experimental qw( lexical_subs ); # Safe: Accepted in 5.26.
sub outer {
my $arg = shift;
my sub inner {
say $arg;
};
inner();
}
outer( 123 );
outer( 456 );
123
456
If you need to support older versions of Perl, you can use the following:
use strict;
use warnings;
use feature qw( say );
sub outer {
my $arg = shift;
local *inner = sub {
say $arg;
};
inner();
}
outer( 123 );
outer( 456 );
123
456
I found a rather good explanation from man perldiag:
Variable "%s" is not available
(W closure) During compilation, an inner named subroutine or eval
is attempting to capture an outer lexical that is not currently
available. This can happen for one of two reasons. First, the
outer lexical may be declared in an outer anonymous subroutine
that has not yet been created. (Remember that named subs are
created at compile time, while anonymous subs are created at run-
time.) For example,
sub { my $a; sub f { $a } }
At the time that f is created, it can't capture the current value
of $a, since the anonymous subroutine hasn't been created yet.
So this would be a possible fix:
sub outer()
{
my $x = 'x'; # just to make a simple example
eval 'sub inner($)
{
print "${x}$_[0]\n";
}';
inner('foo');;
}
...while this one won't:
sub outer()
{
my $x = 'x'; # just to make a simple example
eval {
sub inner($)
{
print "${x}$_[0]\n";
}
};
inner('foo');;
}

Locally change an attribute of a class in Perl

I have come across an odd problem in one of my Perl scripts. I have a Perl object. Within a certain scope I want one of the objects attributes to be changed, but I want the attribute to be restored to it's old value after it leaves the scope.
Example:
my $object = Object->new('name' => 'Bob');
{
# I know this doesn't work, but it is the best way
# I can represent what I amd trying to do.
local $object->name('Lenny');
# Prints "Lenny"
print $object->name();
}
# Prints "Bob"
print $object->name();
Is there a way to achieve something like this?
This might not be as much encapsulation as you were asking for, but you can local-ize an attribute of a hash. This outputs "CarlLennyCarl"
sub Object::new { bless { _name => $_[1] }, $_[0] } }
sub Object::name { $_[0]->{_name} }
my $obj = Object->new("Carl");
print $obj->name;
{
local $obj->{_name} = "Lenny";
print $obj->name;
}
print $obj->name;
You could also local-ize the entire method. This also outputs "CarlLennyCarl":
sub Object::new { bless { _name => $_[1] }, $_[0] } }
sub Object::name { $_[0]->{_name} }
my $obj = Object->new("Carl");
print $obj->name;
{
local *Object::name = sub { "Lenny" };
print $obj->name;
}
print $obj->name;
I was completely misunderstanding what was occurring there. You cannot use local on subroutine calls, that is the issue you are having.
Lets use a code example from one that I know works and try to explain what eval is actually doing.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Cwd;
print getcwd() . "\n";
eval{
local #INC = ('/tmp');
require 'test.pl';
print 'local: ' . Dumper(\#INC);
};
print Dumper(\#INC);
That works because I am modifying a variable, not calling on another subroutine to modify my variable.
In order for it to work as you are expecting, you would have to create a deep copy of the object to modify in local scope or something of the sort. (which I'm pretty sure is what is occurring in the first place)
local creates scope for the given brackets, eval, OR file (your problem there)
If you were able to access the elements directly without the method call (bad practice IMHO) you would likely be able to localize the scope of that element in the object.
Example:
name.pm:
package name;
use strict;
use warnings;
{
sub new {
my ($class,$name) = #_;
my $self = bless {}, $class;
$self->{'name'} = $name if defined $name;
return $self;
}
sub name
{
my ($self,$name) = #_;
$self->{'name'} = $name if defined $name;
return $self->{'name'};
}
}
index.pl:
#!/usr/bin/perl -w
use strict;
use warnings FATAL => 'all';
use name;
my $obj = name->new('test');
print $obj->{'name'} . "\n";
{
local $obj->{'name'} = 'test2';
print $obj->{'name'} . "\n";
}
print $obj->{'name'} . "\n";

Do something after sub foo in Perl?

after foo => sub{
...
}
I just stumble upon code like above, which is called after sub foo finishes,
how does that work?
It seems it's not built-in feature of Perl,right?
It's one of the Moose method modifiers.
Method modifiers can be used to add behavior to methods without modifying the definition of those methods.
Out of curiosity, I've tried to do it myself, and got code that works to some extent (no list context, no corner cases etc.).
Perl allows for horrible things.
% perl -wle 'use After; sub foo { $_[0] * 2};
after foo => sub { print $_[0] }; foo(5); foo(6);'
10
12
Here's After.pm. Please don't ever use it.
use warnings;
use strict;
package After;
# make after() available after 'use After;'
use Exporter;
BEGIN {
our #ISA = qw(Exporter);
our #EXPORT = qw(after);
};
# prototype: bareword + sub
sub after (*&) {
my ($name, $code) = #_;
my $caller = caller; # get calling package
# fetch old sub named "name"
# note $oldcode = *{...} is not ehough
my $oldcode;
{
no strict 'refs';
$oldcode = \&{$caller."::".$name};
};
# defined new sub
my $newcode = sub {
my $ret = $oldcode->(#_); # call old sub as is
$code->($ret); # call the after sub
return $ret; # ignore aftersub's ret val
};
# plant new sub into the calling package
# avoid redefinition warnings
{
no strict 'refs';
no warnings 'redefine';
*{$caller."::".$name} = $newcode;
};
};
1;
It is not a builtin feature as others have already stated. For programs that do not use Moose, you can use Class::Method::Modifiers to get these modifiers.
If after is a predeclared subroutine, it would mean that you call that sub, with foo and an anonymous sub as arguments. It does seem a bit odd, though.
=> is equivalent to a comma, so assuming after is a sub, it would mean this:
after('foo', sub { ... });

How does the Perl 'use' syntax work?

Sample code:
m1.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
sub printab {
print "a = -$a-\n";
print "b = -$b-\n";
}
1;
m2.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
1;
test.pl
use strict;
use warnings;
use m1;
use m2;
init('hello', 'world');
printab();
Run:
$ perl test.pl
a = --
b = --
$
What happens is that the init('hello', 'world') call is mapped to m2.pm and initializes the variables ($a and $b) there.
This kind of makes sense, but what I do not understand is why those values are not available in test.pl.
Is there something fundamentally wrong that I am trying to do here? What is the correct way to use two modules with same named subroutines and variables?
How exactly does a Perl use work? It would help if someone could contrast it with C's #include directive.
In Perl, the use keyword is exactly equivalent to the following:
use Mymodule;
#is the same as
BEGIN {
require Mymodule;
Mymodule->import();
}
So if you are not defining an import routine in your code (or inheriting from Exporter), then your modules are not importing anything into test.pl
As Sinan caught, you are not declaring a package in your modules, so they are defaulting to the main package. In that case, all of your subroutines are in main, but the lexical variables (declared with my) are only scoped to the file they are declared in.
So m1 defines sub init and sub printab to which the lexicals $a and $b are in scope. But then when test.pl loads m2, the init routine is overwritten with the new definition, which is not closed around the two lexicals anymore. So it is writing to the package variables $main::a and $main::b instead of the lexicals which printab is bound to.
If you had warnings enabled (which you always should when learning), you would have been warned about the subroutine redefinition.
You should start each of your modules with:
package Some::Package::Name;
use warnings;
use strict;
and then end each module with:
1;
This is because when you use/require a module, it needs to return a true value at the end so that Perl knows it loaded properly.
First, do read perldoc perlmod.
You do not declare a namespace in either module, so everything is in the main namespace. Declare package m1; in m1.pm and package m2; in m2.pm.
At the very least, you should implement an import method (or inherit the one Exporter provides) so that programs that use modules can decide what to import from where.
It also seems to me that you are exploring around the edges of OO.
Further:
Avoid using $a and $b as variable names because it is easy to confuse them with the package variables $a and $b used by sort.
Don't use lower case module names: They are reserved for pragmata.
A minimal implementation (all in one file for testing convenience) looks like this:
package My::M1;
use strict; use warnings;
sub new { my $class = shift; bless { #_ } => $class }
sub a {
my $self = shift;
my ($v) = #_;
$self->{a} = $v if #_;
return $self->{a};
}
sub b {
my $self = shift;
my ($v) = #_;
$self->{b} = $v if #_;
return $self->{b};
}
package My::M2;
use strict; use warnings;
use base 'My::M1';
sub printtab {
my $self = shift;
for my $x (qw(a b)) {
printf "%s = -%s-\n", $x, $self->$x;
}
}
package main;
my $m = My::M2->new(a => 'hello', 'b' => 'world');
$m->printtab;
printab() is defined in the file m1.pm and only has access to the $a and $b variables that are scoped to that file. The variables $a and $b in m2.pm are scoped to that file, and they are different variables than the $a and $b in m1.pm.
init() sets the variables scoped in m2.pm (because that's the last place the &init function was defined) so it is not setting the same variables that printab() will be trying to print.

How can I code in a functional style in Perl?

How do you either:
have a sub return a sub
or
execute text as code
in Perl?
Also, how do I have an anonymous function store state?
A sub returns a sub as a coderef:
# example 1: return a sub that is defined inline.
sub foo
{
return sub {
my $this = shift;
my #other_params = #_;
do_stuff();
return $some_value;
};
}
# example 2: return a sub that is defined elsewhere.
sub bar
{
return \&foo;
}
Arbitrary text can be executed with the eval function: see the documentation at perldoc -f eval:
eval q{print "hello world!\n"};
Note that this is very dangerous if you are evaluating anything extracted from user input, and is generally a poor practice anyway as you can generally define your code in a coderef as in the earlier examples above.
You can store state with a state variable (new in perl5.10), or with a variable scoped higher than the sub itself, as a closure:
use feature 'state';
sub baz
{
state $x;
return ++$x;
}
# create a new scope so that $y is not visible to other functions in this package
{
my $y;
sub quux
{
return ++$y;
}
}
Return a subroutine reference.
Here's a simple example that creates sub refs closed over a value:
my $add_5_to = add_x_to(5);
print $add_5_to->(7), "\n";
sub add_x_to {
my $x = shift;
return sub { my $value = shift; return $x + $value; };
}
You can also work with named subs like this:
sub op {
my $name = shift;
return $op eq 'add' ? \&add : sub {};
}
sub add {
my $l = shift;
my $r = shift;
return $l + $r;
}
You can use eval with an arbitrary string, but don't do it. The code is hard to read and it restarts compilation, which slows everything down. There are a small number of cases where string eval is the best tool for the job. Any time string eval seems like a good idea, you are almost certainly better off with another approach.
Almost anything you would like to do with string eval can be achieved with closures.
Returning subs is easy by using the sub keyword. The returned sub closes over the lexical variables it uses:
#!/usr/bin/perl
use strict; use warnings;
sub mk_count_from_to {
my ($from, $to) = #_;
return sub {
return if $from > $to;
return $from ++;
};
}
my $c = mk_count_from_to(-5, 5);
while ( defined( my $n = $c->() ) ) {
print "$n\n";
}
5.10 introduced state variables.
Executing text as Perl is accomplished using eval EXPR:
the return value of EXPR is parsed and executed as if it were a little Perl program. The value of the expression (which is itself determined within scalar context) is first parsed, and if there weren't any errors, executed in the lexical context of the current Perl program, so that any variable settings or subroutine and format definitions remain afterwards. Note that the value is parsed every time the eval executes
Executing arbitrary strings will open up huge gaping security holes.
You can create anonymous subroutines and access them via a reference; this reference can of course be assigned to a scalar:
my $subref = sub { ... code ... }
or returned from another subroutine
return sub { ... code ... }
If you need to store states, you can create closures with lexical variables defined in an outer scope like:
sub create_func {
my $state;
return sub { ... code that can refer to $state ... }
}
You can run code with eval