[26072] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8274 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Jul 24 18:05:26 2005

Date: Sun, 24 Jul 2005 15:05:07 -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           Sun, 24 Jul 2005     Volume: 10 Number: 8274

Today's topics:
    Re: "Can't return a temporary from lvalue subroutine... (Anno Siegel)
    Re: copy contructor (Anno Siegel)
    Re: How does a scalar know what it is? <lfs.1@spam.net>
        Question about hashes of hashes <djacober@swissonline.ch>
    Re: Question about hashes of hashes <noreply@gunnar.cc>
    Re: Question about hashes of hashes <sven-thorsten.fahrbach@gmx.net>
    Re: Question about hashes of hashes <djacober@swissonline.ch>
    Re: Question about hashes of hashes <djacober@swissonline.ch>
    Re: Question about hashes of hashes <noreply@gunnar.cc>
    Re: Question about hashes of hashes <sven-thorsten.fahrbach@gmx.net>
    Re: Question about hashes of hashes <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 24 Jul 2005 18:06:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: "Can't return a temporary from lvalue subroutine..."
Message-Id: <dc0lc1$6nf$1@mamenchi.zrz.TU-Berlin.DE>

Ilya Zakharevich  <nospam-abuse@ilyaz.org> wrote in comp.lang.perl.misc:
> [A complimentary Cc of this posting was sent to
> Anno Siegel
> <anno4000@lublin.zrz.tu-berlin.de>], who wrote in article
> <dbgjls$l1e$1@mamenchi.zrz.TU-Berlin.DE>:
> > > What "temporary" is the error message referring to?  Is there a
> > > work around?
> > 
> > It looks like "temporary" refers to the state of a scalar of being
> > "mortal".  Mortal SVs are all over the Perl source, but normally
> > only concern XS programmers.  This is the first time I see the
> > distinction appear at Perl level.
> 
> There is no way to determine that a thing is mortal (short of walking
> up the mortalization stack and checking whether a value is there).

Oh.  I thought of it as some kind of flag in the SV.  Tassilo had already
hinted as much.  Obviously a little knowledge is a dangerous thing.
Apologies for posting a mere guess.

> So, obviously, it is not that.  Here "a temporary" is an intermediate
> variable created by Perl without a direct intervention of a programmer.
> 
> I'm afraid that it was me who put this error message there; so all the
> blame on its wording may be on me.  When I introduced the lvalue subs,
> there was a lot of suspicion on p5p that the whole idea is junk; as a
> result, the handling is over-protecting.  This "can't" in the message,
> for example, is red herring; there would be no problem for Perl to
> change a value of a temporary; it is just that usually it is not what
> a programmer wants:
> 
>   sub add5 : lvalue {
>       my $x = shift;
>       return $x + 5;
>   }

Ah, but that it apparently caught in a different place:

    Can't modify addition (+) in lvalue subroutine return at ...

> So the main usage of this message is to warn the programmer of
> *possibly* flaky code.
> 
> The problem is that the current implementation of tied hashes is using
> a temporary to handle access to a value for a key.  I did not realize
> this when implementing the lvalue subs, so there is no special case to
> handle this "legitimate" use of temporaries in lvalue subs.

Apparently Tassilo has negotiated that narrow passage when he fixed it.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

Date: 24 Jul 2005 18:41:54 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: copy contructor
Message-Id: <dc0ndi$7pg$1@mamenchi.zrz.TU-Berlin.DE>

Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXLIV
> September MCMXCIII in <URL:news:dbu3l0$nf3$1@mamenchi.zrz.TU-Berlin.DE>:
> {}  Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> {} > Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMMCCCXLI
> {} > September MCMXCIII in <URL:news:dbl9pb$g3r$2@mamenchi.zrz.TU-Berlin.DE>:
> {} > 
> {} > > [[ Snipped for brevity ]]
> {} > 
> {} > 
> {} > Here's how I would subclass Angry::Snake. Note that I subclass the
> {} > original Angry::Snake, without requiring it to have an "accessor".
> {}  
> {}  Ah, but in my book (that would be a little paper I'm writing about
> {}  inheritance in Perl) it *has* an accessor.  Any method that accesses the
> {}  object by de-referencing the object is an accessor, whether it returns
> {}  the value to the caller or does something else with it.  That would make
> {}  ->poke_it_with_a_stick an accessor.  A field accessor in the usual
> {}  sense is just a special case.  In most mainstream OO languages there
> {}  are no accessors except field accessors, so the two notions coincide,
> {}  but Perl is different.
> 
> Oh, it has an accessor allright. Whether or not the super class has
> an accessor is not important. Or rather, it's irrelevant. And that's
> (IMO) the beauty of Inside-Out Objects. It's _irrelevant_ whether the
> superclass has accessors. Or attributes/fields. Or how it stores them.
> How the superclass is implemented does not matter - all that matters
> is its API. And the whole implementation can change, accessors can turn
> into non-accessors, or visa versa, if the API remains the same, there's
> no need to update the inheriting class.

