CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem with scalar reference

 



Cupidvogel
Novice

Mar 29, 2012, 10:06 PM

Post #1 of 3 (1099 views)
Problem with scalar reference Can't Post

Hi, can anyone explain why the second example in this code shows syntax error and the third example prints out Not a SCALAR reference, while the first example runs as expected?


Code
my @a = @{[do { local $_ = "fool\n"; chomp; $_ ; } ]}; 
print "@a"; ## prints "fool"
$b = ${do { local $_ = "cool\n"; chomp; $_; }};
print "\n$b";
$c = ${do { local $_ = "cool\n"; chomp; $_; }};
print "\n$c";



histrung
Novice

Apr 1, 2012, 7:09 PM

Post #2 of 3 (1079 views)
Re: [Cupidvogel] Problem with scalar reference [In reply to] Can't Post

Because the first line is a casting and the other two are not a casting.

Code
#!/usr/bin/perl 

# This is casting an array reference to an array
my @a = @{[do { local $_ = "fool\n"; chomp; $_ ; } ]};
print "@a"; ## prints "fool"

# The {} create an anonymous hash.
# This is NOT a casting
# $b = ${do { local $_ = "cool\n"; chomp; $_; }};

# This sets $b to be a reference to an anonymous hash
$b = {do { local $_ = "cool\n"; chomp; $_; }};
print "\n$b";

# The {} create an anonymous hash.
# This is NOT a casting
# $c = ${do { local $_ = "cool\n"; chomp; $_; }};

# This sets $c to be the scalar of an array of
# keys to an anonymous hash
$c = keys(%{{do { local $_ = "cool\n"; chomp; $_; }}});
print "\n$c";

# This set $c to the value of the number of used buckets
# and the number of allocated buckets, separated by a slash.
# http://perldoc.perl.org/perldata.html 1/4 down the page
$c = %{{do { local $_ = "cool\n"; chomp; $_; }}};
print "\n$c";
print "\n";

Output

Code
$ ./b.pl 
fool
HASH(0x100804ff0)
1
1/8
$



Cupidvogel
Novice

Apr 4, 2012, 12:57 PM

Post #3 of 3 (890 views)
Re: [histrung] Problem with scalar reference [In reply to] Can't Post

Thanks. Got it.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives