Gossamer Forum
Home : General : Perl Programming :

Using a package

Quote Reply
Using a package
Hi guys,
I have a package in a seperate file called select.fields like so,
Code:
package select;

@Season = (

'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December');

return( 1 );

I have included it with
Code:
require "select.fields";
and now I`m trying to use it with
Code:
@fields = @select::$name;
where $name is a variable passed in with the value Season.

But no matter how I try I cannott get it working, I figure something in the @fields = @select::$name; is screwing it up, it just wont make @fields the values of @select::$name

Any ideas welcomed, thanks.

chmod
Quote Reply
Re: [chmod] Using a package In reply to
You want to do something like:

Code:
package MySelect;

#=================================

use strict;
use vars qw/@season @ISA @EXPORT_OK/;
@ISA = qw/Exporter/;
@EXPORT_OK = qw/@season/;

#=================================

@season = qw/January February March April May June July August September October November December/;

1;

Then in your script use:

use MySelect qw/@season/;


It doesn't really seem worth creating a module just for that though.

Last edited by:

Paul: May 7, 2002, 2:41 AM
Quote Reply
Re: [Paul] Using a package In reply to
Nah, you want to go the OO route ;-)

- wil
Quote Reply
Re: [chmod] Using a package In reply to
Hi!
It seems to me, what the only opportunity to use your array is:

Code:
@select::Seasons


and if $name ne "Seasons" it should not be work?
Don't you think so?

Good luck,
Anton.
Quote Reply
Re: [Wil] Using a package In reply to
Err....care to show an example....there's not much OO code you can use when you are just exporting an array.
Quote Reply
Re: [Paul] Using a package In reply to
I know. I think it's crazy to use a module for this purpose anyway. It's just the way you set up your module. You could of still done it in an OO way.

- wil
Quote Reply
Re: [Wil] Using a package In reply to
Show me how then.
Quote Reply
Re: [Anton_P] Using a package In reply to
Thats the weird thing, it seems the use of the $name is what is screwing it up.
Code:
@fields = @select::$name;

When I use
Code:
@fields = @select::Season;
it works just fine.

The resaon I need the variable $name is because the package select; has more than one array within it and the same routine builds different select fields based on what is passed in as $name.

I`m still scratching my head..Unsure

thanks for all your help so far.

chmod

Last edited by:

chmod: May 7, 2002, 3:03 AM
Quote Reply
Re: [chmod] Using a package In reply to
Try what I suggested.

If you have more than one array just add them all into the module and update the use vars and @EXPORT_OK lines then you can easily access them with:

use MySelect qw/@array1 @array2/;

Last edited by:

Paul: May 7, 2002, 3:06 AM
Quote Reply
Re: [Paul] Using a package In reply to
I`ll give it a go thanks Paul.

but I`m still confused about naming an array, can an array or an array within a package not be named using a variable?

chmod

Last edited by:

chmod: May 7, 2002, 3:07 AM
Quote Reply
Re: [Paul] Using a package In reply to
Code:
package MySelect;

# ----------------------------------------

use strict;
use vars qw/$VERSION/;

$VERSION = 0.01;

# ----------------------------------------

sub new {
# ----------------------------------------

my $class = shift;
my $self = { @_ };
bless $self, $class;

}

sub seasons {
# ----------------------------------------

@season = qw/January February March April May June July August
September October November December/;

}

1;

- wil
Quote Reply
Re: [chmod] Using a package In reply to
Okay I got it working by changing my original setup.

seperate file called select.fields
Code:
@Season = (

'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December');
required with
Code:
require "select.fields";
used with
Code:
@fields = @$name;
Thanks for all your suggestions guys, but I think the above is the simplest for me, cheers.

chmod
Quote Reply
Re: [chmod] Using a package In reply to
Are you sure that works?

@$name is an array reference....if you add strict to your module it will crap out....it won't work as you expect by calling an array with the name $name.

Last edited by:

Paul: May 7, 2002, 3:46 AM
Quote Reply
Re: [Paul] Using a package In reply to
yeah your right, if I add use strict; at the top of the select.fields file it does indeed crap out.
But without it it works fine, is there a problem with not using use strict;? why should I use it in the included file?

cheers

chmod
Quote Reply
Re: [chmod] Using a package In reply to
Use strict enforces better coding practices. If you add strict and your script dies then you should do your best to change the script so it doesn't die.

Its a good idea to use strict in all scripts...GT do this in all their modules and cgi scripts.

There are only a few examples in which strict can be turned "off" but you should reactivate it straight away again...eg...the following will cause an error with strict:

Code:
my $var = 'test';

&{ $var };

That code is basically the same as just writing test(); ....however it is a subref and strict will spew, so you'd need to add:

Code:
my $var = 'test';

no strict 'refs';
&{ $var };

...to turn off strict references.

Last edited by:

Paul: May 7, 2002, 4:29 AM
Quote Reply
Re: [chmod] Using a package In reply to
Why do you want to use soft references (accessing a variable where it's name is another variable). Unless you have a good reason for doing this, you are better off using hashes:

my %vars = (

seasons => qw/..../;

);

and then access it: $select::vars{$name}; where $name is 'seasons'. This works fine under use strict, and is much safer.

Cheers,

Alex
--
Gossamer Threads Inc.