Home: Perl Programming Help: Beginner:
Problem Understanding Perl Syntax



mar85
Novice

Jan 14, 2013, 12:45 AM


Views: 1974
Problem Understanding Perl Syntax

Hi, I'm a newbie in Perl and I am trying to understand these lines:

Code
foreach (@list_of_libs){ 
if(-e $_) {
@list = split('/',$_);
$target_lib_names = $list[-1];
open (MAIN_LIB, "<", $_) || die $!
}
}

I don't understand what is -e, is it a switch?
and what does -1 mean inside this line $list[-1]?
And also, what does die $! means? Is it to terminate the MAIN_LIB file?

Please help, thanks


7stud
Enthusiast

Jan 14, 2013, 1:17 AM


Views: 1970
Re: [mar85] Problem Understanding Perl Syntax


Quote
I don't understand what is -e, is it a switch

perl has a feature called 'file tests', which look like command line switches. The -e file test returns true or false depending on whether the file with the given name *exists* or not.

To test whether a file with the specified name exists and whether it is readable and writable, you can write this:

Code
if (-e -r -w $_) {. 

}


To find out more about how file tests work, search google, or better yet buy the book "Learning Perl 6th edition".

Note that using $_ in your code is frowned upon. Here is a better way:

Code
for my $fname (@list_of_libs){  
if(-e -r -w $fname) {

}
}


You should also declare your variables with my() the first time you use them. And "for" can be used instead of "foreach", and it is shorter to type.




Quote
and what does -1 mean inside this line $list[-1]?





Code
use strict;  
use warnings;
use 5.012;

my @data = ('a', 'b', 'c');
say $data[1];
say $data[-1];

--output:--
a
c

Using an index of -1 is a convenient way to get the last element of an array.


Quote
And also, what does die $! means? Is it to terminate the MAIN_LIB file?

The standard way to open a file in perl is:


Code
 
open INFILE, '<', "data.txt"
or die "Couldn't open data.txt: $!";

If the file is successfully opened then open() returns a true value, and as a result what's to the right of an 'or' is skipped. If opening the file fails for some reason, then open returns undef, which is false, and that causes the right side of the 'or' to execute.

The global variable $! contains the system error message that caused open() to fail.

die() causes the program to terminate after printing out the specified string.

By the way, you can look up the definition of any perl function on your computer like this:

$ perldoc -f die

Or, search google for "perl die" and look for a hit that is a "perldoc". perl has very good docs.

Note that the code is using "bareword filehandles" and that is the old way of doing things. The new way looks like this:


Code
 
open (my $infile, '<', "data.txt"
or die "Couldn't open data.txt: $!";


I like to use all caps for variables containing filehandles, so I write that like this:


Code
 
open (my $INFILE, '<', "data.txt"
or die "Couldn't open data.txt: $!";


Most of the time it's not necessary, but it is considered good practice to close your file as soon as you are done with it:

Code
close $INFILE;


If you don't close your file, perl will automatically close the file when your program ends.


(This post was edited by 7stud on Jan 14, 2013, 1:50 AM)


rovf
Veteran

Jan 14, 2013, 7:21 AM


Views: 1941
Re: [mar85] Problem Understanding Perl Syntax


Quote
what does die $! means?


die throws an exception. The program is terminated only if the exception is not handled. An exception is handled by enclosing the code which throws the exception, by eval. See perldoc -f die and perldoc -f eval for details.


mar85
Novice

Jan 16, 2013, 6:32 PM


Views: 1914
Re: [7stud] Problem Understanding Perl Syntax

Thanks for the explanation 7stud, much appreciated.