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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
++spartan. Knowing when to do homework is a *good* thing. Wish I had when I was in highschool. :-)
Ok maybe this will help: (Gurus, details are fudged for understanding here)
Perl has two flavours of variables, lexical variables and dynamic variables. These are declared in a module in the following way (this is perl 5.6):
my $lexical="Value"; our $dynamic="Value";
Dynamic variables are package scoped, which means that such a variable (unless local()ized, which we may get to later) is available and shared throughout the *whole* package. These variables are entered into something called a 'pad' or more formally the package symbol table. This table is essentially a hash where the names (without type symbols $ % @ &) are the keys, and the values are these things called typeglobs (which are accessed through the * type symbol) this hash is reffered to inside a package as %:: and outside as %PACKAGE::. A typeglob is essentially a wrapper around the various types that a given name represents. Or in other terms a typeglob is a bundle of all of the dynamic variables that have a given name.This can be seen here
#assuming warnings and strict our $Variable="Variable"; our %Variable=(hash_key=>"Variable"); our @Variable=split(//,"Variable"); sub Variable{ return "Variable"; } open Variable,">>c:/temp/variable.txt" or die "Couldnt open! $!"; { no strict 'refs'; *Other_Variable=*Variable; } $\="\n"; $,=","; print $Other_Variable; print keys %Other_Variable; print @Other_Variable; print Other_Variable(); print Other_Variable "Written to Other_Variable at ".localtime();
If you run it you will see that accessing any variable type with the name Other_Variable really access the equivelent type with the name Variable (and vice versa, they are aliased to each other.) All with ONE assignment.
Now lets have a quick look at the symbol table for the package the above code ran in. Add this code
sub dump_symbol_table { print "SYMBOL TABLE\n--------------------------------------------- +----------\n"; foreach my $key (sort keys %::) { next unless $key=~/^[\x20-\xff]/; (my $value=$::{$key})=~s/([\x00-\x1f])/sprintf("\\x%02x",ord($ +1))/ge; printf "%20s = %-20s\n",$key,"'$value'"; } print "\n"; } BEGIN { dump_symbol_table } END { dump_symbol_table }
Now you should see something like
Other_Variable = '*main::Variable' ... Variable = '*main::Variable' ... \ = '*main::\' _ = '*main::_'
Notice that there is a typeglob '_'? This is the typeglob that holds the dynamic variables $_ @_ %_ and the others. Also notice the aliasing? Other_Variable and Variable both have the same value.
So now on to lexical variables. again these have symbol tables, but they are anonymous and bound to each level of scope in the program. But the typeglob issue is still the same.
So on to the point (sorry its taking so long)
Perl needs a way to tell each of these names apart. Normally it does this via the type symbol before the name, or its position in a statement (such as a print FH "string"; statement).
But when we access an element from on ofthe compound data structures we use something _like_ a scalar context, ie we write $array[1] and not @array[1] The second is a list slice and returns a list, the first is an Array fetch from element 1 and returns a scalar. Since there is a $ in front perl could hypothetically get confused and think that we want $array and not @array, but the presence of the square brackets (or a ->[] for a reference) after the variable name tells perl unequivicably that we are talking about an element of @Array and not the scalar $array.

Ok, so maybe this will help you a bit, at least with some terms that you can chase down in the documentation.
HTH

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

In reply to Re: Re: Re: Re: Sorting strings by demerphq
in thread Sorting strings by rbi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-23 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found