use/require, and variables

N

Nick Wedd

I have always found that putting "use strict" makes life easier for me.

But now, I am trying to use what C programmers call "#include". I have
a "main.pl" file, which needs to include another file where values are
assigned to a variable. I have tried all combinations of
use "includedfile.pl" or require "includedfile.pl"
declaring the variable (a hash) in the main file, or the included file
declaring the variable before or after the use/require statement
declaring the variable as "my" or as "local"
but whatever I try, Perl complains about uninitialised or undeclared
variables.

I have found that if I don't declare my hash at all, and use
'require "includedfile.pl"', it behaves as I want, the values assigned
in the included file are usable in the main file. Of course, failing to
declare a variable means I can't use 'use strict'.

So, I would like to know what the recommended way is of declaring the
kind of variable that you get when you just use one without declaring
it. This way, I would be able to have my #include working and still
have "use strict" there to help me.

Nick
 
G

Gunnar Hjalmarsson

Nick said:
I have always found that putting "use strict" makes life easier for me.

But now, I am trying to use what C programmers call "#include". I have
a "main.pl" file, which needs to include another file where values are
assigned to a variable.

It's not crystal clear what you mean - some code would have been nice -
but...
I have found that if I don't declare my hash at all, and use
'require "includedfile.pl"', it behaves as I want, the values assigned
in the included file are usable in the main file. Of course, failing to
declare a variable means I can't use 'use strict'.

I suppose you can declare the variable in main.pl with "our".
 
C

cmic

I have always found that putting "use strict" makes life easier for me. .....snip

So, I would like to know what the recommended way is of declaring the
kind of variable that you get when you just use one without declaring
it. This way, I would be able to have my #include working and still
have "use strict" there to help me.

Don't know if this is the "recommended way",
but you can do this way:

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

use vars qw(%docs $style);
do "./variables.conf";

print $style, "\n";
print $docs{def}, "\n";
__END__

And the file variables.conf:
#
$style="free";
%docs=(abc => 111, def => 222);
#

though there might be other ways to do this.
Regards
 
N

Nick Wedd

Gunnar Hjalmarsson said:
It's not crystal clear what you mean - some code would have been nice -
but...

Ok. main.pl says, among other things,
require "freqs.pl";
$w += @{$indfreq{"foo"}}[5];
and freqs.pl has many lines such as
$indfreq{"foo"} = [ 0, 0, 0, 0, 19440, 68160, 54960, 8640 ];
$indfreq{"bar"} = [ 0, 0, 0, 0, 14400, 67800, 59640, 9360 ];
I suppose you can declare the variable in main.pl with "our".

Thank you! Using "our" works and is compatible with "use strict". I
must look up what "our" does, it seems to be more recent than my
reference books.

Nick
 
B

Brad Baxter

Nick said:
I have found that if I don't declare my hash at all, and use
'require "includedfile.pl"', it behaves as I want, the values assigned
in the included file are usable in the main file. Of course, failing to
declare a variable means I can't use 'use strict'.

Others have suggested 'use vars' and 'our', which answers your
question. I wanted to comment a little bit about why. If I
run this program:

require 'qt.data';

print "$_\n" for $bird, $fish, $worm;

__END__
contents of qt.data:
---
$bird = 'canary';
$fish = 'tuna';
$worm = 'nematode';
1;
---

it prints:

canary
tuna
nematode

without errors. Note that neither the program nor the data
file have 'use strict'. The reason this works is that perl
is simply allowing you to refer to those global variables
without explicit package names.

If I add 'use strict', I can still print the values if I
provide explicit package names:

use strict;
require 'qt.data';

print "$_\n" for $main::bird, $main::fish, $main::worm;

prints the values without errors.

If I add 'use vars' or 'our', I can dispense with the explicit
package names:

use strict;
our( $bird, $fish, $worm );
require 'qt.data';

print "$_\n" for $bird, $fish, $worm;

The construct 'use vars' predates 'our' to do the same thing.

Be aware that this only works with global variables. If the
data file were instead:

my $bird = 'canary';
my $fish = 'tuna';
my $worm = 'nematode';
1;

not even 'our' will let your program see those values.

I suggest that you use a configuration file module.

Cheers,
 
B

Ben Morrow

Quoth Nick Wedd said:
I have always found that putting "use strict" makes life easier for me.

But now, I am trying to use what C programmers call "#include". I have
a "main.pl" file, which needs to include another file where values are
assigned to a variable. I have tried all combinations of
use "includedfile.pl" or require "includedfile.pl"
declaring the variable (a hash) in the main file, or the included file
declaring the variable before or after the use/require statement
declaring the variable as "my" or as "local"
but whatever I try, Perl complains about uninitialised or undeclared
variables.

It is better to write a proper module you can 'use' which exports the
variables than to use 'require'. See perldoc perlnewmod and perldoc
Exporter.

Ben
 
T

Ted Zlatanov

NW> I have always found that putting "use strict" makes life easier for me.
NW> But now, I am trying to use what C programmers call "#include". I
NW> have a "main.pl" file, which needs to include another file where
NW> values are assigned to a variable. I have tried all combinations of
NW> use "includedfile.pl" or require "includedfile.pl"
NW> declaring the variable (a hash) in the main file, or the included file
NW> declaring the variable before or after the use/require statement
NW> declaring the variable as "my" or as "local"
NW> but whatever I try, Perl complains about uninitialised or undeclared
NW> variables.

NW> I have found that if I don't declare my hash at all, and use
NW> 'require "includedfile.pl"', it behaves as I want, the values assigned
NW> in the included file are usable in the main file. Of course, failing
NW> to declare a variable means I can't use 'use strict'.

NW> So, I would like to know what the recommended way is of declaring the
NW> kind of variable that you get when you just use one without declaring
NW> it. This way, I would be able to have my #include working and still
NW> have "use strict" there to help me.

Consider YAML, XML, AppConfig, etc. data-oriented configuration modules.
They'll make the task easier, you'll be able to edit the configuration
files with external tools if needed, and they'll be less of a risk to
your software.

Ted
 
J

J. Gleixner

Nick said:
Gunnar Hjalmarsson said:
It's not crystal clear what you mean - some code would have been nice -
but...

Ok. main.pl says, among other things,
require "freqs.pl";
$w += @{$indfreq{"foo"}}[5];

IMHO that'd be a bit cleaner as:

$indfreq{ 'foo' }[5]

or if you like to have it be more explicit:

$indfreq{ 'foo' }->[5]
and freqs.pl has many lines such as
$indfreq{"foo"} = [ 0, 0, 0, 0, 19440, 68160, 54960, 8640 ];
$indfreq{"bar"} = [ 0, 0, 0, 0, 14400, 67800, 59640, 9360 ];
[...]
 
B

Ben Morrow

Quoth "J. Gleixner said:
Nick said:
Ok. main.pl says, among other things,
require "freqs.pl";
$w += @{$indfreq{"foo"}}[5];

IMHO that'd be a bit cleaner as:

$indfreq{ 'foo' }[5]

Not just cleaner, more correct. @{...}[5] is an array slice, the same as
@a[5]; for some reason you don't get a 'Scalar value %s better written
as %s' warning if the array is not a simple array variable, but it still
applies. If you really want to avoid the arrow syntax (and the implicit
arrow in the above), you can use

${ $indfreq{"foo"} }[5]

(note the initial '$' rather than '@', since this is a single element).

Ben
or if you like to have it be more explicit:

$indfreq{ 'foo' }->[5]
and freqs.pl has many lines such as
$indfreq{"foo"} = [ 0, 0, 0, 0, 19440, 68160, 54960, 8640 ];
$indfreq{"bar"} = [ 0, 0, 0, 0, 14400, 67800, 59640, 9360 ];
[...]
 
T

Tad J McClellan

Thank you! Using "our" works and is compatible with "use strict". I
must look up what "our" does,


Do that with:

perldoc -f our

then you will be reading documentation that matches whatever
version of perl you have.

it seems to be more recent than my
reference books.


Then your reference books must be more than 8 years old...
 
N

Nick Wedd

Tad J McClellan said:
Do that with:

perldoc -f our

then you will be reading documentation that matches whatever
version of perl you have.

Thanks. I've now read it, I am trying to understand it ...
Then your reference books must be more than 8 years old...

They are. Publication dates are 1997, 1997, 1999. Can you recommend a
single modern book on Perl, suitable for someone with general
programming knowledge, but little understanding of Perl?

Nick
 
N

Nick Wedd

J. Gleixner said:
Nick said:
Gunnar Hjalmarsson said:
Nick Wedd wrote:
I have always found that putting "use strict" makes life easier for me.
But now, I am trying to use what C programmers call "#include". I
have a "main.pl" file, which needs to include another file where
values are assigned to a variable.
It's not crystal clear what you mean - some code would have been nice -
but...
Ok. main.pl says, among other things,
require "freqs.pl";
$w += @{$indfreq{"foo"}}[5];

IMHO that'd be a bit cleaner as:

$indfreq{ 'foo' }[5]

Thank you. That is definitely an improvement, I can see that.

Nick
or if you like to have it be more explicit:

$indfreq{ 'foo' }->[5]
and freqs.pl has many lines such as
$indfreq{"foo"} = [ 0, 0, 0, 0, 19440, 68160, 54960, 8640 ];
$indfreq{"bar"} = [ 0, 0, 0, 0, 14400, 67800, 59640, 9360 ];
[...]
 
J

Jim Gibson

Nick Wedd said:
In message <[email protected]>, Tad J McClellan


They are. Publication dates are 1997, 1997, 1999. Can you recommend a
single modern book on Perl, suitable for someone with general
programming knowledge, but little understanding of Perl?

perldoc -q books
 
T

Tad J McClellan

Nick Wedd said:
Thanks. I've now read it, I am trying to understand it ...


They are. Publication dates are 1997, 1997, 1999. Can you recommend a
single modern book on Perl, suitable for someone with general
programming knowledge, but little understanding of Perl?


I don't recommend any book as a primary reference to Perl.

I recommend(ed above) the documentation that ships with
the perl distribution as the primary Perl reference. :)

You can even use my primary reference to identify secondary references:

perldoc -q book
perldoc perlbook


For a reference book, I'd go with the Camel book ("Programming Perl").

If you're looking for a tutorial rather than a reference,
then "Learning Perl" seems to fit the bill.
 
D

David Combs

I like this book you *never* see mentioned here:

Pro Perl, by Peter Wainwright, APRESS

1030pages, excellent, assumes, though, that you
already know how to program in some (any) language.

Cheap enough -- try www.bookpool.com, though amazon
has been lowering its prices a bit too.


Check out the amazon reviews.


David
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top