Solved

Make sense of the output

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

12 Experts available now in Live!

Get 1:1 Help Now