For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > perl variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl variables
alexandre.melard@gmail.com

2005-07-26, 5:02 pm

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.

---
Apologize for my English I ain't no English
---

Tim Hammerquist

2005-07-26, 5:02 pm

alexandre.melard@gmail.com <alexandre.melard@gmail.com> wrote:
> Is-it possible in Perl as it is in PHP, to do something like:
>
> 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?


see perlvar(3), section "Symbolic references"

> 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.


You speak pretty strongly in English. One might consider that
paragraph rude. Please show respect for the others in this group, lest
they plonk you.

Tim Hammerquist
Paul Lalli

2005-07-26, 5:02 pm

alexandre.melard@gmail.com wrote:
> 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

alexandre.melard@gmail.com

2005-07-26, 5:02 pm

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ürgen Exner

2005-07-26, 5:02 pm

alexandre.melard@gmail.com wrote:
> 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


Tim Hammerquist

2005-07-26, 10:03 pm

alexandre.melard@gmail.com <alexandre.melard@gmail.com> wrote:
> Why so many people use the word "bullshit"? I though it was very
> common...


Yes, in a few senses. It is very frequently used. But the word emerged
out of a classist society where "commoners" were understood to be the
lower classes. In this way, "common" comes to be a synonym for
"vulgar."

"Bullshit" is coincidentally labelled as a "vulgar" term in many
dictionaries.

No harm done. I've made many a faux pas in learning foreign languages.
So long as a lesson is learned. :)

Cheers,
Tim Hammerquist
Anno Siegel

2005-07-27, 9:03 am

Paul Lalli <mritty@gmail.com> wrote in comp.lang.perl.misc:
> alexandre.melard@gmail.com wrote:


[...]

>
> 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
alexandre.melard@gmail.com

2005-07-27, 5:05 pm

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

Alex

alexandre.melard@gmail.com wrote:
> 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.
>
> ---
> Apologize for my English I ain't no English
> ---


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com