Our local state, my, my - Understanding Perl variables
-
Upload
xsawyer -
Category
Technology
-
view
6.402 -
download
1
description
Transcript of Our local state, my, my - Understanding Perl variables
![Page 1: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/1.jpg)
Our local state, my, my
![Page 2: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/2.jpg)
Your speaker for the evening
● Sawyer X● Github.com/xsawyerx● Blogs.perl.org/users/sawyer_x● #dancer @ irc.perl.org
![Page 3: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/3.jpg)
Our local state, my, my
![Page 4: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/4.jpg)
our, local, state, my, my
![Page 5: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/5.jpg)
Perl variables, the easy part
● our is global● my is lexical
![Page 6: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/6.jpg)
Easy part done!
![Page 7: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/7.jpg)
What's a global variable?
● Perl code is divided to namespaces● We use 'package' to declare them● 'main' is the default namespace● Globals are package variables● Variables relating to that namespace● (not the same as “superglobals”)● (globals are saved in typeglobs)
![Page 8: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/8.jpg)
Global variables, examples
● our $name; # $main::name
● package My::Package;
our $name; # $My::Package::name
● say $Moose::VERSION;
![Page 9: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/9.jpg)
Globals: done!
![Page 10: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/10.jpg)
What's a lexical variable?
● Scoped variables● Variables that exist only in a scope!● Available scopes: block, file, eval● We define lexical variables with 'my' ● (they are saved in a lex pad)
![Page 11: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/11.jpg)
Lexical variables, examples
● { my $exists_only_here }● { my $outer; { my $inner } }● foreach my $name (@names) {
say $name; # okay
}
say $name; # error
![Page 12: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/12.jpg)
Lexical variables, pop quiz!
package Example;
my $exvar = 30;
package main;
say $exvar;
● Error or no error?
![Page 13: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/13.jpg)
Lexical variables, pop quiz!
● No error!● my is lexical● package is a namespace, not a scope● The scope here is the “file scope”● Here is the correct way to do it:
{ package Example; my $exvar; }
![Page 14: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/14.jpg)
Lexicals: done!
![Page 15: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/15.jpg)
What's a state variable?
● Lexical variables with a twist!● They don't get reinitialized● sub inc {
state $myvar = 0; # default value
return ++$myvar;
}
say inc($_) for 1 .. 10;
![Page 16: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/16.jpg)
States: Done!
![Page 17: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/17.jpg)
What's a local variable?
● Something that confuses people● But very simple, actually● Localizes an already existing variable● Used to temporarily override
variables instead of creating new ones● Useful with superglobals● Prevents fscking them up
![Page 18: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/18.jpg)
Local variables, examples
● Slurping file content:
use autodie;
open my $fh, '<', $filename;
my $content = do { local $/; <$fh> };
close $fh;
![Page 19: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/19.jpg)
Local variables, examples
● No output buffering for this scope:
local $| = 1;● Disabling warnings for a scope:
{
local $^W = 0;
# do something that would warn
}
![Page 20: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/20.jpg)
Locals: done!
![Page 21: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/21.jpg)
Questions?
![Page 22: Our local state, my, my - Understanding Perl variables](https://reader033.fdocuments.us/reader033/viewer/2022060107/554cb445b4c905335b8b4dea/html5/thumbnails/22.jpg)
Thank you!(yes, you can finally go home)