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

Re: Re: Re: Sorting strings

by spartan (Pilgrim)
on Aug 31, 2001 at 19:19 UTC ( #109419=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Sorting strings
in thread Sorting strings

I need to do some homework. I still do not understand. Thank you so much for your explanation though, I just don't get it. :(

Very funny Scotty... Now PLEASE beam down my PANTS!

Replies are listed 'Best First'.
Re: Re: Re: Re: Sorting strings
by demerphq (Chancellor) on Aug 31, 2001 at 22:32 UTC
    ++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.

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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://109419]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-01-29 04:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found