Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Our local state, my, my
Your speaker for the evening● Sawyer X● Github.com/xsawyerx● Blogs.perl.org/users/sawyer_x● #dancer @ irc.perl.org
Our local state, my, my
our, local, state, my, my
Perl variables, the easy part● our is global● my is lexical
Easy part done!
Whats a global variable?● Perl code is divided to namespaces● We use package to declare them● main is the default namespac...
Global variables, examples●   our $name; # $main::name●   package My::Package;    our $name; # $My::Package::name●   say $...
Globals: done!
Whats a lexical variable?● Scoped variables● Variables that exist only in a scope!● Available scopes: block, file, eval● W...
Lexical variables, examples● { my $exists_only_here }● { my $outer; { my $inner } }● foreach my $name (@names) {     say $...
Lexical variables, pop quiz!    package Example;    my $exvar = 30;    package main;    say $exvar;●   Error or no error?
Lexical variables, pop quiz!● No error!● my is lexical● package is a namespace, not a scope● The scope here is the “file s...
Lexicals: done!
Whats a state variable?● Lexical variables with a twist!● They dont get reinitialized● sub inc {    state $myvar = 0; # de...
States: Done!
Whats a local variable?● Something that confuses people● But very simple, actually● Localizes an already existing variable...
Local variables, examples●   Slurping file content:    use autodie;    open my $fh, <, $filename;    my $content = do { lo...
Local variables, examples● No output buffering for this scope:  local $| = 1;● Disabling warnings for a scope:  {     loca...
Locals: done!
Questions?
Thank you!(yes, you can finally go home)
Upcoming SlideShare
Loading in …5
×

Our local state, my, my - Understanding Perl variables

8,802 views

Published on

This talk should explain the different ways to define Perl variables. What each one does and how they work. It will help you avoid problems with incorrect variable definition and to learn how to use variables in a smarter manner.

This was the last talk of a Tel Aviv Perl Mongers (TA.pm) group meeting.

Published in: Technology
  • Be the first to comment

Our local state, my, my - Understanding Perl variables

  1. 1. Our local state, my, my
  2. 2. Your speaker for the evening● Sawyer X● Github.com/xsawyerx● Blogs.perl.org/users/sawyer_x● #dancer @ irc.perl.org
  3. 3. Our local state, my, my
  4. 4. our, local, state, my, my
  5. 5. Perl variables, the easy part● our is global● my is lexical
  6. 6. Easy part done!
  7. 7. Whats 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)
  8. 8. Global variables, examples● our $name; # $main::name● package My::Package; our $name; # $My::Package::name● say $Moose::VERSION;
  9. 9. Globals: done!
  10. 10. Whats 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)
  11. 11. Lexical variables, examples● { my $exists_only_here }● { my $outer; { my $inner } }● foreach my $name (@names) { say $name; # okay } say $name; # error
  12. 12. Lexical variables, pop quiz! package Example; my $exvar = 30; package main; say $exvar;● Error or no error?
  13. 13. 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; }
  14. 14. Lexicals: done!
  15. 15. Whats a state variable?● Lexical variables with a twist!● They dont get reinitialized● sub inc { state $myvar = 0; # default value return ++$myvar; } say inc($_) for 1 .. 10;
  16. 16. States: Done!
  17. 17. Whats 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
  18. 18. Local variables, examples● Slurping file content: use autodie; open my $fh, <, $filename; my $content = do { local $/; <$fh> }; close $fh;
  19. 19. Local variables, examples● No output buffering for this scope: local $| = 1;● Disabling warnings for a scope: { local $^W = 0; # do something that would warn }
  20. 20. Locals: done!
  21. 21. Questions?
  22. 22. Thank you!(yes, you can finally go home)

×