Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to use variables from other package?

by nite_man (Deacon)
on Apr 27, 2003 at 17:25 UTC ( [id://253500]=note: print w/replies, xml ) Need Help??


in reply to How to use variables from other package?

I would like to suggest OO way.
Package with constants:
package C; use vars qw( $AUTOLOAD %const_values ); my %const_values = ( CONST_1 => 1, CONST_2 => 23, CONST_3 => 'Test', . . . ); use overload '``' => sub { return $const_values{shift} } ; # All constants can be accessed as by using AUTOLOAD sub AUTOLOAD { no strict 'refs', 'subs'; if ($AUTOLOAD =~ /.*::([A-Z]\w+)$/) { my $const_name = $1; *{$AUTOLOAD} = sub {return $const_values{$const_name}}; return $const_values{$const_name}; } return undef; } 1;
Usage in my test.pl:
#!/usr/bin/perl -w use C; print "Const 1:".C->CONST_1."\n"; print "Const 2:".C->CONST_2."\n"; print "Const 3:".C->CONST_3."\n"; __DATA__ Const 1: 1 Const 2: 23 Const 3: Test
Hope that it will be usefull for you.
      
--------------------------------
SV* sv_bless(SV* sv, HV* stash);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253500]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2025-01-25 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (70 votes). Check out past polls.