Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

type glob problem

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

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

use Data::Dumper; { my @arra = (10,20); &doublevalue(*arra); } sub doublevalue { local *a = shift ; print Dumper @a; }

In the above code I am passing type glob of *arra to the function doublevalue, in this function I am making *a as temporary alias to *arra, but in @a I am not getting the @arra values. What is the problem in this code ?

Replies are listed 'Best First'.
Re: type glob problem
by nagalenoj (Friar) on Mar 03, 2009 at 07:23 UTC
    You cannot use a lexically scoped array as a parameter, because lexical variables don't have typeglobs associated with them. Lexical variables won't be in symbol tables.
Re: type glob problem
by ELISHEVA (Prior) on Mar 03, 2009 at 07:45 UTC
    Was this meant as an example to illustrate questions about how to work with globs or are you simply trying to pass a value into a function doublevalue(...)? The sample you have posted looks like a very old Perl coding style - what version of Perl are you working with?

    If the point was to pass values, the current way to pass values is to use variables and references, like this:

    use strict; use warnings; use Data::Dumper; { my @arra = (10,20); #no need for & and you can just pass a reference #&doublevalue(*arra); doublevalue(\@arra); } sub doublevalue { #my is preferred to local #local *a = shift ; my $a = shift; print Dumper @$a; }

    If you want to learn more about how type globs work, see perlref.

    Best, beth

Re: type glob problem
by bichonfrise74 (Vicar) on Mar 03, 2009 at 07:37 UTC
    If you change this...
    &doublevalue(\@arra);
    This seems to work just fine for me, unless I'm missing something.
Re: type glob problem
by Anonymous Monk on Mar 03, 2009 at 07:24 UTC
    you need to use references, see print shift; for clues
Re: type glob problem
by boom (Scribe) on Mar 03, 2009 at 09:20 UTC

    You can pass in an ordinary reference to a subroutine expecting a type glob, and it will work fine.

Re: type glob problem
by rir (Vicar) on Mar 03, 2009 at 22:11 UTC
    nagalenoj gave you the correct info; I'm just stating it another way. Your my @arra and your *arra are different and unrelated variables. The *arra is a package variable *main::arra which can, as a typeglob, refer to the variable @main::arra but that has nothing to do with a lexical, aka my, variable named @arra.

    You are passing an empty typeglob to doublevalue. This demonstrates that you are passing nothing, loosely speaking, to your function:

    my @arra = (10,20); print ">@main::arra<$/"; print ">>@arra<<$/";
    Update: With your code, use warnings; would have flagged that the name main::arra was used only once, hinting at the problem. With use diagnostics; the message is more confusing as if it were written before lexical variables were added to the language.

    Be well,
    rir

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found