Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

The Elements

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

bomber
Junior Member
*


Posts: 34
Group: Registered
Joined: Sep 2006
Status: Offline
Reputation: 0
Points: 250 (Donate)
Post: #1

Smile The Elements


The rest of these notes will refer to the Perl 5 Reference Guide, highlighting and expanding on important points. So get your Reference Guide and turn to Section 2, Literals.


Perl's Three Data Structures

* Scalars can be numeric or character as determined by context:

123 12.4 5E-10 0xff (hex) 0377 (octal)

'What you $see is (almost) what \n you get' 'Don\'t Walk'

"How are you?" "Substitute values of $x and \n in \" quotes."

`date` `uptime -u` `du -sk $filespec | sort -n`

$x $list_of_things[5] $lookup{'key'}

Single-quotes ' ' allow no substitution except for \\ and \'. Double-quotes " " allow substitution of variables like $x and control codes like \n (newline). Back-quotes ` ` also allow substitution, then try to execute the result as a system command, returning as the final value whatever the system command outputs.

* Arrays of scalars (also called lists) are sequentially-arranged scalars:

('Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday')

(13,14,15,16,17,18,19) equivalent to (13..19)

(13,14,15,16,17,18,19)[2..4] equivalent to (15,16,17)

@whole_list

* Associative arrays (also called hashes) help you remember things:

$DaysInMonth{'January'} = 31; $enrolled{'Joe College'} = 1;

$StudentName{654321} = 'Joe College';

$score{$studentno,$examno} = 89;

%whole_hash

Perl 5 allows combinations of these, such as lists of lists and associative arrays of lists.


Name Conventions

Scalar variables start with '$', even when referring to an array element. The variable name reference for a whole list starts with '@', and the variable name reference for a whole associative array starts with '%'.

Lists are indexed with square brackets enclosing a number, normally starting with [0]. In Perl 5, negative subscripts count from the end. Thus, $things[5] is the 6th element of array @things, and

('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[1]

equals 'Mon'.

Associative arrays are indexed with curly brackets enclosing a string. $whatever, @whatever, and %whatever are three different variables.

Code:
@days = (31,28,31,30,31,30,31,31,30,31,30,31);
                         # A list with 12 elements.

          $#days         # Last index of @days; 11 for above list

          $#days = 7;    # shortens or lengthens list @days to 8 elements

          @days          # ($days[0], $days[1],... )

          @days[3,4,5]   # = (30,31,30)

          @days{'a','c'} # same as ($days{'a'},$days{'c'})

          %days          # (key1, value1, key2, value2, ...)


Case is significant--"$FOO", "$Foo" and "$foo" are all different variables. If a letter or underscore is the first character after the $, @, or %, the rest of the name may also contain digits and underscores. If this character is a digit, the rest must be digits. Perl has several dozen special variables whose second character is non-alphanumeric. For example, $/ is the input record separator, newline "\n" by default. An uninitialized variable has a special "undefined" value which can be detected by the function defined(). Undefined values convert depending on context to 0, null, or false.

The variable "$_" Perl presumes when needed variables are not specified. Thus:

<STDIN>; assigns a record from filehandle STDIN to $_
print; prints the curent value of $_
chop; removes the last character from $_
@things = split; parses $_ into white-space delimited
words, which become successive
elements of list @things.

$_, $1, $2, $3, and other implicit variables contribute to Perl Paradox Number Two: What you don't see can help you or hurt you. See Quick Reference Guide Section 25, Special Variables.

Subroutines and functions are referenced with an initial '&', which is optional if reference is obviously a subroutine or function such as following the sub, do, and sort directives:

sub square { return $_[0] ** 2; }

print "5 squared is ", &square(5);

Filehandles don't start with a special character, and so as to not conflict with reserved words are most reliably specified as uppercase names: INPUT, OUTPUT, STDIN, STDOUT, STDERR, etc.

19-09-2006 12:17 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 13-10-2008, 09:31 PM


Copyright © 2002-2008 MyBB Group
Powered By MyBB