[32494] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3759 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 16 09:09:21 2012

Date: Thu, 16 Aug 2012 06:09:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 16 Aug 2012     Volume: 11 Number: 3759

Today's topics:
    Re: strange warning when open file <peter@makholm.net>
    Re: strange warning when open file <rweikusat@mssgmbh.com>
    Re: strange warning when open file <nospam.gravitalsun@hotmail.com.nospam>
    Re: strange warning when open file <willem@toad.stack.nl>
    Re: strange warning when open file <nospam.gravitalsun@hotmail.com.nospam>
    Re: strange warning when open file <rweikusat@mssgmbh.com>
    Re: strange warning when open file <nospam.gravitalsun@hotmail.com.nospam>
    Re: using cpan effectively <rweikusat@mssgmbh.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 15 Aug 2012 12:31:28 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: strange warning when open file
Message-Id: <87has4jvjz.fsf@vps1.hacking.dk>

"George Mpouras" <nospam.gravitalsun@hotmail.com.nospam> writes:

> Do you have any idea how to fix the warning
> Name "main::FH" used only once: possible typo at C:\work\Files\test.pl
> at the following code ?

And it doesn't tell you a line number where it encounters the name
main::FH?

My perl does, it is on line 16.

>    foreach my $key2 (keys %{$File{$key1}})
>    {
> $File{$key1}{$key2} = { *FH => \"$key1,$key2"  , 'FILE' => "$output_dir/$key1,$key2" };

i.e. this line.

You are using a variable called *FH here and nowhere else in your
script. Which is what perl tries to warn you about.

Variables using the * sigil is a class of variables called
typeglobs. Just like variables starting with the $ sigil ara scalars and
variables starting with the @ sigil is arrays.

Typeglobs are an internal type used by perl to hold symbol table
entries. Previously it was used for references and filehandles, but this
usage is deprecated. You can read more about typeglobs in the perldata
manual page.


What you probably meant was the string 'FH'. You can do this by either
just removing the * and use the stringification feature of the =>
operator or explicitly use a string like you do for the FILE field.

//Makholm


------------------------------

Date: Wed, 15 Aug 2012 12:23:14 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: strange warning when open file
Message-Id: <87vcgko0v1.fsf@sapphire.mobileactivedefense.com>

Peter Makholm <peter@makholm.net> writes:

[...]

> Variables using the * sigil is a class of variables called
> typeglobs. Just like variables starting with the $ sigil ara scalars and
> variables starting with the @ sigil is arrays.
>
> Typeglobs are an internal type used by perl to hold symbol table
> entries. Previously it was used for references and filehandles, but this
> usage is deprecated.

The short way to describe what 'a typeglob' actually is would be 'what
a symbol is in Lisp': An object which aggregates a number of 'slots'
used by the language for different purposes and which is usually
stored in a symbol table where it can be found by doing an 'ordinary'
name lookup which thus provides a way to employ the functionality
provided by the 'slots' by name. For a perl typeglob, the
commonly-used slots would be (assuming the name pointing to the typeglob was
richard) $richard, the slot used for scalars, %richard, the hash slot,
@richard, the array slot and lastly, the &richard slot for
subroutines. The *-notation can be used to refer to the typeglob
itself, eg, in the case of a named I/O handle (another typeglob slot),
the syntax \*richard could be used to pass a reference to that
(actually, to the typeglob itself) to a subroutine (*richard{IO} could
be used to create a reference to the 'thing' in the I/O handle slot).

Another use of *richard would be to assign a reference to something to
one of the slots, eg

*richard = sub { return 'richard'; }

creates an anonymous subroutine returning 'richard' and assigns a reference
to that to the subroutine slot of the glob associated with the name
richard. Effectively, this binds a name to this anonymous subroutine
(or vice versa). This is also the way how importing subroutines from
other modules works.

The reference to references refers to a deficiency of some ancient
programming language someone reportedly used somewhere about twenty
years ago. It ought to be forgotten nowadays. Likewise, in ancient
versions of Perl5, the only way to create an I/O handle was to refer
to a typeglob via the symbol table of the current package, eg

open(IN, '</etc/passwd')

will open the file /etc/passwd for reading and deposit the
corresponding I/O handle in the I/O handle slot of the typeglob
associated with the name IN. Nowadays, it is usually more convenient
to use an 'undefined' scalar variable for this purpose: This cause an
anonymous typeglob to be created and assigned to this variable
(actually, a reference to it is assigned). This has the nice
additional benefit that the filehandle will be closed automatically
when the scalar variable goes out of scope (be warned that this is
another highly blasphemous heresy because 'mighty sheep' garbage
collectors don't provide this functionality ...)


------------------------------

Date: Wed, 15 Aug 2012 20:28:25 +0300
From: "George Mpouras" <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: strange warning when open file
Message-Id: <k0gm7s$odu$1@news.ntua.gr>

The reference to references refers to a deficiency of some ancient
programming language someone reportedly used somewhere about twenty
years ago. It ought to be forgotten nowadays. Likewise, in ancient
versions of Perl5, the only way to create an I/O handle was to refer
to a typeglob via the symbol table of the current package, eg

open(IN, '</etc/passwd')



As you can see Rainer the code is dynamic. In reality the hash is also
dynamic, so I will end up with hundrends of open fileshat they will get
written also dynamic using the hash keys as filehanl pointers
So using a simple keyword like IN is not good enough.
I have to use the full path of hash keys as a unique FH identifier, othelse
all writings will be wrong.
This is why i used the * ... 



------------------------------

Date: Wed, 15 Aug 2012 18:14:09 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: strange warning when open file
Message-Id: <slrnk2npnh.2st.willem@toad.stack.nl>

George Mpouras wrote:
) As you can see Rainer the code is dynamic. In reality the hash is also
) dynamic, so I will end up with hundrends of open fileshat they will get
) written also dynamic using the hash keys as filehanl pointers
) So using a simple keyword like IN is not good enough.
) I have to use the full path of hash keys as a unique FH identifier, othelse
) all writings will be wrong.
) This is why i used the * ... 

