Professional Web Applications Themes

Perl Class simple problem - PERL Beginners

Afternoon all, I'm trying to write a class for configuration values for different sites so that they can use the same conf class, then create an object and get whatever value they need. The problem I have this: I create an object and pass a value to my constructor like this: Conf->new(site=>'x'), this tells the constructor which site I want the config for, then at the end of the constructor I call $self->_init(); This passes a reference to $self ( I think ) using $self = shift; When I die out using: die %{$self}; I get the value that "x" ...

  1. #1

    Default Perl Class simple problem

    Afternoon all, I'm trying to write a class for configuration values for
    different sites so that they can use the same conf class, then create an
    object and get whatever value they need.

    The problem I have this:

    I create an object and pass a value to my constructor like this:
    Conf->new(site=>'x'), this tells the constructor which site I want the
    config for, then at the end of the constructor I call $self->_init();
    This passes a reference to $self ( I think ) using $self = shift; When I
    die out using: die %{$self};
    I get the value that "x" equals ( x => '/usr/local/apache/htdocs/x' ) I
    need to perform additional logic in $self->_init(); and what I really need
    to do is get the name, which in this case is "x" instead of the value which
    in this case is "/usr/local/apache/htdocs/x". How do I do this I'm a bit
    lost with perl OO programming :(

    Thank you for any light anyone can shed on this.

    Graeme :)

    ################ Perl Code #################

    sub new{
    my ($caller, %arg) = _;
    my $conv = {
    x => '/usr/local/apache/htdocs/x',
    y => '/usr/local/apache/htdocs/y',
    z => '/usr/local/apache/htdocs/z'
    };
    my $caller_is_obj = ref($caller);
    my $class = $caller_is_obj || $caller;
    my $self = bless {}, $class;
    $self->{_doc_root} = $conv->{$arg{site}||'x'};
    if(!exists $conv->{$arg{site}}){
    die "check for typos in yer params";
    }
    $self->_init();
    }

    sub _init{
    my $self = shift;
    die %{$self};
    }
    ##################### End of Perl Code ####################

    __________________________________________________ _______________
    Get a FREE connection, FREE modem and one month's FREE line rental, plus a
    US or European flight when you sign up for BT Broadband!
    http://www.msn.co.uk/specials/btbroadband

    Graeme Guest

  2. #2

    Default RE: Perl Class simple problem

    Graeme McLaren <mailto:com> wrote:

    [snip useful code]
    : $self->_init();

    Pass the site to _init():

    $self->_init( $arg{site} );

    : }
    :
    : sub _init{
    : my $self = shift;

    my $site = shift;
    die $site;

    : die %{$self};
    : }

    That was the short answer. The long answer might add
    "site" to the class. Here we make site available to the
    caller as a read-only method.

    package Conf;

    use Carp 'croak';

    sub new {
    my( $caller, %arg ) = _;

    my $conv = {
    x => '/usr/local/apache/htdocs/x',
    y => '/usr/local/apache/htdocs/y',
    z => '/usr/local/apache/htdocs/z'
    };

    croak
    q(new() requires 'site' parameter: ) .
    q(Conf->new( site => 'x' ))
    unless exists $conv->{ $arg{site} };

    my $class = ref( $caller ) || $caller;

    my $self = bless {
    site => $arg{site},
    _doc_root => $conv->{ $arg{site} },
    }, $class;

    $self->_init();

    return $self;
    }

    sub _init {
    my $self = shift;
    die $self->site();
    }

    sub site {
    my $self = shift;
    return $self->{site};
    }

    1;

    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328


    Charles Guest

  3. #3

    Default Re: Perl Class simple problem


    On May 29, 2004, at 5:10 AM, Graeme McLaren wrote:
    [..] 

    Well, you could start a whole lot simpler with say

    #---------------------------------
    # Our Stock Constructor
    # note: <http://www.perlmonks.org/index.pl?node_id=52089>
    sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;

    } # end of our simple new

    since that part about the

    my $caller_is_obj = ref($caller);

    is not as useful - note the url to perlmonks

    Then if you really want to go with the '_init()'
    Idea then you could grow that to

    sub new
    {
    my $class = shift;
    my $self = {};
    bless $self, $class;

    $self->_init(_);
    }

    sub _init{
    my ($caller, %arg) = _;
    my $conv = {
    x => '/usr/local/apache/htdocs/x',
    y => '/usr/local/apache/htdocs/y',
    z => '/usr/local/apache/htdocs/z'
    };

    ....

    self; # since the 'new' returns the blessed self...
    }



    ciao
    drieux

    ---

    Drieux Guest

  4. #4

    Default Perl Class simple problem: What is "die" being used for?

    Greetings!

    I'm curious about the Graeme's _init() method, reproducted below:

    sub _init {
    my $self = shift;
    die $self->site();
    }

    What is "die" used for? I thought that the effect of "die" was to nd
    the process. That's not the effect here, though. What is it doing
    here?

    Thanks very much!

    Rob Richardson






    __________________________________
    Do you Yahoo!?
    Friends. Fun. Try the all-new Yahoo! Messenger.
    http://messenger./
    Rob Guest

  5. #5

    Default Impossible error message

    Greetings!

    My program was compiling and running. It was giving me strange results
    from my print() functions. One of the list members was kind enough to
    point out my mistake. (I was putting "$user->LastName()" into a string
    to be printed.) I fixed it. Now I my program doesn't compile, and
    it's giving me an error message that makes no sense to me:

    Can't use string ("UserList") as a HASH ref while "strict refs" in use
    at UserList.pm line 66, <USERLIST> line 1.

    Here is the function containing line 66:

    sub GetUser
    {
    my $self = shift;
    my $userName = shift;
    my $user = new User;

    trace ("Getting user for login name $userName\n");

    # The following line is line 66
    if (exists $self->{'users'}->{$userName})
    {
    $user = $self->{'users'}->{$userName};
    }
    else
    {
    $user = new User;
    $user->{'loginName'} = 'unknown';
    }
    return $user;
    }

    UserList.pm contains the "UserList" package, which contains the
    UserList class and its methods. UserList maintains a list of users in
    a hash keyed by the user's login name.

    At line 66, the variable $userName contains "interrobang". "UserList"
    is not being used as a hash reference or as anything else at this
    point, as far as I know. What could be causign this error?

    This is not the first time a compile error has shown up in a previously
    running program. I need to learn PHP.






    __________________________________
    Do you Yahoo!?
    Friends. Fun. Try the all-new Yahoo! Messenger.
    http://messenger./
    Rob Guest

  6. #6

    Default Re: Impossible error message

    On Sat, May 29, 2004 at 02:39:53PM -0700, Rob Richardson wrote:
     

    Hello Earthman!
     

    This is not a compile time message, it is a run time message.
     

    Creating a new object here doesn't seem useful. you assign to $user in
    both branches below.
     

    And the message means that $self->{users} contains the string "UserList"
    rather than the hash reference you were presumably expecting.
     

    It would appear that you have assigned to $self->{users} the string
    "UserList" rather than an object of the class UserList.
     

    Well, Perl is not for everyone. You might find PHP more to your taste.
    Or Java. Or C#. Or Ruby. Or COBOL. Or Intercal. Who can say?

    Run time errors like this can show up when you do something you shouldn't
    have done. Having strict refs turned on has helped uncover it. Now you
    just need to track down where the incorrect assignment took place.

    --
    Paul Johnson - net
    http://www.pjcj.net
    Paul Guest

  7. #7

    Default RE: Perl Class simple problem: What is "die" being used for?

    Rob Richardson <mailto:com> wrote:

    : Greetings!
    :
    : I'm curious about the Graeme's _init() method, reproducted below:
    :
    : sub _init {
    : my $self = shift;
    : die $self->site();
    : }
    :
    : What is "die" used for? I thought that the effect of "die"
    : was to end the process. That's not the effect here, though.
    : What is it doing here?

    We were just testing. Trying to end the program and return
    the desired result. Assuming the test was successful, the 'die'
    statement would be substituted with the actual code (which
    hasn't been written yet).


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Charles Guest

Similar Threads

  1. defining a simple class
    By dfduiud33 in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: April 16th, 05:22 AM
  2. simple probability problem using PERL
    By Charles Lu in forum PERL Beginners
    Replies: 4
    Last Post: January 26th, 03:09 PM
  3. Replies: 1
    Last Post: November 24th, 10:11 AM
  4. #26364 [Bgs->Opn]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 24th, 10:04 AM
  5. #26364 [NEW]: initializing class in other class vars value problem
    By brightone at o2 dot pl in forum PHP Development
    Replies: 0
    Last Post: November 23rd, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139