Perl syntax that I don't understand.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl syntax that I don't understand.
# 1  
Perl syntax that I don't understand.

I'm just trying to confirm that I understand someone's code correctly.

If someone has code that says:
Code:
$foo ||= mysub();

I'm assuming that it means if $foo is nothing or undef, then assign it some value via mysub(). If I'm wrong on this, please let me know.


Also, what's the difference between this:

Code:
$somescalar = [['one','two'],['three','four']];

and this:

Code:
@myarray = (['one','two'],['three','four']);

I'm assuming they both do the same thing and create a new list of lists in memory. The first one is a reference to the list of lists while the other is the actual list(array) of lists itself. If I'm wrong here, please let me know. I'm probably only 40-70 hours into perl. I've coded in other languages before, but syntax is usually the big hurdle for me.

Last edited by mrwatkin; 05-13-2011 at 11:32 AM..
# 2  
For the first part you are right. For the second not really... To understand those two lines of code easier, run this script:
Code:
#!/usr/bin/perl
use Data::Dumper;
@myarray = [['one','two'],['three','four']];
print &Dumper(\@myarray);
@myarray = (['one','two'],['three','four']);
print &Dumper(\@myarray);

It will result in this:
Code:
solaris% ./b.pl 
$VAR1 = [
          [
            [
              'one',
              'two'
            ],
            [
              'three',
              'four'
            ]
          ]
        ];
$VAR1 = [
          [
            'one',
            'two'
          ],
          [
            'three',
            'four'
          ]
        ];

As you can see first line of code resulted in one more "level" of reference. This is because it created single element array that it's only element is reference to array containing references to lower level arrays. In simple words, first code is not what you would usually want to use Smilie
# 3  
@bartus: but he's assigning it into a scalar:
Code:
$somescalar = [['one','two'],['three','four']];

So he'll end up with $somescalar being a reference to an anonymous array.

@mrwatkin:
You are right. The first construct will create a reference to an array. You can access the array elements through -> operator, like
Code:
$el11 = $somescalar->[1][1];   #el11 == "four"
$el11 = $myarray[1][1];  #el11 == "four"

Except the array is initialized straight there, instead of doing:
Code:
@myarray = (['one','two'],['three','four']);
$somescalar = \@myarray;

The advantage of working with references is that when you are passing them through a function, you are just passing an address to memory, instead of copying the whole thing over (passing by value).

BTW, in defining the @myarray you are using the anonymous array construct also: ['one','two'] will yield a reference to an array containing 'one' and 'two'.
# 4  
Quote:
Originally Posted by mirni
@bartus: but he's assigning it into a scalar:
Code:
$somescalar = [['one','two'],['three','four']];

He wasn't assigning into scalar before he edited the post Smilie And I was too lazy to change my post after he edited his.
# 5  
ahhh.... It looked suspicious to me, I have to admit. Smilie

@mrwatkin: Not a good idea to change your original post -- people who come across this thread later will be confused.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #447
Difficulty: Medium
DebianDog is also Puppy Linux.
True or False?

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Don't understand AWK asort behaviour

Hello, I have the following script : BEGIN { print "1 ***"; split("abc",T,""); T="e"; T="z"; T="y"; for (i in T) printf("%i:%s ",i,T); print ""; for (i=1; i<=length(T); i++) printf(T); print "" print "2 ***"; asort(T,U); for (i in U) printf("%i:%s ",i,U); ... (3 Replies)
Discussion started by: jgilot
3 Replies

2. UNIX for Dummies Questions & Answers

I don't understand conditions :(

Hi there, I have a very general question. I'm rather new to (bash) shell scripting and I don't understand how conditions work... I've read numerous tutorials but I don't get it. I really don't. Sometime what I do works, sometime it doesn't and that's frustating. So what's the actual difference... (0 Replies)
Discussion started by: hypsis
0 Replies

3. UNIX for Dummies Questions & Answers

trying to compile and don't understand error message

this is my program i am trying to compile /* filedata -- display information about a file */ #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> /* * use octarray for determing * if permission bits set */ static short octarray = {0400, 0200, 0100,... (2 Replies)
Discussion started by: heywoodfloyd
2 Replies

4. UNIX for Dummies Questions & Answers

Another Simple BASH command I don't understand. Help?

I have a text file called file1 which contains the text: "ls -l" When I enter this command: bash < file1 > file1 file1 gets erased. However if I enter this command: bash < file1 > newfile the output from "ls -l" is stored in newfile. My question is why doesn't file1's text ("ls -l") get... (3 Replies)
Discussion started by: phunkypants
3 Replies

5. Homework & Coursework Questions

I don't understand some basics..

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1)find all lines in file ,myf that contain all the words cat dog and mouse in any order and start with the letter... (1 Reply)
Discussion started by: cudders
1 Replies

6. Shell Programming and Scripting

Don't understand how RS functions in awk

I learn using RS in awk to extract portion of file in this forum which is wonderful solution to the problem. However, I don't understand how exactly it operates. I don't quite understand the mechanism behind how searching for /DATA2/ can result in extracting the whole section under "DATA2" ... (3 Replies)
Discussion started by: joe228
3 Replies

7. Solaris

I don`t understand how It work (about startup script)?

Hi all. The startup script in /usr/local/bin. After user login the script run an application. Iwould in the same way run the another application. How to make It similar? Where I must to look? Regards. (3 Replies)
Discussion started by: wolfgang
3 Replies

8. UNIX for Advanced & Expert Users

don't understand the unix script

if {"$my_ext_type" = MAIN]; then cd $v_sc_dir Filex.SH $v_so_dir\/$v_fr_file Can somebody tell me what does this suggest. I am pretty new to unix and I am getting confused. What i understood from here is If we have a file extension name as MAIN which we have then we change the directory to... (1 Reply)
Discussion started by: pochaman
1 Replies

Featured Tech Videos