Solved

Make sense of the output

Posted on 2004-08-18
4
205 Views
Last Modified: 2006-11-17
while (my ($key,$value) = each(%main::)) {
local(*gl) = $value;
print "$key - $gl\n";
}

What does the above sample code print?
Choice 1 The names and definitions of all subroutines in the main namespace
Choice 2 A list of all globs contained in the main namespace
Choice 3 The names and scalar contents of all variables in the main namespace
Choice 4 A list of all packages contained underneath the main namespace
Choice 5 The current script indexed by line numbers


I ran the above code in perl and got the following o/p
/ -

stderr -
gl -
utf8:: -
" -  
CORE:: -
DynaLoader:: -
stdout -
attributes:: -
 -
stdin -
ARGV -
INC -
ENV -
PEN - :utf8:utf8
UNIVERSAL:: -
$ - 2007
_<perlio.c - perlio.c
main:: -
- - 0
_<perlmain.c - perlmain.c
PerlIO:: -
_<universal.c - universal.c
0 - ./perl_progs
 - 256
@ -
_<xsutils.c - xsutils.c
STDOUT -
IO:: -
 - /usr/bin/perl
_ -
+ -
STDERR -
Internals:: -
STDIN -
DB:: -
<none>:: -


I am not able to make sense of the output. Can any one tell me if the o/p is one of the following

Choice 1 The names and definitions of all subroutines in the main namespace
Choice 2 A list of all globs contained in the main namespace
Choice 3 The names and scalar contents of all variables in the main namespace
Choice 4 A list of all packages contained underneath the main namespace
Choice 5 The current script indexed by line numbers


Thanks,
Raghu
0
Comment
Question by:raghu_vs
  • 2
4 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 11832145
Compare:

perl -MData::Dumper -e 'print Dumper(\%main::)'
0
 
LVL 18

Expert Comment

by:kandura
ID: 11836100
Sorry, my adsl connection is acting up today.

Anyway, your snippet is attempting to do option 2, more or less. What is doing is getting all keys and values in the %main:: namespace, taking the value as a glob (in an attempt to have *gl contain every possible kind of data), and then coerce it into a string. As you saw in the output, the stringification doesn't work well in most cases.
0
 
LVL 8

Accepted Solution

by:
davorg earned 70 total points
ID: 11838836
This sounds to me like homework :)

Perhaps you can eliminate some things that it definitely isnt?

Choice 1 The names and definitions of all subroutines in the main namespace

Your output includes 'gl' (a variable) andSTDERR (a filehandle) so that doesn't look likely.

Choice 2 A list of all globs contained in the main namespace

Do you know what a glob is? Perhaps you should look that up in your textbook.

Choice 3 The names and scalar contents of all variables in the main namespace

As I said for choice 1, the list contains the names of filehandles - so this doesn't look likely.

Choice 4 A list of all packages contained underneath the main namespace

As with globs, you'll need to know what a package is in order to know if this is what the list contains.

Choice 5 The current script indexed by line numbers

I think it's obvious that it isn't this :)

So you're just left with globs and packages. And if you know the difference then it's easy to find out which it is.

Dave...
0
 

Author Comment

by:raghu_vs
ID: 11840264
oh no! This is not home work. I got this question while taking the brainbench perl practice test.
Anyway thanks for all the input!


Raghu
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to mysql 5 130
Port 80 requests 16 87
Ned Perl Snippet to Read Files in Directory 5 62
create a gui in perl 3 72
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now