Well, paradise.  A sunlit Caribbean island with no inheritance tax.  The
one thing I don't see at a glance is how you would do multiple inheritance
that way.  Your object *is* an object of the base class, only blessed
into yours.  It can't be physically be many structures at once.

> {}  That's just terminology.  Maybe I should think it over and find a better
> {}  term.  But what can you call a method whose distinguishing property is that
> {}  it accesses the object?
> 
> I don't really care how you call them. I don't think a different term from
> them is important. In fact, when I call methods in a class, I do not want
> to have to know whether it's an accessor or not. When I look to a class
> from anywhere but the class itself, all I should see is methods. If I
> need to know (perhaps to do something different) that a method is an
> "accessor", something is wrong. IMO, it breaks encapsulation, and OO with
> broken encapsulation is no fun at all.

I agree.  In inheritance paradise there is no need for the term "accessor".
But that blissful state is not where programmers find themselves who
go about Perl OO naively, or follow what they are taught in most tutorials.

To see why and how things go wrong, and what to do about it, I think I
need the term.  But that's no concern of yours, you're right to point that
out.

> {} > Note that Sleepy::Snake doesn't have its own constructor.
> {}  
> {}  No, it can't.  One of the tenets about Perl inheritance is "If you
> {}  inherit an accessor, you must also inherit ->new", which is what happens
> {}  here.  Inherit as opposed to override.
> 
> Sure it can have its own constructor. I just decided not to. I prefer
> not to configure an object (that is, setting attribute values) in a
> constructor because that makes multiple inheritance a real pain in the
> ass, so I prefer to only have constructors in classes that don't inherit
> other classes, and such constructors should only return the blessed
> reference, and do not anything else. Having said, if I were to create
> a constructor for the Sleepy::Snake class, I'd do it like this:
> 
>     sub new {
>         my $snake = Angry::Snake -> new;     # Let the class set itself up
>                                              # in whatever way it pleases.
>         $sleepiness {refaddr $snake} = 0.5;  # Default value.
>         bless $snake => shift;               # Bless it to our class.
>     }
> 
> {}  Like all short formulas about complex fields (except exp(i*pi) = -1, hehe),
> {}  it is wrong.  You can override ->new, provided you call the base class'
> {}  ->new to create the object.  Your objects must be structurally the same
> {}  as those of the base class if you want the base class accessors to work
> {}  on them.  Put like that it's a truism.
> 
> Yes. But that's the beauty of Inside-Out Objects. It doesn't care what
> structure the super class has, because it doesn't use the structure at
> all. All it needs is the memory address.
> 
> {}  However, in package Sleepy::Snake it would be safe to override ->new
> {}  
> {}      sub new { shift()->SUPER::new->set_sleepiness( 0.5) }
> {}  
> {}  so as not to create snakes with undefined sleepiness.
> 
> That works as well as the constructor I used above. But it makes 
> multiple inheritance a lot harder.

As mentioned above, that was my first concern about your approach.  How
do you inherit from more than one class in this way?  Could you elaborate?

[some snippage]

> Even with Inside-Out Objects, we have:
> 
>     push @{$array {refaddr $_ [0]}} => 'one', 'two', 'three';
> 
> But I've fixed that as well. With Lexical::Attributes, I just write:
> 
>     push @.array => 'one', 'two', 'three';
> 
> But that's for a different thread.
> \end{side-remark}

Huh, what's that?  A source filter?  Oh, never mind.  I'll look if I must
know.

I find this thread very worthwhile.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

