Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

package/scope question

by pbradley (Initiate)
on Nov 16, 2001 at 21:13 UTC ( #125856=perlquestion: print w/replies, xml ) Need Help??

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

hello, I'm very new to perl and have come across an aggrivating issue which I have not been able to find a solution too. I'm writing test scripts that will pump messages into the application I'm testing. My design of the scripts was to have one larger file that would include all the setup that every test will need. then I would have a test .pl file for every type of test and that perl file would just include (or require) the file. every thing works fine until in one of the tests I need to be able to change a variable in the file. My problem in the most basic form is this: the file:
package framework; use strict; my $date = "2001-11-12"; sub testcase1 { print $date; }
the file:
require ""; $framework::date = "2001-12-05"; &framework::testcase1; $framework::date = "2001-11-15"; &framework::testcase1;
the output after running this test is 2001-11-122001-11-12. the date is not being changed like I thought it would be. Does anybody know of an easy way for me to access and change frameworks $date variable from within the Any help would be greatly appreciated. thanks.

Replies are listed 'Best First'.
Re: package/scope question
by davorg (Chancellor) on Nov 16, 2001 at 21:42 UTC

    Inside the framework package you declare $date as a lexical variable. This means that the variable is only visible inside the file.

    Inside your main package, you try to access a package variable called $framework::date. This is a completely separate variable to the lexical that you declared previously. This is why updating it has no effect on the output from testcase1 - you're updating the wrong variable.

    Easiest solution is to replace the lexical variable declaration with a package variable declaration in like this:

    use vars '$date';

    (or our $date if you're using Perl 5.6.x)


    "The first rule of Perl club is you don't talk about Perl club."

(jeffa) Re: package/scope question
by jeffa (Bishop) on Nov 16, 2001 at 21:22 UTC
    How about using our instead of my:
    use strict; require ''; $framework::date = "2001-12-05"; &framework::testcase1; $framework::date = "2001-11-15"; &framework::testcase1; ------------------------------------------ package framework; use strict; our $date = "2001-11-12"; sub testcase1 { print "$date\n"; } 1; # don't forget to return true value!!
    I don't use our very much, but it seems applicable here. Personally, i would opt for the more standard OO approach:
    use strict; require ''; my $fw = framework->new(); $fw->set_date("2001-12-05"); $fw->testcase1(); $fw->set_date("2001-11-15"); $fw->testcase1(); ----------------------------------------- package framework; sub new { my $class = shift; my $self = { date => '2001-11-12' }; return bless $self, $class; } sub set_date { my ($self,$date) = @_; shift->{'date'} = $date; } sub testcase1 { print shift->{'date'}, "\n"; } 1;
    Perhaps Test::Simple or Test::Unit would be helpful to you as well.

    (this node updated with minor tweaks and such...)


    (the triplet paradiddle)
      when I try the our approach I get warnings and errors: use of reserved word our depricated at line.... Variable $date is not imported at framework line.. Global symbol $date requires explicit package name at ... when I try the OO approach, I get the following error: did not return a true value at line 2. one thing I should mention that may make a difference is that I am not putting any of my files in the perl lib path. (and I hope very much to avoid doing this as the setup for the test is already complex enough without having to do this)
        Sounds like you are using an older version of Perl - try perl -v to see which version you are using.

        Like i said, i personally stick with the OO style - you need to simply stick 1; as the last line in


        (the triplet paradiddle)
        my bad I forgot to put the return value in.. thanks.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2021-09-22 09:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found