http://www.perlmonks.org?node_id=994771


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

I missed one of your questions, apologies:

Significance of "my": It has a measurably distinguished behavior distinct from "our". Most notably useful in Modules.

Pay attention to the value of $TRUE in the sample:

This is t2.pm:

#!/usr/bin/perl use strict; package t2; # -------------------------------------------------------------------- +---------- # Constants (Operationally, if not technically) # -------------------------------------------------------------------- +---------- our $RET_SUCCESS = 0; our $RET_FAILURE = (-1); our $RET_FATAL = (-2); my $TRUE = 1; my $FALSE = 0; # -------------------------------------------------------------------- +---------- # version() - Returns the version number of this module # -------------------------------------------------------------------- +---------- sub version { return sprintf "0.10a"; } # -------------------------------------------------------------------- +---------- # Perl modules MUST return true # -------------------------------------------------------------------- +---------- 1; __END__

This is t3.pl:

#!/usr/bin/perl use strict; use t2; print "our: \$t2::RET_FAILURE = '$t2::RET_FAILURE'\n"; print " my: \$t2::TRUE = '$t2::TRUE'\n"; exit; __END__

Replies are listed 'Best First'.
Re^2: Use of "my" after Perl v5.14
by Rohit Jain (Sexton) on Sep 20, 2012 at 22:58 UTC
    And apologies accepted :)
Re^2: Use of "my" after Perl v5.14
by Rohit Jain (Sexton) on Sep 20, 2012 at 22:55 UTC

    I think $TRUE variable will not be visible in second file from t2 package.. Because it is declared to be private in that package.. Will it show compiler error??

      I didn't get an error. But then I am not using v5.14.
        I didn't get an error. But then I am not using v5.14.

        You're also not using warnings, which would have made Perl complain about "Use of uninitialized value $t2::TRUE in concatenation (.) or string at t3.pl line 7" (line 7 in my altered file) regardless of Perl version. Note also that
            print " my:  \$t2::TRUE        = '$t2::TRUE'\n";
        prints
            my:  $t2::TRUE        = ''
        (empty single-quotes instead of '1') with or without warnings.

        Update: Upon re-reading your post, it occurs to me that your intention may have been to point up the fact that the value of the lexical  $TRUE defined in the  .pm file is not visible in the  .pl file ("Pay attention to the value of $TRUE in the sample..."). If so, I offer my own apologies! (In any event, the use of warnings remains a valuable, albeit implicit, recommendation.)