Date: Sun, 24 Jul 2005 15:08:11 -0500
From: "Tom" <lfs.1@spam.net>
Subject: Re: How does a scalar know what it is?
Message-Id: <3sGdnefEatKaaH7fRVn-pA@comcast.com>


"Dave" <daveandniki@ntlworld.com> wrote in message
news:LxLEe.15418$Ag3.7534@newsfe4-gui.ntli.net...
> I think this is what you want to read:
>
> From Klaus' link:
> http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlguts.pod#Datatypes
>
> or type:
>
> perldoc perlguts
>
> at the CLI.
>
> Dave
>
> "Tom" <lfs.1@spam.net> wrote in message
> news:toqdnVRhz5Wr3X7fRVn-og@comcast.com...
> > In C, I can just read the K&R book and know
> > how everything is pretty much coded at the
> > machine level.  At least to some extent.
> >
> > But how does Perl know when you type
> >
> > print $ref
> >
> > for example that it is not an integer, string or what have
> > you?
> >
> > And how does
> >
> > print $num;    2  or "Two"
> >
> > again at the machine level, how does perl know
> > what it has?
> >
> >
> >
> >
>
>

It looks to me that these scalars are carrying around
alot of baggage.  Does it need to carry this baggage
on the function calls?




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

Date: Sun, 24 Jul 2005 22:39:31 +0200
From: djacober <djacober@swissonline.ch>
Subject: Question about hashes of hashes
Message-Id: <dc0u0m$1ih$1@news.hispeed.ch>

Hi all

I tried to pass a hash of hashes as a reference to a subroutine but 
everytime I try to make an operation with to the hash I get the 
following mistake. I found a lot of information on the web but it's 
still not clear to me what I can do about it

Here's the error message I get:

djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
Can't use string ("1") as a HASH ref while "strict refs" in use at 
detect_eyes.pl line 126.

Here's a the snippet of my code that causes the headache:

