Solved

Make sense of the output

Posted on 2004-08-18
4
207 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

825 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