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

changing package variables?

by LameNerd (Hermit)
on Mar 04, 2003 at 23:44 UTC ( [id://240486]=perlquestion: print w/replies, xml ) Need Help??

LameNerd has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I have this package:
package MYPACKAGE; use Exporter; use vars qw / $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS /; $VERSION = 1.0; @ISA = qw/ Exporter /; @EXPORT = (); @EXPORT_OK = qw / sub1 /; %EXPORT_TAGS = ( DEFAULT => [ qw / sub1 / ] ); use strict; my $var1 = "DEFUALT I WANT TO CHANGE"; sub sub1 { print "var1 = $var1\n"; } 1;
Now I use this packe in this script like so:
#!/usr/bin/perl -w use strict; use lib ('.'); use MYPACKAGE qw / sub1 /; use vars qw / $var1 /; $var1="PRINT THIS"; sub1();
The output is:
var1 = DEFUALT I WANT TO CHANGE
I was hoping that the output would be
var1 = PRINT THIS
How come the value of $var1 didn't change when I did $var1="PRINT THIS";
in my script?

Replies are listed 'Best First'.
Re: changing package variables?
by tachyon (Chancellor) on Mar 04, 2003 at 23:58 UTC

    Remove the my declaration and use vars to declare $var1 in your package and export $var1 and it will work.

    use strict; use lib ('.'); use MYPACKAGE qw / sub1 $var1 /; use vars qw / $var1 /; sub1(); $var1="PRINT THIS"; sub1(); package MYPACKAGE; use Exporter; use vars qw / @ISA @EXPORT @EXPORT_OK /; @ISA = qw/ Exporter /; @EXPORT = (); @EXPORT_OK = (qw/sub1 $var1/);; use strict; use vars '$var1'; $var1 = "DEFUALT I WANT TO CHANGE"; sub sub1 { print "var1 = $var1\n" } 1; __DATA__ var1 = DEFUALT I WANT TO CHANGE var1 = PRINT THIS

    The OO way to do it is with get and set methods. The advantage of this is that a my var can not be accessed* outside the package so the only way to get/set the value is using the accessor methods. These can be useful as you can ensure that only valid values are set.

    use strict; use lib ('.'); use MYPACKAGE qw / set_var get_var/; print get_var(), "\n"; set_var('Some new val'); print get_var(), "\n"; package MYPACKAGE; use Exporter; use vars qw / @ISA @EXPORT @EXPORT_OK /; @ISA = qw/ Exporter /; @EXPORT = (); @EXPORT_OK = (qw/set_var get_var/);; use strict; my $var1 = "DEFUALT I WANT TO CHANGE"; sub set_var { $var1 = shift } sub get_var { return $var1 } 1;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      That's what I was looking for! Thanks most wise monk!
Re: changing package variables?
by jasonk (Parson) on Mar 04, 2003 at 23:50 UTC

    Because the variable in the package is declared within the scope of that package. So basically you now have two variables:

    $MYPACKAGE::var1 contains 'DEFAULT I WANT TO CHANGE'
    $main::var1 contains 'PRINT THIS'.

    You need to peruse perlvar and the docs for the package command.

      Thanks!
      I was under the mistaken impression that doing
        use MYPACKAGE qw / sub1 /;
        use vars qw / $var1 /;
      
      Would give me instance of $MYPACKAGE::var1 in my main package scope.
      So what's the diffence between
        use vars qw / $var1 /;
      
      and
        my $var1
      
      ?????? What was I thinking?

        In a nutshell :my does not declare a package variable. use vars does. There's a wealth of information on this topic in the Tutorials section of the site (subsection "variables"); I'll plug my Variable Scoping in Perl: the basics and Dominus' Coping With Scoping specifically.

        update adjusted the link to CwS to point directly to it.

        If not P, what? Q maybe?
        "Sidney Morgenbesser"

Re: changing package variables?
by BrowserUk (Patriarch) on Mar 05, 2003 at 00:14 UTC

    You might also want to look into our vars and broquaint's Lexical scoping like a fox.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://240486]
Approved by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-26 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found