Paul wrote:
> > >   my $x = do { code };
> > > works, because do flips the context, expecting a block and
> > > returning a value.
> >
> > I hadn't actually thought of it that way, but it's remarkably well
> > explained. Well done.
>
> Thanks!
>
> > > That way I can say
> > >   my $fh = do { local $_ };
> > >   open $fh, $file or die $!;
> >
> > Ah, now that's not quite right. The 'do' block will create a
> > local copy of $_, initialised to 'undef'. The end of the block
> > returns 'undef' as it is the last executed line, and $_ will
> > be restored from its saved value. The line is therefore the
> > same as:
> >
> >     my $fh = undef;
> >
> > or just
> >
> >     my $fh;
> >
> > and the entire code works as:
> >
> >     open my $fh, $file or die $!;
>
> I goofed. Should have been
>
>   my $fh = do { local *_ };

That doesn't do anything useful either. My guess is that Perl will
attempt to stringify the typeglob and put something like
qw( *package::_ ) into $x. If you've got this:

    my $fh = do { local *_ };
    open $fh, $file;

then  you will be opening the file on a _very_ oddly-named
handle. Try opening and reading from two files at once in the
same way! If you have this instead:

    my $fh = do { local *_; \*_ };
    open $fh, $file;

then you are making $fh a reference to a typeglob, which can
be used in place of a real typeglob in most places (if not
everywhere?). However, what this does:

    open my $fh, $file;

is to autovivify a typeglob for you and set $fh to a reference
to it, so you get the same effect. In only one line!

>
> which can be passed to things that are expecting a filehandle, and NOT
> a plain scalar.

Well you can pass a scalar, but this is the same sin as using symbolic
references. If you pass a plain scalar as a filehandle you will be
specifying the filehandle by _name_. Should be safe though, as long as
it's something obscure like *main::_ :-D

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to