Glob Q

J

J Krugman

Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

Thanks!

jill
 
J

John Bokma

J said:
Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

Don't use wrong tech lingo. I wouldn't call any of those a glob.
 
J

Jürgen Exner

J said:
Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

I agree, it can be tricky for people who are not used to
Unix-style/csh-style globbing.
Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

I must be missing something. What do those variable definition have to do
with glob()?

jue
 
M

Mark Seger

I think what the author of the note may be asking about is the
difference between 'my', 'local' and no explicit declaration which
effects scope and not globs. I believe, but am also sure someone else
will procide a more precise definition, is that 'my' limits the scope to
the block in which it's asserted, 'local' carries the scope into other
blocks that are entersd from the one in which it's initially asserted
and if declaration is asserted, the variable becomes global. sort of... :cool:
-mark
 
M

Mark Seger

I must be missing something. What do those variable definition have to do
with glob()?

I think what the author of the note may be asking about is the
difference between 'my', 'local' and no explicit declaration which
effects scope and not globs. I believe, but am also sure someone else
will procide a more precise definition, is that 'my' limits the scope to
the block in which it's asserted, 'local' carries the scope into other
blocks that are entersd from the one in which it's asserted and if there
is no explicit declaration, the variable is global. sort of...
-mark
 
S

Shawn Corey

John said:
J Krugman wrote:




Don't use wrong tech lingo. I wouldn't call any of those a glob.

What he is talking about are type globs, to be confused with file globs.
File globs are used with the function glob().

Type globs are used to alias variables. Perl 4 did not have references,
so you had to use type globs instead. In Perl 5, use references except
for file handles, then 'use FileHandle;'

Type globs, like formats, left over from Perl 4 and people rarely use
them since there are better ways to do things.


--- Shawn
 
X

xhoster

John Bokma said:
Don't use wrong tech lingo. I wouldn't call any of those a glob.

It seems to me that perldoc would. Perhaps you can suggest a better term?

Xho
 
J

J Krugman

In said:
J Krugman wrote:
Don't use wrong tech lingo. I wouldn't call any of those a glob.

Ignorance is OK. Righteous ignorance is insufferable:

r news.panix.com ...
Organization: (NONE)
X-No-Confirm: yes

In said:
J Krugman wrote:
Don't use wrong tech lingo. I wouldn't call any of those a glob.

Ignorance is OK. Righteous ignorance is insufferable:

% perl -de 1

Loading DB routines from perl5db.pl version 1.23
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 1
DB<1> x \*VAR
0 GLOB(0x8448028)
-> *main::VAR


jill
 
J

Jim Keenan

J said:
Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

Other than telling you that typeglobs are rarely used these days, I
don't think anyone has grappled with your original question. OTOH, I've
puzzled for days over what the context of your question is.

Suppose that I simply evaluated the 3 variables above:

use Data::Dumper;

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

print "$x\n";
print "$y\n";
print "$z\n";

print Dumper($x, $y, $z);

All I get is:

*main::VAR
*main::VAR
*main::VAR
$VAR1 = *::VAR;
$VAR2 = *::VAR;
$VAR3 = *::VAR;

So, at one level, there is no difference among those three things, if by
"difference" we require as a necessary aspect, "must evaluate to
different things."

But I suspect that's not what you meant. Can you give us a more
concrete, contextualized example of a typeglob used in a way you don't
understand?

jimk
 
S

Shawn Corey

Jim said:
Other than telling you that typeglobs are rarely used these days, I
don't think anyone has grappled with your original question. OTOH, I've
puzzled for days over what the context of your question is.

Suppose that I simply evaluated the 3 variables above:

use Data::Dumper;

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

Actually, I'm still trying to figure out what the following is doing:

my $y = local *VAR;

This is (I think) create a local variable VAR and assign its type glob
to $y. Since VAR has no value, why would you want to do this? Shouldn't
this be:

local $y = *VAR;

--- Shawn
 
J

John Bokma

Shawn said:
What he is talking about are type globs, to be confused with file globs.
File globs are used with the function glob().

Ouch, yeah, I read file globs :-D. Apologies to the OP.
 
S

Steven Kuo

Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;
my $y = local *VAR;
my $z = do { local *VAR };

Thanks!

jill




Imagine replacing that typeglob with corresponding package-scoped
variables. Your declaring a local scope to the typeglob is virtually
the same as localizing package-scoped variables (or redefining a
function).

Tyepglobs allow you to reference entries in the symbol table; the
lexical variables in your example become references to scoped package
variables. It's really nothing more than that.

Compare:


#!/usr/bin/perl
use strict;
use warnings;
use vars qw(*VAR);

my $x = *VAR;
$$x = "bar";
@$x = ( "FOOBAR\n");

my $z = do { local *VAR };
@$z = ("HELLO", "WORLD");


{
my $y = local *VAR;
$$y = "foo";
print $VAR;
}

print $VAR, "\n";
print @VAR;
print "@$z\n";

########################################################################

#!/usr/bin/perl
use strict;
use warnings;
use vars qw(@VAR $VAR);

my $x = \$VAR;
my $x2 = \@VAR;
$$x = "bar";
@$x2 = ( "FOOBAR\n");

my $z = do { local @VAR; \@VAR };
@$z = ("HELLO", "WORLD");


{
local $VAR;
my $y = \$VAR;
$$y = "foo";
print $VAR;
}

print $VAR, "\n";
print @VAR;
print "@$z\n";
 
A

Anno Siegel

J Krugman said:
Why is it that I find globs so mysterious? Am I alone in this?
Or is it a common thing?

Anyway, my question is this: what's the difference between $x, $y,
and $z in

my $x = *VAR;

Here, whoever gets hold on $x can access the package variables $VAR, @VAR,
%VAR, the filehandle and the sub VAR, plus more exotic stuff. The glob
refers to a live variable whose values you may or may not want overwritten.

Its purpose is to create an alias to one or more of the package variables.
This technique was sometimes useful in Perl 4 for parameter passing, but
is now obsolete. References do a better job.
my $y = local *VAR;

Localizing the glob protects the original values of the associated package
variables, but assignments to @$y (say) still assign to @VAR, etc. After
leaving the scope of local(), the original values are restored. Within
the scope there are two different ways of accessing the same locations.

It can be used to produce an otherwise unused glob, usually for the
creation of a file handle. Within the scope of local, the filehandle
would be identical with the filehandle VAR. That's tolerable in a
small scope but is better avoided, in particular if VAR may be used
as a filehandle elsewhere in the program.
my $z = do { local *VAR };

The do-block takes care of that. Now the scope of local is the do-block,
so the glob in $z is essentially anonymous. The original values of
*VAR are unaffected. In particular, if VAR was an open filehandle, it
still is, and the glob in $z has nothing to do with it. It can be
opened to another file without interference.

Therefore, the common idiom to create a glob for a filehandle is
"do { local *VAR }". However, all this is now encapsulated in
the Symbol module and the various *::Handle modules, and ultimately
in open() itself with its autovivification of an undefined value
to a globref. It is rarely necessary to handle globs directly.

The evolution of "lexical filehandles" from typeglobs is an interesting
step. Much like natural evolution made an auditory ossicle from an
extra joint in the jaw (or whatever it was) that was no longer needed,
Perl exploits the fact that typeglobs are no longer needed for parameter
passing and uses them as filehandles.

And Perl's messy IO gets a little messier.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top