Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re: Use of "my" after Perl v5.14 by tobyink
in thread Use of "my" after Perl v5.14 by Rohit Jain

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (3)
    As of 2018-05-22 02:55 GMT
    Find Nodes?
      Voting Booth?