perl variables

A

alexandre.melard

Hi there,

I like triky questions: here is one.
I would like to use some subroutines for a wider use.

Is-it possible in Perl as it is in PHP, to do something like:

First, please do not tell me that it would not compile this, I KNOW! I
would like to try to give you the idea of what I want to do.

so enough blabla...

my $toto = "salut"

my $tutu = "toto";

print '$toto : ', $toto, "\n";
print '$tutu : ', $tutu, "\n";
print '${$tutu} : ', ${$tutu}, "\n";

would print hopefully:

$toto : salut
$tutu : toto
${$tutu} : salut

Do you see the idea, anything interesting?

Give it a good though and come back to me, and please, don`t come back
with some bullshit like I ain't no programmer but something
constructive.

Alexandre.
 
P

Paul Lalli

Hi there,

I like triky questions: here is one.
I would like to use some subroutines for a wider use.

Is-it possible in Perl as it is in PHP, to do something like:

First, please do not tell me that it would not compile this, I KNOW!

Other than the missing semi-colon, what makes you think this won't
compile?
I would like to try to give you the idea of what I want to do.

so enough blabla...

my $toto = "salut"

my $tutu = "toto";

print '$toto : ', $toto, "\n";
print '$tutu : ', $tutu, "\n";
print '${$tutu} : ', ${$tutu}, "\n";

would print hopefully:

$toto : salut
$tutu : toto
${$tutu} : salut

Do you see the idea, anything interesting?

The short answer, the answer to the question you're actually asking, is
"Yes, this will work, but only with package variables, not with lexical
variables". In other words, change all those "my"s to "our"s, and that
will do exactly what you want it to do.

HOWEVER, the better answer is "You don't want to do this." This is
taking advantage of a feature known as "symbolic references". There
are a variety of reasons not to use "symrefs", as they're known. For
details:
1) Search this group for "symrefs" to find a multitude of posts about
them
2) Read perldoc perlref to learn about them
3) Read perldoc -q "variable name" to learn a better approach

The basic better approach is to use a hash instead:
my $tutu = "toto";
my %vars;
$vars{$tutu} = "salut";

print '$tutu : ', $tutu, "\n";
print '$vars{$tutu} : ', $vars{$tutu}, "\n";
Give it a good though and come back to me, and please, don`t come back
with some bullshit like I ain't no programmer but something
constructive.

Your signature claims that you don't speak English natively. You would
probably do well to be informed then, that the above paragraph contains
profanity. "bullshit" is not a word generally used in polite
communication.

Paul Lalli
 
A

alexandre.melard

I am sorry, I might not manipulate the language very well, give me time
please, I learned English in yellowstone working as kitchen porter :-(
And I have seen many people that think I post here because I am dumb...
and do not see what I want to say, give me a chance, I can change that
;-)

Why so many people use the word "bullshit"? I though it was very
common...

I apreciate your help anyway, and you always give me lot of reading
Paul :)

I might become a true Perl programmer some day.
 
J

Jürgen Exner

Hi there,

I like triky questions: here is one.
I would like to use some subroutines for a wider use.

Is-it possible in Perl as it is in PHP, to do something like:

First, please do not tell me that it would not compile this, I KNOW! I
would like to try to give you the idea of what I want to do.

so enough blabla...

my $toto = "salut"

my $tutu = "toto";

print '$toto : ', $toto, "\n";
print '$tutu : ', $tutu, "\n";
print '${$tutu} : ', ${$tutu}, "\n";

would print hopefully:

$toto : salut
$tutu : toto
${$tutu} : salut

That is called a symbolic reference and technically you can use it just like
you wrote it.

HOWEVER, please see the numerous threads about symbolic references (just
search Googel for symref), perldoc perlref, and in particular the FAQ
(perldoc "variable name": How can I use a variable as a variable name?)
about why this is a _very_ bad idea and what to use instead. In short: use
your own hash instead of highjacking the system symbol table.
Do you see the idea, anything interesting?

Not really, rather something old and boring that has been discussed to death
many times.

jue
 
A

Anno Siegel

Paul Lalli said:
(e-mail address removed) wrote:
[...]
my $toto = "salut"

my $tutu = "toto";

print '$toto : ', $toto, "\n";
print '$tutu : ', $tutu, "\n";
print '${$tutu} : ', ${$tutu}, "\n";

would print hopefully:

$toto : salut
$tutu : toto
${$tutu} : salut

Do you see the idea, anything interesting?

The short answer, the answer to the question you're actually asking, is
"Yes, this will work, but only with package variables, not with lexical
variables". In other words, change all those "my"s to "our"s, and that
will do exactly what you want it to do.

It is enough to make $toto (the symbolically referenced one) a package
variable.

Anno
 
A

alexandre.melard

Sorry Jun, I am not that old in computing :-D I will gladely use such
an expression in 10 to 20 years :p

Alex
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top