CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Quizzes - Learn Perl the Fun Way:
Pad

 



Cure
User

Jul 13, 2000, 4:46 PM

Post #1 of 7 (14554 views)
Pad Can't Post

 my $cure = q!cure!;
my @cure = qw!one two three!;

cure(*cure);

sub cure {
$new = shift;
print $$new
}


What will this code print to the screen? Explain why the code doesnt print anything or what it prints to the screen.


Cure


dws
Deleted

Jul 13, 2000, 10:03 PM

Post #2 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

It sucks to get the right answer for the wrong reason.


durose78
Novice

Jul 13, 2000, 10:26 PM

Post #3 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

What will it print out? Nothing.


dws
Deleted

Jul 13, 2000, 11:22 PM

Post #4 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

cure() gets passed a typeglob, so $new should be "*main::cure". The last time I accidentally deferenced a string I got undef, so I'll guess this prints nothing.


SixKiller
Deleted

Jul 14, 2000, 6:36 AM

Post #5 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

as dws said $new is '*main::cure'
but deref'ing a glob afaik gives the datatype you expected ... like $$new is $cure
@$new is @cure %$new is %cure.....i don't know why..but maybe it's because it's like a symref...like ${'main::cure'}..
so in this example $$new will give the value of $cure which is 'cure' and that's what will be printed out

sorry again for my horrible English

hmm..wrong...everything i said was true if $cure wasn't my()ed...but it is...and if it is like symref..symrefs are only for variables that are'nt my()ed(i'd guess typeglobs anyway don't work with my()ed variables?)

[This message has been edited by SixKiller (edited 07-14-2000).]


Jasmine
Administrator

Jul 14, 2000, 7:18 AM

Post #6 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

Right on, SixKiller. Globs do not work with any my'd variables.

Had cure written print $cure instead of print $$cure, it would have printed *main::cure because *cure is referring to the glob for any cure variable in the main:: package symbol table (remember that main is the default package). Since any my variable doesn't appear in the symbol table, dereferencing the scalar $cure doesn't yield anything.

The use of "*cure" in cure(*cure) is a trick, kind and gentle folks, into thinking that the *cure really had anything to do the with my $cure and my @cure Wink

It's interesting to note that any name which was globbed in the subroutine call, such as cure(*hello) will print *main::hello (if using print $new), even if $cure and @cure were not changed. It would still print nothing if print $$new were used, because there is no $hello variable in the main:: package defined.

Take out the my for $cure, and it will print "cure", because the cure scalar will be present in the main package table.

Wheeeee... Aren't globs fun? Smile


Cure
User

Jul 16, 2000, 8:33 AM

Post #7 of 7 (14553 views)
Re: Pad [In reply to] Can't Post

ANSWER TO QUIZ --> IT WILL PRINT NOTHING TO THE SCREEN BECAUSE my() variables DO NOT have anything to do with the symbol table.
Lets say you have $cure in your program as a global variable then $cure would be in your symbol table. my() variables go to the scratchpad. Perl uses a symbol table (implemented interanlly as a hash table) to map identifier names ( the string "cure" without the prefix to thr appropriate values. But you know that a hash table does not tolerate duplicate keys, so you can't really have two entries in the hash table with the same name poitning to two different values. For the reason, Perl interposes a structure called a typeglob between the symbol table entry and the otehr data types, it is just a bunch of pointers to values that can be accessed by the same name, with one pointer for each value type.

Good job Jasmine and Sixkiller.
Jasmine,very good job for explaining it in detail.


Cure


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives