Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2014-07-28 16:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (204 votes), past polls