Results 1 to 7 of 7
Suggest me any online link where I can see samples of references and get better idea. And please help me in understand following
1)
My $fb_r1
open (FB,"/tmp/test.txt") or die ...
- 02-15-2008 #1Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
Need to understand all type references in perl?
Suggest me any online link where I can see samples of references and get better idea. And please help me in understand following
1)
My $fb_r1
open (FB,"/tmp/test.txt") or die ("unable to open FB file: $!\n");
while (<FB>) {
my $line=$_;
my ($m1,$r1,$s1) = split(":",$line);
$fb_r1->{"$m1"}->{"$m1"}="";
$fb_r1->{"$m1"}->{"$r1"}="";
$r_list->{"$r1"}=" ";
}
close (FB);
2)
my $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh-errstr();
$sth->execute() or die "Cannot execute: " . $sth->errstr();
foreach $source (@source_date){
my $sql = "select test, percent from testing_stat";
while (my $row = $sth->fetchrow_arrayref) {
$list->{@$row[0]}->{$source}=@$row[1];
}
}
Meaning of above given???
I am completely confused in references, please help me in understand and suggest me some online link......Switched to Scripting
- 02-15-2008 #2
I can't speak of any online source, though I suspect that Perl Monks may have a tutorial. I personally learned references from O'Reilly's Intermediate Perl.
However, some of the basics:
Basically, a scalar variable can hold a reference to another value. In this way, it acts basically as a pointer in C: the variable does not hold the value, but rather the location of the value. You can follow the reference to manipulate the value.
Because the variable only holds the location of the value, this means that you can essentially store non-scalar data in a scalar variable. Let's see an example:
Suppose I want a function to accept two different arrays. Without references, this would be rather difficult, since I would need to know how to separate the arrays in the resultant @_. Therefore, my function will accept two arrayrefs instead:
Do you see the @{...} syntax? This means "take the value inside here and dereference it to an array". Because I am using the array, I can do array-specific operations, like push.Code:sub my_sub { my($a1, $a2) = @_; # $a1 and $a2 now both hold arrayrefs push @{$a1}, "val1"; push @{$a2}, "val2"; }
Now then: how do I do something like take the index of an arrayref? This is done with a C-based syntax:
The "->" syntax means "dereference this value as appropriate. Here, since I'm taking an array index (note the "[...]"), it dereferences to an array. Had I used "$a->{'val'}", it would have dereferenced to a hash. The same applies above: "%{$a1}" would dereference to a hash.Code:sub arrayref_index { my $a = shift; print "The first value is: ", $a->[0], "\n"; }
Now then, in the code you have posted:
1) First off, it looks like you're initializing values, and this is generally unnecessary in Perl. Thanks to autovivification, values will spring into existence when you need them.
Anyway, let's look at this line:
Here, $fb_r1 is being used as a hashref. You are looking at the value for the key $m1, which in turn is a hashref. You are then assigning to the value for the key $m1 in this second hashref.Code:$fb_r1->{"$m1"}->{"$m1"}="";
This can be extrapolated to the other two lines.
2) There are two things here. First, you have the lines such as:
Even though these use the "->" syntax, these are actually method calls using object-oriented Perl. I suggest that you learn about object-oriented programming, but we will disregard it here.Code:$dbh->prepare($sql) $sth->execute()
As for what's going on in the loop, we already discussed this at length in the other thread you made a while back. The syntax is rather strange here, because array slices are being taken where they are unnecessary. However, at its most simple:
$list is a hashref, and you are taking one of its values (and the key for that value is in turn taken from $row, which is an arrayref). This value is itself a hashref, and you are taking the value for the key $source. You assign to this from the arrayref that is $row.Code:$list->{@$row[0]}->{$source}=@$row[1];
I hope that this makes sense, and hopefully someone can suggest a good online source.DISTRO=Arch
Registered Linux User #388732
- 02-16-2008 #3
Here's a link for understanding references:
perldsc - perldoc.perl.org--
Bill
Old age and treachery will overcome youth and skill.
- 02-16-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
perldoc perlreftut
- 02-16-2008 #5
ghostdog74's citation (perlreftut - perldoc.perl.org) is better than mine as an intro, though after you've read that one, you'll probably want to read the one I suggested (perldsc - perldoc.perl.org) as well. I'm repeating both of them to improve their form slightly.
--
Bill
Old age and treachery will overcome youth and skill.
- 02-19-2008 #6Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
Thanks all great help
Switched to Scripting
- 02-20-2008 #7Linux Newbie
- Join Date
- Jan 2008
- Posts
- 114
Now I can understand
$ips->{"@$row[0]"}=test_ip(@$row[1]);
$ips was the scalar now which has become a hash ref and we are intializing the key value. But following is little tricky, please help me in understand.
$ips->{"@$row[0]"}->{"@$row[1]"}=test_ip(@$row[1]);
and later in any other function how can I use $ips???Switched to Scripting


Reply With Quote