Top Banner
Our local state, my, my
22

Our local state, my, my - Understanding Perl variables

May 08, 2015

Download

Technology

xSawyer

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Our local state, my, my - Understanding Perl variables

Our local state, my, my

Page 2: Our local state, my, my - Understanding Perl variables

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

Our local state, my, my

Page 4: Our local state, my, my - Understanding Perl variables

our, local, state, my, my

Page 5: Our local state, my, my - Understanding Perl variables

Perl variables, the easy part

● our is global● my is lexical

Page 6: Our local state, my, my - Understanding Perl variables

Easy part done!

Page 7: Our local state, my, my - Understanding Perl variables

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

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

Globals: done!

Page 10: Our local state, my, my - Understanding Perl variables

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

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

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

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

Lexicals: done!

Page 15: Our local state, my, my - Understanding Perl variables

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

States: Done!

Page 17: Our local state, my, my - Understanding Perl variables

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

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

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

Locals: done!

Page 21: Our local state, my, my - Understanding Perl variables

Questions?

Page 22: Our local state, my, my - Understanding Perl variables

Thank you!(yes, you can finally go home)