You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Rob Tanner <rt...@onlinemac.com> on 2000/07/13 22:02:42 UTC

Damian Conway book example (is the bug mine or his?)

Hi all,

Damian Conway's "Object Oriented Perl" has been so highly praised on this 
list that I had to run out an order myself a copy.  The praise is not 
without merit -- it's a marvelous book (and readable too!!!).

On p. 28 of his review of the "essential Perl" he talks briefly about a 
hash slice, and I who have been perling since the old perl 4 days had never 
come across that capability before.  But in a piece of code I'm writing 
right now, I tried it out and I continueally get the following compile time 
error:

     Can't modify single ref constructor in scalar assignment at \
            accntREQUEST.cgi line 154, near ");"

There's always more than one way to skin a cat, and I've tried several. 
This is what I really want to accomplish:

my $colleague = {};
@{$colleague} = { "status",
		  "id",
		  "last_name",
		  "first_name",
		  "middle_name",
		  "campus",
		  "dob"
		 } = split(/:/, $record);

But, I have also tried the following, which is closer to Conway's example:

my %colleague;
@colleague{ .... } = split(/:/, $record);

And I have also created the hash and added the keys prior to attempting the 
insert.  I have even tried putting the split in an additional set of 
parentheses on the notion that maybe I had to make the list context 
explicit.

In every case, I get the error quoted above.  The only thing that changes 
is the offending line number, which is always the last line of the 
instruction.

Does his example not work, or more likely, what am I doing wrong.

-- Rob

       _ _ _ _           _    _ _ _ _ _
      /\_\_\_\_\        /\_\ /\_\_\_\_\_\
     /\/_/_/_/_/       /\/_/ \/_/_/_/_/_/  QUIDQUID LATINE DICTUM SIT,
    /\/_/__\/_/ __    /\/_/    /\/_/          PROFUNDUM VIDITUR
   /\/_/_/_/_/ /\_\  /\/_/    /\/_/
  /\/_/ \/_/  /\/_/_/\/_/    /\/_/         (Whatever is said in Latin
  \/_/  \/_/  \/_/_/_/_/     \/_/              appears profound)

  Rob Tanner
  McMinnville, Oregon
  rtanner@onlinemac.com

Re: Damian Conway book example (is the bug mine or his?)

Posted by Barrie Slaymaker <rb...@telerama.com>.
Rob Tanner wrote:
> 
> 
> my $colleague = {};
> @{$colleague} = { "status",
                ^
Extraneous = op
>                   "id",
>                   "last_name",
>                   "first_name",
>                   "middle_name",
>                   "campus",
>                   "dob"
>                  } = split(/:/, $record);

- Barrie

Re: Damian Conway book example (is the bug mine or his?)

Posted by darren chamberlain <da...@boston.com>.
Rob Tanner (rtanner@onlinemac.com) said something to this effect:
> There's always more than one way to skin a cat, and I've tried several. 
> This is what I really want to accomplish:
> 
> my $colleague = {};
> @{$colleague} = { "status",
> 		  "id",
> 		  "last_name",
> 		  "first_name",
> 		  "middle_name",
> 		  "campus",
> 		  "dob"
> 		 } = split(/:/, $record);

Write it as @{$colleague}{@keys} = (@values), e.g.:

bash $ perl
my $colleague = {};
my $record = join(':',(1..7));
@{$colleague}{"status",
                  "id",
                  "last_name",
                  "first_name",
                  "middle_name",
                  "campus",
                  "dob"} = split(':',$record);
print map { sprintf "%s => %s\n",$_,$colleague->{$_} } keys %{$colleague};
^D
first_name => 4
status => 1
last_name => 3
campus => 6
id => 2
dob => 7
middle_name => 5
bash $

(darren)

-- 
Unix is an operating system, OS/2 is half an operating system, Windows
is a shell, and DOS is a boot partition virus.
-- Peter H. Coffin

Re: [OT] Damian Conway book example (is the bug mine or his?)

Posted by Ken Williams <ke...@forum.swarthmore.edu>.
Please remember, this is the mod_perl list.  Followup to a different
list if this doesn't answer your question, please.


rtanner@onlinemac.com (Rob Tanner) wrote:
>     Can't modify single ref constructor in scalar assignment at \
>            accntREQUEST.cgi line 154, near ");"
[...]
>my $colleague = {};
>@{$colleague} = { "status",
>		  "id",
>		  "last_name",
>		  "first_name",
>		  "middle_name",
>		  "campus",
>		  "dob"
>		 } = split(/:/, $record);


...and that's exactly what you're doing, assigning to a single ref
constructor.  Just like saying the following, which is a syntax error:

  {one => 1} = (2,3);



You mean:

my $colleague = {};
@{$colleague}{"status",
              "id",
              "last_name",
              "first_name",
              "middle_name",
              "campus",
              "dob"
             } = split(/:/, $record);


  -------------------                            -------------------
  Ken Williams                             Last Bastion of Euclidity
  ken@forum.swarthmore.edu                            The Math Forum