[comp.lang.perl] shift

bjorn@mips.COM (Bjorn Satdeva - /sys/admin Inc) (11/01/90)

Why is the following not legal?

	$OldName = shift( split( '\.', $OldDomain ) );

Split is returnning an array.  Doing a shift on the result, without 
first storing it to a temp variable seems to me it should be a perfectly 
legal thing to to.

What had I misunderstood?

Bjorn Satdeva

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (11/01/90)

In article <42583@mips.mips.COM> bjorn@mips.COM (Bjorn Satdeva - /sys/admin Inc) writes:
: Why is the following not legal?
: 
: 	$OldName = shift( split( '\.', $OldDomain ) );
: 
: Split is returnning an array.  Doing a shift on the result, without 
: first storing it to a temp variable seems to me it should be a perfectly 
: legal thing to to.
: 
: What had I misunderstood?

Perl differentiates between named arrays and array values, also called lists.
A number of operators that alter the state of a list require a named array
rather than an array value, since only named arrays can remember state.

You can, however, do what you want.  Say either of

	$OldName = (split( /\./, $OldDomain)[0];	# or [$[]

or 

	($OldName) = split( /\./, $OldDomain);

The latter is preferred for efficiency, since split to a list is smart about
how many fields it actually has to produce.  Of course, there's always

	($OldName) = /^([^.]*)/;

Larry