How to declare a variable???

Discussion in 'Perl Misc' started by leiji22@yahoo.com, Dec 19, 2005.

  1. Guest

    I'd like to define a large hash at the end of the script file. The hash
    contains some parameters.

    But I need to use this hash variable at the beginning of the script
    file.

    The hash is very long, so it's impossible for me to put it at the
    beginning of the script.

    Do I need to declare it at the beginning of the file? If not, does it
    matter where in the file I put the variable definition?

    Thanks for any help!
     
    , Dec 19, 2005
    #1
    1. Advertising

  2. Scott Bryce Guest

    wrote:
    > I'd like to define a large hash at the end of the script file. The hash
    > contains some parameters.
    >
    > But I need to use this hash variable at the beginning of the script
    > file.
    >
    > The hash is very long, so it's impossible for me to put it at the
    > beginning of the script.
    >
    > Do I need to declare it at the beginning of the file? If not, does it
    > matter where in the file I put the variable definition?
    >
    > Thanks for any help!
    >


    I'm not sure exactly what you are trying to accomplish. It looks to me
    like you want to define a large data structure near the beginning of
    your algorithm, but want to place the code that defines the data
    structure near the end of your script for esthetic reasons.


    my $large_data_structure = get_large_data_structure();

    # Do something with the large data structure here
    # $large_data_structure contains a reference to a hash.

    sub get_large_data_structure {

    my %large_data_structure = (

    # Large data structure defined here.

    }

    return \%large_data_structure;
    }
     
    Scott Bryce, Dec 19, 2005
    #2
    1. Advertising

  3. Scott Bryce Guest

    Scott Bryce wrote:

    > sub get_large_data_structure {
    >
    > my %large_data_structure = (
    >
    > # Large data structure defined here.
    >
    > }

    -------^

    That should be a ).

    >
    > return \%large_data_structure;
    > }
     
    Scott Bryce, Dec 19, 2005
    #3
  4. wrote:
    > I'd like to define a large hash at the end of the script file. The hash
    > contains some parameters.
    >
    > But I need to use this hash variable at the beginning of the script
    > file.
    >
    > The hash is very long, so it's impossible for me to put it at the
    > beginning of the script.
    >
    > Do I need to declare it at the beginning of the file? If not, does it
    > matter where in the file I put the variable definition?


    You could use a subroutine to define it:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my %large_hash = define_large_hash();

    # many, many, many, many lines of code

    sub define_large_hash {
    key1 => value1,
    key2 => value2,
    ....
    key9998 => value9998,
    key9999 => value9999,
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Dec 19, 2005
    #4
  5. Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > wrote:
    > > I'd like to define a large hash at the end of the script file. The hash
    > > contains some parameters.
    > >
    > > But I need to use this hash variable at the beginning of the script
    > > file.
    > >
    > > The hash is very long, so it's impossible for me to put it at the
    > > beginning of the script.
    > >
    > > Do I need to declare it at the beginning of the file? If not, does it
    > > matter where in the file I put the variable definition?

    >
    > You could use a subroutine to define it:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my %large_hash = define_large_hash();
    >
    > # many, many, many, many lines of code
    >
    > sub define_large_hash {
    > key1 => value1,
    > key2 => value2,
    > ...
    > key9998 => value9998,
    > key9999 => value9999,
    > }
    >
    > __END__


    Let me show up a few alternatives.

    Almost equivalently, the constant pragma can be used:

    my %large_hash = LARGE_HASH;

    # many, many, many, many lines of code

    use constant LARGE_HASH => (
    key1 => value1,
    key2 => value2,
    ...
    key9998 => value9998,
    key9999 => value9999,
    );

    Using Eric Roode's Readonly (from CPAN) would currently require an
    explicit BEGIN block to make sure %large_hash is set early:

    my %large_hash;

    # many, many, many, many lines of code

    use Readonly;
    BEGIN {
    Readonly %large_hash => (
    key1 => value1,
    key2 => value2,
    ...
    key9998 => value9998,
    key9999 => value9999,
    );
    }


    As of the next version (if I understand correctly) one will be able
    to write

    my %large_hash;

    # many, many, many, many lines of code

    use Readonly \ %large_hash => (
    key1 => value1,
    key2 => value2,
    ...
    key9998 => value9998,
    key9999 => value9999,
    );

    Either way, I notice that with Readonly the declaration "my %large_hash"
    would *have* to be commented to the effect that it is set at compile time
    further down in the code. That is the cost of saving the intermediate
    routine define_large_hash (or LARGE_HASH) of the other solutions, which
    make the assignment explicit.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Dec 19, 2005
    #5
  6. Jim Gibson <> wrote in
    news:191220051007403589%:

    > In article <do6ggt$bne$-Berlin.DE>, Anno Siegel
    > <-berlin.de> wrote:
    >

    ....

    >> Using Eric Roode's Readonly (from CPAN) would currently require an
    >> explicit BEGIN block to make sure %large_hash is set early:
    >>
    >> my %large_hash;
    >>
    >> # many, many, many, many lines of code
    >>
    >> use Readonly;
    >> BEGIN {
    >> Readonly %large_hash => (
    >> key1 => value1,
    >> key2 => value2,
    >> ...
    >> key9998 => value9998,
    >> key9999 => value9999,
    >> );
    >> }
    >>

    >
    > Why use Readonly? Isn't simpler better?


    If the hash is going to be used as a lookup table, it might make sense
    to prevent it from being changed.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Dec 19, 2005
    #6
  7. Guest

    wrote:
    > I'd like to define a large hash at the end of the script file. The hash
    > contains some parameters.
    >
    > But I need to use this hash variable at the beginning of the script
    > file.
    >
    > The hash is very long, so it's impossible for me to put it at the
    > beginning of the script.


    How does its size make it impossible to put at the beginning, but possible
    to put it at the end?

    Anyway, I often do things like this by putting the data in a __DATA__
    section, and near the start script reading it into a hash with a "while
    (<DATA>)" loop.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 19, 2005
    #7
  8. Anno Siegel Guest

    Jim Gibson <> wrote in comp.lang.perl.misc:
    > In article <do6ggt$bne$-Berlin.DE>, Anno Siegel
    > <-berlin.de> wrote:
    >
    > > > wrote:
    > > > > I'd like to define a large hash at the end of the script file. The hash
    > > > > contains some parameters.
    > > > >
    > > > > But I need to use this hash variable at the beginning of the script
    > > > > file.

    >
    >
    > >
    > > Let me show up a few alternatives.
    > >
    > > Almost equivalently, the constant pragma can be used:
    > >
    > > my %large_hash = LARGE_HASH;
    > >
    > > # many, many, many, many lines of code
    > >
    > > use constant LARGE_HASH => (
    > > key1 => value1,
    > > key2 => value2,
    > > ...
    > > key9998 => value9998,
    > > key9999 => value9999,
    > > );
    > >
    > > Using Eric Roode's Readonly (from CPAN) would currently require an
    > > explicit BEGIN block to make sure %large_hash is set early:
    > >
    > > my %large_hash;
    > >
    > > # many, many, many, many lines of code
    > >
    > > use Readonly;
    > > BEGIN {
    > > Readonly %large_hash => (
    > > key1 => value1,
    > > key2 => value2,
    > > ...
    > > key9998 => value9998,
    > > key9999 => value9999,
    > > );
    > > }
    > >

    >
    > Why use Readonly? Isn't simpler better?


    Often. We know too little about how %large_hash is used to tell. I
    happened to be interested in how a Readonly solution looks like. I don't
    think the OP said explicitly that %large_hash wouldn't be changed, I just
    assumed it for convenience.

    Then again, I do try to avoid BEGIN blocks when I can by piggy-backing
    compile-time action on a use-statement. "BEGIN" looks somewhat alarming,
    an everyday "use" is easier on the reader's nerves. The "BEGIN"-solution
    *is* simple in being direct, but using a module *looks* simpler. That's
    what information hiding is about.

    > my %large_hash;
    >
    > # many, many, many, many lines of code
    >
    > BEGIN {
    > %large_hash = (
    > key1 => value1,
    > key2 => value2,
    > ...
    > key9998 => value9998,
    > key9999 => value9999,
    > );
    > }


    Sure. Like the Readonly solutions that needs a comment where %large_hash
    is declared/first used.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Dec 19, 2005
    #8
  9. Guest

    Thanks so much for the alternative solutions!

    Very helpful. Have a good holiday season, guys.
     
    , Dec 20, 2005
    #9
  10. Ian Stuart Guest

    A. Sinan Unur wrote:
    > If the hash is going to be used as a lookup table, it might make sense
    > to prevent it from being changed.

    Actually, I've come across a few situations where I've wanted to be able
    to have a large lookup-table with all the common elements in it, yet
    also be able to add some specific "extras" for specific tasks.

    For example: MathML::Entities contains some 2,000 XML+XHTML entities
    (&foo;) and the number-code for that entity ('aacute' => 'รก').
    This is good for 90% of cases, however I have an XML dataset created
    from 1980's data, with entities such as &dotlessi; - which are not part
    of the XML+XHTML standard.
    This means that, to manage this data, I need to extend the
    MathML::Entities lookup-table - not possible if it's either a 'my'
    variable, locked as a read-only hash, or without a method for adding
    records...
     
    Ian Stuart, Dec 22, 2005
    #10
    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. khawar
    Replies:
    1
    Views:
    7,349
    GastonQ
    Jul 10, 2003
  2. Boris Zakharin

    Error: Must Declare Variable

    Boris Zakharin, Jul 21, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    618
    Boris Zakharin
    Jul 22, 2003
  3. Jiggaz
    Replies:
    6
    Views:
    32,206
    Raghudubba
    Nov 5, 2009
  4. Miguel Dias Moura

    How to declare a variable in the global scope?

    Miguel Dias Moura, Dec 14, 2004, in forum: ASP .Net
    Replies:
    41
    Views:
    10,852
    Vikesh Arora
    Dec 27, 2004
  5. =?Utf-8?B?SXNoYWFu?=

    variable declare dynamically

    =?Utf-8?B?SXNoYWFu?=, Mar 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    403
Loading...

Share This Page