Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessing lexical variables from inside a named sub #43

Open
zmughal opened this issue Feb 17, 2015 · 0 comments
Open

accessing lexical variables from inside a named sub #43

zmughal opened this issue Feb 17, 2015 · 0 comments

Comments

@zmughal
Copy link

zmughal commented Feb 17, 2015

Say I have the following code:

my $factor = 2;
sub mult_by_factor {
    $factor * shift;
}
say mult_by_factor(21);

This works when used in a regular Perl script, but not at the REPL. I know it might not be the best practice, but it can come in handy when testing things out.

There is are work arounds:

my $factor = 2;
*mult_by_factor =  sub {
    $factor * shift;
};
say mult_by_factor(21);

or

our $factor = 2;
sub mult_by_factor {
    $factor * shift;
}
say mult_by_factor(21);

This might be worth documenting at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant