Help - Search - Member List - Calendar
Full Version: use strict and load
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Ken Killer
I load the config file in the main program using do,
<main.pl>
#!perl
do "conf";
use strict;
print "value of v is $vn";
------------------------------------
and the config file has only one line,
<conf>
$v = 'hello';

when I run the main.pl, it reports following error, how to fix this?

Global symbol "$v" requires explicit package name at main.pl line 4.
Execution of main.pl aborted due to compilation errors.

Chris Devers
On Mon, 4 Jul 2005, Ken Killer wrote:

QUOTE
I load the config file in the main program using do,
<main.pl
#!perl
do "conf";
use strict;
print "value of v is $vn";
------------------------------------
and the config file has only one line,
<conf
$v = 'hello';

Have you tried making that

my $v = 'hello';

?

When you `use strict`, you need to declare your variables.



--
Chris Devers

aqua red
Could you be more specific what is the code supposed to achieve? Besides
that, I got the same mistake, though it does run without "strict" like ...

#!/usr/bin/perl
use warnings;
require "conf.pl";
print "value of v is $vn";
# got me value of v is hello (:

# do "conf"; Disabled 'cause not needed.
I dunno much about perl, this line looks
very strange to me (: The only "do" I met was like... a loop structure,
say...
do {$line = <STDIN>;}while ($line ne "");

aqua red
And - it works with "strict" if cooked this way

# ========== this here is main.pl======
#!/usr/bin/perl
use warnings;
use strict;
require "conf2.pl"; #so it knows where to go

print "tha value of v izz $conf2::vn";
#it's $v from conf2.pl, not from main

# ========== this here is conf2.pl======
#!/usr/bin/perl
use warnings;

package conf2;
$v = 'hello';
1;

# it wants a package - give 'im a package (:

Ken Killer
I tried to add 'my' before $v, but I got same error as before.
On 7/5/05, Chris Devers <[Email Removed]> wrote:
QUOTE
On Mon, 4 Jul 2005, Ken Killer wrote:

I load the config file in the main program using do,
<main.pl
#!perl
do "conf";
use strict;
print "value of v is $vn";
------------------------------------
and the config file has only one line,
<conf
$v = 'hello';

Have you tried making that

my $v = 'hello';

?

When you `use strict`, you need to declare your variables.



--
Chris Devers


Shobha Deepthi
Youare trying to print the value of $v before its declaration

print "value of v is $vn";

Try defining this variable before this print statement.



Shobha Deepthi V
The statement below is true.
The statement above is false.






Ken Killer wrote:

QUOTE
I tried to add 'my' before $v, but I got same error as before.
On 7/5/05, Chris Devers <[Email Removed]> wrote:


On Mon, 4 Jul 2005, Ken Killer wrote:



I load the config file in the main program using do,
<main.pl
#!perl
do "conf";
use strict;
print "value of v is $vn";
------------------------------------
and the config file has only one line,
<conf
$v = 'hello';


Have you tried making that

my $v = 'hello';

?

When you `use strict`, you need to declare your variables.



--
Chris Devers







Wiggins d'Anconia
Ken Killer wrote:
QUOTE
I load the config file in the main program using do,
<main.pl
#!perl
do "conf";
use strict;
print "value of v is $vn";
------------------------------------
and the config file has only one line,
<conf
$v = 'hello';

when I run the main.pl, it reports following error, how to fix this?

Global symbol "$v" requires explicit package name at main.pl line 4.
Execution of main.pl aborted due to compilation errors.


perldoc -f do

Specifically the section for 'do EXPR' and more specifically the example
at the bottom. You should be error checking your call to 'do'. And of
particular importance is the line:

"Note that inclusion of library modules is better done with the "use"
and "require" operators, which also do automatic error checking and
raise an exception if there's a problem."

In this case you need to provide the package name for $v because it
hasn't been declared, or declare it with 'our'.

perldoc -f our

Either in 'main.pl', add:

our $v;
or use,

print "value of v is $::vn";

Of course there are better ways to handle program configuration, there
are numerous modules on CPAN for handling all manners of formatted
config files.

http://danconia.org


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.