Macros

C

chatiman

Hello,

Is it possible to define macros in perl without using the C preprocessor ?

Or is there a way to access the caller's function local variables ?

Thanks
 
B

Ben Morrow

chatiman said:
Hello,

Is it possible to define macros in perl without using the C preprocessor ?
Filter::Simple

Or is there a way to access the caller's function local variables ?

Why do you want to do this? There is almost certainly a better way.

However, if you must: PadWalker.

Ben
 
C

chatiman

Why do you want to do this? There is almost certainly a better way.
Has you saw in my previous post, I'm trying to "localize" my application
(ie support different languages). I took a look at Locale::Maketext but it
seems
to complex to use and require additionnal packages.

So I thought to write a simple module which will do what I want :

The idea is to have a text file for each language (or a simple hash) like:
id1: the text associated to id1 with $variableA

And also I will have a function:

sub my_gettext {
my ($id) = @_
return eval "return \"$localized{$id}\"";
}

where localized is a hash associated to the configuration file

This would work when variable are not localized with "my" ...

But to be "cleaner" I would like it to work in anycases

for that I need to access the caller's localized variables or use macros

But the main idea is to keep it simple ...
 
J

Jay Tilton

: "Ben Morrow" <[email protected]> a écrit dans le message de : [email protected]...

: > > Or is there a way to access the caller's function local variables ?
: >
: > Why do you want to do this? There is almost certainly a better way.
: >
: Has you saw in my previous post, I'm trying to "localize" my application
: (ie support different languages). I took a look at Locale::Maketext but it
: seems
: to complex to use and require additionnal packages.

Life is like that sometimes. If you want something done right, you'll have
to work at it.

: So I thought to write a simple module which will do what I want :
:
: The idea is to have a text file for each language (or a simple hash) like:
: id1: the text associated to id1 with $variableA
:
: And also I will have a function:
:
: sub my_gettext {
: my ($id) = @_
: return eval "return \"$localized{$id}\"";
: }
:
: where localized is a hash associated to the configuration file
: This would work when variable are not localized with "my" ...

Then don't use such lexical variables to store information needed by the
subroutine.

: But to be "cleaner" I would like it to work in anycases

That's just wrong thinking. Prying open a foreign lexical scope is useful
for debugging or in cases of dire emergency, but doing it in real
production code is an _appallingly_ bad idea. The code will certainly not
be clean.

: for that I need to access the caller's localized variables or use macros
: But the main idea is to keep it simple ...

The simple solution would be to use package variables when you need to
access them from somewhere else. That's the whole reason why package
variables exist.

For a recent discussion of I18N techniques, see the thread starting at
Message-ID: <[email protected]>
 
B

Brian McCauley

chatiman said:
Has you saw in my previous post, I'm trying to "localize" my application
(ie support different languages). I took a look at Locale::Maketext but it
seems
to complex to use and require additionnal packages.

So I thought to write a simple module which will do what I want :

The idea is to have a text file for each language (or a simple hash) like:
id1: the text associated to id1 with $variableA

And also I will have a function:

sub my_gettext {
my ($id) = @_
return eval "return \"$localized{$id}\"";
}

Oh, no not again! Seems hardly a week since we had two (yes two)
threads on this FAQ.

Don't use double quotes to do this. Double qoutes are quite common in
natural language.

If you want to do this use the chop/here-doc approach (or the approach
given in the FAQ).

sub my_gettext {
my ($id) = @_
chop ( my $r = eval "<<_END_\n$localized{$id}\n_END_");
$r;
}

I really wish the FAQ would include this answer - since it is the real
answer to the question. Pretending it doesn't exist just means
people, when they come upon it, may not be warned how dangerous it is
(your internationalization config file could contain arbitrary Perl
code) and will loose trust in the honesty of the FAQ.

This would work when variable are not localized with "my" ...

But to be "cleaner" I would like it to work in anycases

I'll look at it

Well String::Interpolate will do what you think you want but even I
who wrote it wouldn't really recommend it. Indeed the ability to use
PadWalker is experimental and was only only really an exercise to see
if it's possible. I do not consider it clean.

If you use String::Interpolate there are better ways.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top