Special variable "@$"

N

narra.madan

can any one explain the meaning of the perl special variable @$;
the variable is used in the code..
tha part of the code is shown below..

#! c:\perl\bin\perl

use strict;

my(@fields);

@fields=qw("hello" "how" "are" "u");

print "@fields\n";

&do(\@fields);

print "@fields\n";

sub do
{

my($fields_1,@fields,$var);

$fields_1=@_;

@fields=@$fields_1;

$var=$fields[0];

print "$var\n";
}
 
J

Jürgen Exner

can any one explain the meaning of the perl special variable @$;
the variable is used in the code.. [...]
&do(\@fields);

sub do {
my($fields_1,@fields,$var);
$fields_1=@_;
@fields=@$fields_1;

In this context $fields_1 is a reference to an array and the leading @
dereferences this array.

jue
 
N

narra.madan

is there any problem in the code....no data is being printed in the
subroutine that i passed to it...and evenafter coming back from the
subroutine i tried to print the array but no data is being displayed...
 
H

Heinrich Mislik

can any one explain the meaning of the perl special variable @$;
the variable is used in the code.. [...]
&do(\@fields);

sub do {
my($fields_1,@fields,$var);
$fields_1=@_;
@fields=@$fields_1;

In this context $fields_1 is a reference to an array and the leading @
dereferences this array.

It woulb be, if done right:

($fields_1)=@_;
@fields=@$fields_1;

Greetings

Heinrich
 
G

Guest

: > $fields_1=@_;

Written as it is, $fields_1 holds the number of elements in @_, not more,
not less.

Oliver.
 
D

Dave Weaver

can any one explain the meaning of the perl special variable @$;
the variable is used in the code..
tha part of the code is shown below..

#! c:\perl\bin\perl
use strict;

use warnings;
my(@fields);
@fields=qw("hello" "how" "are" "u");

Better written as:
my @fields = qw(hello how are you);

(Unless you *really* wanted the quotes).
And yes, "you" is better than "u" :)

print "@fields\n";

&do(\@fields);

"do" is a poor name for a subroutine. The casual reader may confuse it
with the "do" keyword.

Do you know what the "&" does on a function call? If not, you don't
need to use it.

deref_test(\@fields);

print "@fields\n";

sub do
{
my($fields_1,@fields,$var);

It's better practice to declare your variables only when you need
them, not well in advance.
$fields_1=@_;

This is your problem. You are using the array @_ in scalar context.
Using an array in scalar context returns the number of elements in the
array (in this case 1, since you passed just one parameter to this
subroutine). To do the assignment is list context:

($fields_1) = @_;

(but $fields_1 is still a horrible name for a variable!)
@fields=@$fields_1;

And now you try to de-reference an integer, giving an error:
Can't use string ("1") as an ARRAY ref while "strict refs" in use at ...
$var=$fields[0];

print "$var\n";
}

Better written as:
sub deref_test {
my ($fields_ref) = @_;

my $var = $fields_ref->[0];

print "Var = '$var'\n";
}

(untested).
 
N

narra.madan

thanks all...my problem is rectified...

i inserted
my(xxxx)=@_;
this just worked fine...
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top