Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export scalars with 'on fly' generated names 1

Status
Not open for further replies.

1yura1

Programmer
Feb 7, 2003
36
0
0
UA
Hi All.

I have symple hash in module (ModuleName), for example:
my %exec;
$exec{'cat'} = '/bin/cat';
$exec{'grep'} = '/bin/grep';
$exec{'host'} = '/usr/bin/host';

I want module export scalars with 'on fly' generated (from this hash) names=hash keys, and scalars values=hash values.

As the result, I need to import scalars in a script:

use ModuleName qw($cat $grep $host);

Where should be:
$cat = '/bin/cat';
$grep= '/bin/grep';
$host = '/usr/bin/host';

Is it possible in Perl?
Thanks.
 
I'm not sure I understand. Do you mean this?
Code:
package ModuleName;
use strict;
use warnings;
use Exporter();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@ISA = qw(Exporter);
[b]@EXPORT_OK = qw($cat $grep $host);[/b]

my %exec;
$exec{'cat'} = '/bin/cat';
$exec{'grep'} = '/bin/grep';
$exec{'host'} = '/usr/bin/host';

[b]our ($cat, $grep, $host) = @exec{qw(cat grep host)};[/b]

1;

package Someprog;
use strict;
use warnings;
[b]use ModuleName qw($cat $grep $host);[/b]

print qq(\$cat = $cat\n\$grep = $grep\n\$host = $host\n);
Output:
$cat = /bin/cat
$grep = /bin/grep
$host = /usr/bin/host

I say I'm not sure I understand as this seems to be what you're asking, but I don't see where "on the fly" figures into it. ??
 
because the variable names might not be $cat, $grep etc

:-( don't know - an eval trick is called for obviously but I'm rubbish at those -- anyone?

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hello,

Thanks for your replies.

The issue is that I don't know hash keys/values, theirs number (in general). The hash is stored in XML, and has to be loaded from it.

So I need dynamicly form @EXPORT_OK.
But the script will import variables as you wrote by:
use ModuleName qw($cat $grep $host);

I need the mechanizm to "translate" hash keys into scalar names, hash key value - into this scalar value, and "push" these scalars into @EXPORT_OK for exporting.

 
Is there a reason why you can't just export the whole hash?
 
Hello,

It is easier to work with imported scalars instead of hash ,,,
And it is interesting for me if there is the way to do such export/import.

Best Regards.
 
Hi Chessbot.

Yes, exactly, are the same possible in Perl?

Thanks,
Yura.
 
ishnid makes a good point imho

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top