Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Variable and object scoping

by Angel (Friar)
on Nov 26, 2002 at 18:45 UTC ( [id://215896]=perlquestion: print w/replies, xml ) Need Help??

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

I have an object that I want to have a global scope. This is an instance of HTML::template. But I want the subroutines to have access and call the new method will this work or am I doing this the wrong way? Will the new call be local to sub foo or will this output correctly and if I call the new again will something break?
my $template = undef; #calls foo &foo #outputs page print $template->output; sub foo { $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters in the template $template->param(home => $ENV{HOME}); $template->param(path => $ENV{PATH}); }

Replies are listed 'Best First'.
Re: Variable and object scoping
by BUU (Prior) on Nov 26, 2002 at 18:57 UTC
    you would most probably wish to do something like this:
    #!/usr/bin/perl my $t=new HTML::Template; foo($t); print $t->output; sub foo { my $f=shift; $f->param(foo=>baz); }

      Yes, it would be better.

      Anyway, Angel's code would work correctly. Angel, just one thing: use foo(), not &foo. Just for style ;-)

      -- 
              dakkar - Mobilis in mobile
      

        use foo(), not &foo. Just for style ;-)

        And to not bypass prototypes, and to not accidentally use @_ magic, and to be consistent with method calls... :)

        bbfu
        Black flowers blossum
        Fearless on my breath

Re: Variable and object scoping
by TGI (Parson) on Nov 26, 2002 at 23:32 UTC
    To make template a global you can do this:
    use vars qw/$template/; $template = undef; #calls foo foo() #outputs page print $template->output; sub foo { $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters in the template $template->param(home => $ENV{HOME}); $template->param(path => $ENV{PATH}); }
    or
    our $template; $template = undef; #calls foo foo() #outputs page print $template->output; sub foo { $template = HTML::Template->new(filename => 'test.tmpl'); # fill in some parameters in the template $template->param(home => $ENV{HOME}); $template->param(path => $ENV{PATH}); }

    Please note that I clear $template's value, since I assume we don't want to risk values survivng there between mod_perl base invocations. You may not be programming for mod_perl, but it is worthwhile to produce compatible code.

    Maybe you should consider making a Page object and stashing a template reference there.

    package Page; use HTML::Template; sub new { my $class = shift; my $self = {}; bless $self, $class; $self->setTemplate('default.tmpl'); return $self; } sub setTemplate { my $self = shift; my $file = shift; # You really should validate the template filename here. $self{template} = HTML::Template->new(filename => $file); } sub template { my $self = shift; return $self->{template}; } sub output { my $self = shift; return $selft->template->output; } 1;
    Then your script would look like:
    use Page; my $p = Page->new; $p->setTemplate('test.tmpl'); $p->template->param(home => $ENV{HOME}); $p->template->param(path => $ENV{path}); $p->output;

    This way is nice because you don't rely on any globals, it is easy to write test scripts for your object, you can work with multiple templates at once, and you can store other interesting info in the page object.


    TGI says moo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 06:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found