Some confusion about references

W

weijiay

I am confused by the behaviour of the following code:

$ranges="3-4";
@$ranges=split(/-/, $ranges);
print $ranges;
print @$ranges;

print $ranges prints out "3-4", while print @$ranges prints out "34".

Shouldnt $ranges now be a reference to an array?

Will
 
X

Xicheng

I am confused by the behaviour of the following code:

$ranges="3-4";
@$ranges=split(/-/, $ranges);
you were using symbolic references, please dont do that unless you do
know what you do.

'split' returns a list of elements and since the default $, is NULL so
when you print an array, all elements in that array are concatenated
one by one without spaces. you might explicitly set $,
{
local $, = ' + ';
print @$ranges;
}

or

print "@$ranges";

then you will know what you get from the array..

Best,
Xicheng
 
P

Paul Lalli

I am confused by the behaviour of the following code:

$ranges="3-4";
@$ranges=split(/-/, $ranges);
print $ranges;
print @$ranges;

print $ranges prints out "3-4", while print @$ranges prints out "34".

Shouldnt $ranges now be a reference to an array?

No.

The above code is a startling example of why you should always enable
strict and warnings. If you had, Perl would have warned you that you
were attempting to use the string "3-4" as a symbolic reference, and
prevented you from doing so. You have created here two separate
entries in the package main's symbol table:
'ranges' => *::ranges,
'3-4' => *{'::3-4'},

You have set the global variable $ranges to the string '3-4', and the
global variable @{"3-4"} to a list containing the string '34'.

Don't do what you did. 1) If your variable starts as a string, keep it
a string, don't try to make it into an array reference. 2) Don't use
symbolic references. 3) Always, yes *always* use strict and warnings.

Paul Lalli
 
S

Scott Bryce

I am confused by the behaviour of the following code:

$ranges="3-4";
@$ranges=split(/-/, $ranges);
print $ranges;
print @$ranges;

print $ranges prints out "3-4", while print @$ranges prints out "34".

On my machine it won't compile.

Can't declare array dereference in my at C:\Scratch\test.pl line 5, near
"$ranges="
Execution of C:\Scratch\test.pl aborted due to compilation errors.
 
P

Paul Lalli

Scott said:
On my machine it won't compile.

Can't declare array dereference in my at C:\Scratch\test.pl line 5, near
"$ranges="
Execution of C:\Scratch\test.pl aborted due to compilation errors.

It compiles just fine if you don't "fix" the OPs code by adding
strictures and lexical declarations...

Paul Lalli
 
S

Scott Bryce

Paul said:
It compiles just fine if you don't "fix" the OPs code by adding
strictures and lexical declarations...

(scratching my head) I thought I had tried it both ways. I see what
happened now. I fixed the OP's code. When It wouldn't compile I remmed
out use strict and use warnings, but left in the lexical declarations.
 
W

Will

ah, that clears it up. And yes, I will use strict from now on. Thanks
for the help!

Will
 
W

Will

(scratching my head) I thought I had tried it both ways. I see what
happened now. I fixed the OP's code. When It wouldn't compile I remmed
out use strict and use warnings, but left in the lexical declarations.

Do you mean it does not compile if u prepend "my" to $ranges? It still
runs for me if I turn off strict.
 
S

Scott Bryce

Will said:
Do you mean it does not compile if u prepend "my" to $ranges? It still
runs for me if I turn off strict.

You could try it and find out. The line my machine choked on was

my @$ranges=split(/-/, $ranges);
 
C

ced

Xicheng said:
you were using symbolic references, please dont do that unless you do
know what you do.

'split' returns a list of elements and since the default $, is NULL so
^^^^^
when you print an array, all elements in that array are concatenated
one by one without spaces. you might explicitly set $,
{
local $, = ' + ';
print @$ranges;
}

or

print "@$ranges";

then you will know what you get from the array..
There's an Interesting side issue though on the subject of '$,'.
Someone
on p5p just proposed that the default for $, be NULL rather than
undefined
which is *the* default:

$ perl -le 'use warnings; print join $,, 1..3'
Use of uninitialized value in join or string at -e line 1.
123

This'd only be an annoyance if you're explicitly joining with
$, -- otherwise, in the case of @$ranges above $, would
be implicitly converted to a NULL in the print.
 
X

Xicheng

forgot to mention "in the list context." in scalar contex, it returns
the number of splited strings.. thanks for correcting me.
There's an Interesting side issue though on the subject of '$,'.
Someone
on p5p just proposed that the default for $, be NULL rather than
undefined
which is *the* default:

$ perl -le 'use warnings; print join $,, 1..3'
Use of uninitialized value in join or string at -e line 1.
123

This'd only be an annoyance if you're explicitly joining with
$, -- otherwise, in the case of @$ranges above $, would
be implicitly converted to a NULL in the print.

This looks interesting, from my "Perl Pocket Reference"(OReilly 4th
edition), the default value is an empty string (not undefined). but I
guess, this special variable is not supposed to be used in an
expression. it just does things which influence the behaviors of ','
operator in some expressions....my 2 cents....

Best,
Xicheng

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top