Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Use of "my" after Perl v5.14

by tobyink (Abbot)
on Sep 20, 2012 at 22:36 UTC ( #994770=note: print w/ replies, xml ) Need Help??


in reply to Use of "my" after Perl v5.14

my is not something new in Perl 5.14. It was introduced in Perl 5.0 almost 18 years ago, and is generally the recommended way of declaring variables. When you don't use my then you should have a damned good reason not to.

Perl traditionally allowed you to use variables without declaring them. The strict pragma (also introduced in Perl 5.0) can be employed to force the programmer to declare all their variables. Most Perl programmers use strict routinely. Perl 5.14 made the version declaration use v5.14 also automatically use strict mostly to save everyone some typing.

You have read wrong. my is not for "creating private variables in a subroutine"... or at least it's not exclusively for that. Try this example, which doesn't even define any subs:

#!/usr/bin/env perl use v5.14; { my $var = "Hello world"; say $var; } say $var;

From this example, I hope you can see that lexical variables (those declared with my are not private to a sub; they are private to a lexical scope - that is, an area between curly braces. (Each whole Perl module/script is also a scope in itself.) Thus it's quite easy to declare a variable which can be seen outside a sub:

use v5.14; my $global; sub set { $global = shift; } set("Hello world"); say $global;

The $global variable above is not fully global. It is available throughout the file, but other files cannot see it. (Say, in a situation where one module is trying to read a variable in another module.) This is often what you want.

Perl's true globals are package variables. These need to contain "::". For example:

$Foo::Bar::monkey = 'capuchin'; @Foo::Bar::monkies = qw( capuchin howler spider ); %Foo::Bar::monkies = ( capuchin => 1, howler => 2, spider => 3, ); $::Hello = "World"; # is an alias for $main::Hello

Perl gives you a shortcut for package variables. If you're in package Foo::Bar, then you can use our $monkey to make $monkey act as an alias for $Foo::Bar::monkey.

{ package Foo::Bar; our $monkey; $monkey = 'capuchin'; } { package main; use feature 'say'; say $monkey; # blank line (it's undefined) say $Foo::Bar::monkey; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Use of "my" after Perl v5.14
Select or Download Code
Re^2: Use of "my" after Perl v5.14
by Rohit Jain (Sexton) on Sep 20, 2012 at 22:57 UTC
    Thanks tobyink for such a elaborated and nice explanation..

      I suppose it's also worth mentioning that my and our have two other friends: local and state.

      local sets a new, temporary value for a package variable, which expires at the end of the current scope, with the variable's original value being restored.

      In this example, bar() sets $::hello temporarily to "Goodbye" and this new value is visible to foo(), but after bar() has finished running, the original value of "Hello" is restored again.

      use v5.14; sub foo { say $::hello; } sub bar { local $::hello = "Goodbye"; foo(); } $::hello = "Hello"; bar(); say $::hello;

      And state can be used to create a variable which keeps its state, not being re-initialised.

      use v5.14; sub counter { state $count = 1; say $count++; } counter(); counter(); counter(); counter();
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Then I suspect, local works just like control variable in foreach loop, where I read that, if the control variable is already declared before the loop, then its value will be restored after the foreach loop ends.

        E.g: -

        my $packageVar = 5; foreach $packageVar (@someArr) { # Some code } print $packageVar; # Will print 5;

        *EDIT: - Didn't saw this difference.. The "local" state of variable is visible accross multiple subroutines (in fact at all places in that file I guess) untill we get out of scope of that subroutine.. That is not the case with foreach loop..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994770]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-10-23 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (124 votes), past polls