Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

how to create a constant file and use it in other file

by perlfan99 (Sexton)
on Jul 16, 2008 at 00:09 UTC ( #697829=perlquestion: print w/replies, xml ) Need Help??

perlfan99 has asked for the wisdom of the Perl Monks concerning the following question:

dear perlmonks,

i want to create a constant file and use it without the package name in other file. this is what i did,

///// //////////// package MyConst; use Readonly; Readonly::Scalar $const1 => 1; ///////// END ////////////
////// ///////// use MyConst; print $const1; ////////// END //////////
i got this error: "Global symbol "$const1" requires explicit package name at line 4

it doesn't matter if i call "$const1" or "$MyConst::const1", same error as above, maybe i just don't understand Perl namespace, any help will be greatly appreciated.


Replies are listed 'Best First'.
Re: how to create a constant file and use it in other file
by pc88mxer (Vicar) on Jul 16, 2008 at 00:13 UTC
    Referring to $MyConst::const1 works for me:
    use strict; use warnings; use MyConst; print $MyConst::const1, "\n"; # prints 1
      sorry i just forget that i put "use strict;" in "" file, without that line, it works; with that line, i got the compile error.
        Then just add:
        our $const1;
        to (just before the Readonly declaration.)

        Update: To use a variable in another package without requiring a prefix, you would have to import the symbol from the other package. The standard way to do this is to have MyConst be a subclass of Exporter.

        oh, i am using perl v5.6.1

        can you use the constant data without prefixing it with the package name? ( $const1 instead of $MyConst::const1 )

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://697829]
Approved by pc88mxer
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2023-06-08 05:39 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.