Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

typglob problem

by vinoth.ree (Monsignor)
on Mar 18, 2009 at 03:56 UTC ( [id://751344]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; my $cure = "cure"; my @cure = ('one', 'two', 'three'); &cure(*cure); sub cure { my $new = shift; print $$new; }

Why this code does not print anything ?

Replies are listed 'Best First'.
Re: typglob problem
by almut (Canon) on Mar 18, 2009 at 04:12 UTC

    There are no typeglobs for lexical (my) variables. Try our instead.

Re: typglob problem
by ikegami (Patriarch) on Mar 18, 2009 at 04:51 UTC

    Globs are symbol table entries, and only package variables are in the symbol table. Lexical (my) variables are not in the symbol table.

    But there's no reason for you to be using globs anyway.

    use strict; use warnings; my $cure = "cure"; my @cure = ('one', 'two', 'three'); cure(\$cure); sub cure { my $new = shift; print $$new; }

    perlref, perlreftut

Re: typglob problem
by nagalenoj (Friar) on Mar 18, 2009 at 04:15 UTC
    Dear Monk, Try the below,
    use strict; use warnings; our $cure = "cure"; our @cure = ('one', 'two', 'three'); &cure(*cure); sub cure { my $new = shift; print "$$new"; print "@$new"; }

    I hope, you would have understood by seeing the code. Yes, ofcourse my variables won't available in symbol table.

    Also, read the Variables and Scoping tutorials for more.

Re: typglob problem
by tilly (Archbishop) on Mar 18, 2009 at 04:38 UTC
    While others have pointed you towards a solution, I'm going to very, very strongly suggest that you avoid using symbolic references. Read this article and follow the links to follow-ups 2 and 3.

    Instead of that approach go read references quick reference and use real references instead.

      Noone used or suggested using symbolic references. I wouldn't advise using globs either, though.
        Oops, I just looked at the $$new and didn't look at the calling code.

        But the problems raised are similar.

Re: typglob problem
by Anonymous Monk on Mar 18, 2009 at 04:31 UTC
    It prints
    Use of uninitialized value in print at - line 11.
    which is a clue that ${*cure} is undefined

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://751344]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-24 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found