sub detect_eye {

   my $ref_color = @_;
   foreach my $line (@board_number) {
     VALUE: foreach my $alpha (@board_alpha) {

## this line doesn't work !!!!
126:      print "$line,$alpha : $ref_color->{$line}{$alpha} \n";


Here's how I call the subroutine:

sub main {
   read_board();
   detect_eye(\%white);
}


Can any one help?

Thanks for your support
Daniel


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

Date: Sun, 24 Jul 2005 22:42:17 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Question about hashes of hashes
Message-Id: <3kicpcFu94mnU1@individual.net>

djacober wrote:
> 
> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> Can't use string ("1") as a HASH ref while "strict refs" in use at 
> detect_eyes.pl line 126.

<random code snippets snipped>

> Can any one help?

If you make it easy to help, I'm sure someone can.

Please post a short but _complete_ program that people can copy and run 
and that exhibits the problem you're having!

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 24 Jul 2005 23:17:42 +0200
From: Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net>
Subject: Re: Question about hashes of hashes
Message-Id: <20050724231742.6eeb0ab2.sven-thorsten.fahrbach@gmx.net>

On Sun, 24 Jul 2005 22:39:31 +0200
djacober <djacober@swissonline.ch> wrote:

> Hi all
> 
> I tried to pass a hash of hashes as a reference to a subroutine but 
> everytime I try to make an operation with to the hash I get the 
> following mistake. I found a lot of information on the web but it's 
> still not clear to me what I can do about it
> 
> Here's the error message I get:
> 
> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
> Can't use string ("1") as a HASH ref while "strict refs" in use at 
> detect_eyes.pl line 126.
> 
> Here's a the snippet of my code that causes the headache:
> 
> sub detect_eye {
> 
>    my $ref_color = @_;

Wrong context. This doesn't mean 'give me the first element of @_' (which is what you probably had in mind) but 'give me the number of elements in the list'. You pass the hashref \%white to the sub, so @_ contains one element, i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use string ("1")...'. What you want to do is either:
	1 - The C way   : my $ref_color = $_[0];	or
	2 - The Perl way: my $ref_color = shift;
shift() operates on @_ if no additional arguments are given and you should use it instead of accessing individual array elements by their subscripts, which is slower.
perldsc is also a good reference if you run into trouble with hashes of hashes or other data structures.


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

Date: Sun, 24 Jul 2005 22:55:27 +0200
From: djacober <djacober@swissonline.ch>
Subject: Re: Question about hashes of hashes
Message-Id: <dc0uug$3ql$1@news.hispeed.ch>

Gunnar Hjalmarsson wrote:
> djacober wrote:
> 
>>
>> djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
>> Can't use string ("1") as a HASH ref while "strict refs" in use at 
>> detect_eyes.pl line 126.
> 
> 
> <random code snippets snipped>
> 
>> Can any one help?
> 
> 
> If you make it easy to help, I'm sure someone can.
> 
> Please post a short but _complete_ program that people can copy and run 
> and that exhibits the problem you're having!
> 

Sorry I didn't want to overload with code:

Here's the complete code

Regards Daniel

#!/usr/bin/perl
#
#################################################################################
#
# detect_eyes.pl        : perl program to find the double eyes in a go game
#
# Filename              : detect_eyes.pl
#
# Version               : 1.0
#
# Author                : D. Jacober, DSAI 2001 8. Semester KI
# Date                  : 05.07.2005
# Changes               :
#
# Input                 : Text file on the command line with a go board
# Output                : Displays double eyes on the go board
#
#################################################################################

use strict;
use warnings;
use Time::localtime;
use IO::Handle;
###use Sort::Fields;

# Variablen deklarieren
my $infile = shift || "example.txt";
my $outfile = shift || "double_eyes.txt";
my %board = ();
my %white = ();
my %black = ();
my %empty = ();
my @open = ();
my @closed = ();
my @current = ();
my @board_number = (1,2,3,4,5);
my @board_alpha = ("A","B","C","D","E");
my $max_num = 5;
my $min_alpha = "A";
my $max_alpha = "F";

#################################################################################
# sub to read input file
#################################################################################

sub has_neighbour {
   my ($line,$alpha) = @_;
   my $value = $white{$line}{$alpha};
   my $next_alpha=chr(ord($alpha)+1);
   my $before_alpha=chr(ord($alpha)-1);
   my $has_neighbour = 0;
   my @new_member = ();

   LINE: foreach my $num ($line-1,$line,$line+1) {
     next LINE if ( ($num < 1) || ($num > $max_num) );

     ALPHA: foreach my $char ($before_alpha,$alpha,$next_alpha) {
       next ALPHA if ( ($num == $line) && ($alpha eq $char) );
       next ALPHA if ( ($char lt $min_alpha) || ($char gt $max_alpha) );

       if ( exists($white{$num}{$char}) && $value eq $white{$num}{$char}) {

         foreach my $in_open (@open) {
           next LINE if ("$num $char" eq $in_open);
         }

         foreach my $in_current (@current) {
           next LINE if ("$num $char" eq $in_current);
         }

         print "inserting $num $char in open\n";
         $new_member[$has_neighbour++] = "$num $char";
       }

     }
   }

   push(@new_member, @open);
   @open = @new_member;
   return $has_neighbour;
}

sub read_board {
   my @alpha_coordinate;

   open(INFILE,$infile) or die " cannot open $infile ";

   while(<INFILE>) {
     s/^\s//g;
     chomp;
     if (s/^\///) {
       s/^\s//g;
       @alpha_coordinate = split(/ /);
     } elsif (s/^(\d+)//) {
       my $line_no = $1;
       s/^\s//g;
       my @line = split / /;
       my $n = 0;
       foreach my $field (@line) {

         #full board with all figures
         $board{$line_no}{$alpha_coordinate[$n]} = $field;

         #separate hashes for each color type (black, white and empty)
         if ($field =~ /0/) {
           $white{$line_no}{$alpha_coordinate[$n]} = $field;
         } elsif ($field =~ /X/) {
           $black{$line_no}{$alpha_coordinate[$n]} = $field;
         } else {
           $empty{$line_no}{$alpha_coordinate[$n]} = $field;
         }

         $n++;
       }
     } else { die "wrong input line \n"; }
   }
   close(INFILE);

}

sub detect_eye {

   my $ref_color = @_;
   foreach my $line (@board_number) {
     VALUE: foreach my $alpha (@board_alpha) {
       print "$line,$alpha : $ref_color->{$line}{$alpha} \n";

       if (exists($white{$line}{$alpha}) ) {

         if (has_neighbour($line,$alpha) > 1) {unshift(@current, "$line 
$alpha");}

         while ( $#open != -1 ) {
           print "@open \n";
           print "@current \n";
           my ($num, $char) = split(/ /,shift(@open));

           if (has_neighbour($num,$char) ) {unshift(@current, "$num 
$char");}

         }
         exit;
       }

     }
   }
}

sub main {
   read_board();
   detect_eye(\%white);
}

main();




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

Date: Sun, 24 Jul 2005 22:57:44 +0200
From: djacober <djacober@swissonline.ch>
Subject: Re: Question about hashes of hashes
Message-Id: <dc0v2k$3ql$2@news.hispeed.ch>

Sven-Thorsten Fahrbach wrote:
> On Sun, 24 Jul 2005 22:39:31 +0200
> djacober <djacober@swissonline.ch> wrote:
> 
> 
>>Hi all
>>
>>I tried to pass a hash of hashes as a reference to a subroutine but 
>>everytime I try to make an operation with to the hash I get the 
>>following mistake. I found a lot of information on the web but it's 
>>still not clear to me what I can do about it
>>
>>Here's the error message I get:
>>
>>djacober@tux ~/perl $ perl -w detect_eyes.pl > eyes.out
>>Can't use string ("1") as a HASH ref while "strict refs" in use at 
>>detect_eyes.pl line 126.
>>
>>Here's a the snippet of my code that causes the headache:
>>
>>sub detect_eye {
>>
>>   my $ref_color = @_;
> 
> 
> Wrong context. This doesn't mean 'give me the first element of @_' (which is what you probably had in mind) but 'give me the number of elements in the list'. You pass the hashref \%white to the sub, so @_ contains one element, i.e. 1 is being assigned to $ref_color, hence the error message 'Can't use string ("1")...'. What you want to do is either:
> 	1 - The C way   : my $ref_color = $_[0];	or
> 	2 - The Perl way: my $ref_color = shift;
> shift() operates on @_ if no additional arguments are given and you should use it instead of accessing individual array elements by their subscripts, which is slower.
> perldsc is also a good reference if you run into trouble with hashes of hashes or other data structures.

Yes you're a Genious so simple but took me some hours of my life seeking 
in the docs

Thanks a lot

Regards Daniel


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

Date: Sun, 24 Jul 2005 22:55:20 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Question about hashes of hashes
Message-Id: <3kidhqFujccnU1@individual.net>

djacober wrote:
> Gunnar Hjalmarsson wrote:
>> djacober wrote:
>>> Can any one help?
>>
>> If you make it easy to help, I'm sure someone can.
>>
>> Please post a short but _complete_ program that people can copy and 
>> run and that exhibits the problem you're having!
> 
> Sorry I didn't want to overload with code:
> 
> Here's the complete code

Well, I didn't ask for the _complete_ code either. ;-)

Now, Sven-Thorsten found the mistake anyway, but for next time, please 
study the posting guidelines for this group to better understand what I 
was after.

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Sun, 24 Jul 2005 23:30:05 +0200
From: Sven-Thorsten Fahrbach <sven-thorsten.fahrbach@gmx.net>
Subject: Re: Question about hashes of hashes
Message-Id: <20050724233005.160f2ad0.sven-thorsten.fahrbach@gmx.net>

On Sun, 24 Jul 2005 22:57:44 +0200
djacober <djacober@swissonline.ch> wrote:

> Yes you're a Genious so simple but took me some hours of my life seeking 
> in the docs
> 
> Thanks a lot
> 
> Regards Daniel

This won't be the last time you encounter something like that ;-). It's often the most simple things that take longest to fix.
Happy coding.

SveTho


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

Date: Sun, 24 Jul 2005 23:01:20 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Question about hashes of hashes
Message-Id: <3kidt2Fu7q4nU1@individual.net>

Sven-Thorsten Fahrbach wrote:
> djacober wrote:
>>
>>sub detect_eye {
>>
>>   my $ref_color = @_;
> 
> What you want to do is either:
> 	1 - The C way   : my $ref_color = $_[0];	or
> 	2 - The Perl way: my $ref_color = shift;

or

     3 - Another Perl way: my ($ref_color) = @_;

TIMTOWDI

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 V10 Issue 8274
***************************************


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