That's not how filehandle/fileglob references work.

Try this:

  use strict;
  use warnings;

  my %x;
  $x{one}{FILE} = "testfile-one.txt";
  $x{two}{FILE} = "testfile-two.txt";
  for my $xx (keys %x) {
    open $x{$xx}{FH}, '>', $x{$xx}{FILE} or die "Failed to open: $_\n";
  }
  for my $xx (keys %x) {
    print { $x{$xx}{$FH} } "Text for $xx\n" or die "Failed to print: $_\n";
  }
  for my $xx (keys %x) {
    close $x{$xx}{FH} or die "Failed to close: $_\n";
  }

And also try to figure out why it works.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Wed, 15 Aug 2012 21:40:43 +0300
From: "George Mpouras" <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: strange warning when open file
Message-Id: <k0gqfc$16en$1@news.ntua.gr>

Actually thats what I did. It works ( I wonder why ... )


# Open Files
foreach my $key1 (keys %File)
{
    foreach my $key2 (keys %{$File{$key1}})
    {
    $File{$key1}{$key2} = { FH => undef , 'FILE' => 
"$output_dir/$key1,$key2" };
    open $File{$key1}{$key2}{'FH'}, '>', $File{$key1}{$key2}{'FILE'} or die 
"Could not open file \"$File{$key1}{$key2}{'FILE'}\" because \"$^E\"\n";
    }
} 



------------------------------

Date: Wed, 15 Aug 2012 20:54:07 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: strange warning when open file
Message-Id: <87393oylr4.fsf@sapphire.mobileactivedefense.com>

"George Mpouras" <nospam.gravitalsun@hotmail.com.nospam> writes:
> Actually thats what I did. It works ( I wonder why ... )
>
>
> # Open Files
> foreach my $key1 (keys %File)
> {
>    foreach my $key2 (keys %{$File{$key1}})
>    {
>    $File{$key1}{$key2} = { FH => undef ,

The assignment is not needed: The first time the other code uses
$File{$key1}{$key2}{FH} in a rvalue context which needs a reference
to a 'value' of some type, a suitable value will be created and a
reference to it assigned to the hitherto 'undefined' location
(so-called 'auto-vivification'). One of these contexts is the first
argument to an open call which is either supposed to be the name of a
typeglob or a reference to a type glob, cf the following example:

--------------
open($passwd, '<', '/etc/passwd');
print($passwd, "\n");
--------------

which (on a system where a file named /etc/passwd exists ;-) prints
the reference to the anonymous glob which was assigned to $passwd when
autovivification took place.

As Peter Makholm already wrote earlier: The error in your original
code was that you wrote

*FH => undef

in a hash definition. The =>-operator automaically quotes its left
argument if "it begins with a letter or underscore and is composed
only of letters, digits and underscores" (=> perlop(1)). This is not
the case here, hence *FH is evaluated as an expression. The value of
this expression is the typeglob currently associated with the name FH
in the symbol table of the current package, which causes the warning
to be printed. It will probably also not do what you wanted wrt
creating a hash entry because *FH stringfies to the fully-qualified
name of the referenced typeglob, cf

-----------
my $h = { *FH => 'FH' };

print $h->{*FH}, "\n";

package haha;

print $h->{*FH}, "\n";

print("The solution: ", keys(%$h), "\n");
-----------

[in German, the letter 'h' is pronounced 'ha']


------------------------------

Date: Wed, 15 Aug 2012 23:00:02 +0300
From: "George Mpouras" <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: strange warning when open file
Message-Id: <k0gv43$1nlj$1@news.ntua.gr>

Thanks for the *info !


------------------------------

Date: Wed, 15 Aug 2012 11:50:02 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: using cpan effectively
Message-Id: <871uj8pgyt.fsf@sapphire.mobileactivedefense.com>

Cal Dershowitz <cal@example.invalid> writes:
> I seem to have wiped out my previous perl install when updating
> ubuntu, so I'm back to square one.  I've installed cpan, Data::Dumper,
> YAML, File::Slurp successfully, but hit a snag when installing
> Image::Magick.
>
> What does a person do when make install fails?  The particular error
> looks to be:
>
> [can only paste as quotation]
>>   CPAN.pm: Going to build J/JC/JCRISTY/PerlMagick-6.77.tar.gz
>>
>> Checking if your kit is complete...
>> Looks good
>> Note (probably harmless): No library found for -lMagickCore
>
> ...
>> Magick.xs:60:31: fatal error: magick/MagickCore.h: No such file or directory
>> compilation terminated.

This suggest that your install lacks the 'development package' for the
ImageMagick libaries and possibly, the library package as well.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3759
***************************************


home help back first fref pref prev next nref lref last post