perl variables

Discussion in 'Perl Misc' started by alexandre.melard@gmail.com, Jul 26, 2005.

  1. Guest

    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
    ---
     
    , Jul 26, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    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
     
    Paul Lalli, Jul 26, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jul 26, 2005
    #3
  4. 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
     
    Jürgen Exner, Jul 26, 2005
    #4
  5. Anno Siegel Guest

    Paul Lalli <> wrote in comp.lang.perl.misc:
    > 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
    --
    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.
     
    Anno Siegel, Jul 27, 2005
    #5
  6. Guest

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

    Alex

    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
    > ---
     
    , Jul 27, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,809
  2. Replies:
    9
    Views:
    959
  3. Talha Oktay
    Replies:
    8
    Views:
    217
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    305
    PerlFAQ Server
    Jan 23, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    690
    PerlFAQ Server
    Feb 3, 2011
Loading...

Share This Page