Hash of Structs in a Package, is there an easier way?

Discussion in 'Perl' started by Norman Ackroyd, Jul 26, 2004.

  1. Ok, so I'm working on a Perl proggy to convert Net.Data to JSP (gotta
    love Perl.) I've got a Module that has a Class to store all relevant
    information about a Net.Data user defined function that is read in. In
    this class is a Hash of a Struct where the key is the variable name
    and the Struct itself contains the variable type and value:

    package ND2Parse::Function;

    use Carp;
    use Class::Struct;

    struct Var => {
    type => '$',
    value => '$',
    };

    sub new
    {
    my $classname=shift;
    my $functionname=shift;
    my $self={
    vars => {},
    name => $functionname
    };
    bless($self,$classname);
    return $self;
    }

    sub addvar
    {
    my $self=shift;
    my $var=shift;
    my $type=shift;
    my $value=shift;
    unless ($var)
    {
    carp "No var name passed in ->addvar(<var>,<type>,<value>)\n";
    return 0;
    }
    ${$self->{vars}}{$var}=Var->new();
    ${$self->{vars}}{$var}->type($type) if $type;
    ${$self->{vars}}{$var}->value($value) if $value;
    return 1;
    }


    My question: Is there an easier/cleaner/more readable way to reference
    and update the Struct other than what I'm doing:

    ${$self->{vars}}{$var}=Var->new();
    ${$self->{vars}}{$var}->type($type) if $type;
    ${$self->{vars}}{$var}->value($value) if $value;

    Specifically, this portion seems rather unwieldy to me:

    ${$self->{vars}}{$var}


    Any thoughts would be appreciated.

    -Norm
    Norman Ackroyd, Jul 26, 2004
    #1
    1. Advertising

  2. Norman Ackroyd

    Guest

    (Norman Ackroyd) wrote...

    > [...] this portion seems rather unwieldy to me:
    >
    > ${$self->{vars}}{$var}


    Yeah, it usually written using the shorthand:

    $self->{vars}{$var}

    This newsgroup does not exist (see FAQ). Please do not start threads here.
    , Jul 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MetalOne
    Replies:
    4
    Views:
    331
    MetalOne
    Feb 22, 2004
  2. Jim S

    There must be an easier way

    Jim S, Feb 14, 2007, in forum: HTML
    Replies:
    3
    Views:
    397
    John Hosking
    Feb 15, 2007
  3. John Henry
    Replies:
    19
    Views:
    397
    Dennis Lee Bieber
    Dec 1, 2006
  4. Replies:
    22
    Views:
    631
    Al Balmer
    Mar 12, 2006
  5. Michael W. Ryder
    Replies:
    6
    Views:
    111
    Michael W. Ryder
    Jan 21, 2008
Loading...

Share This Page