IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesPerlLanguage Basics  Print This     

Cross Ref > Language Basics

By Mike Prestwood

Perl versus Access VBA: A side by side comparison between Perl and Access VBA.

 
Language Basics
 

Language basics is kind of a catch all for absolute beginner stuff. The items (common names) I chose for language basics is a bit random and include items like case sensitivity, commenting, declaring variables, etc.

Case Sensitivity

[Other Languages] 

Languages Focus

Case sensitiviy in this case is referring to commands and variable names. For example, are "printf" and "PrintF" equivalent? Are fullname and FullName equivalent? When you create commands, operations, methods, or variables should you worry about case?

Perl:   Yes

Perl is case sensitive.

Syntax Example:
print "hello"; //This works.
Print "hello"; //This does not.
Access VBA:   No

Access VBA is not case sensitive. Like VB Classic, if you type any other case for command or variable names, Access VBA will change it to the "accepted" or "defined" case. For example, if you type msgbox it is converted to Msgbox.

Syntax Example:  

The following code works:

MsgBox ("hello")




Code Blocks

[Other Languages] 

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

Perl: 

In Perl, you create the entire HTML page within your .PL script file using print commands.

For Perl, PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in this example because I see morePeal codeformatted that way.

Syntax Example:
$x = "Yes";
 
If ($x == "Yes") {
print "Hello world";
  print "I am a Perl coder.";
}
Access VBA:   End Xxx

Access VBA code blocks are surrounded by statement ending keywords that all use End such as End Sub, End If, and WEnd.

Syntax Example:
Sub x
End Sub
 
If x Then
End If
  
While x
WEnd




Comments

[Other Languages] 

Languages Focus

Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.

Perl:   #

Commenting Code
Perl uses # for single line comments and Perl does NOT have a multiple line comment.

Compiler Directives (A special comment.)

Perl also uses compiler directives embedded in comments with #! as in:

#!/usr/local/bin/perl -w
Syntax Example:
#This is a comment in Perl.
Access VBA:   ' or REM

Commenting Code
Access VBA, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). Access VBA does NOT have a multiple line comment.

Directives - #

Directives are sometimes called compiler or preprocessor directives. A # is used for directives within Access VBA code. Access VBA offers only an #If..then/#ElseIf/#Else directive.

Syntax Example:
'Single line comment.

REM Old school single line comment.

#If MyDirective Then
'...some code.
#End If




Constants

[Other Languages] 

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

Perl:   use constant

In Perl, you declare constants using the use constant keywords:

use constant CONST_NAME => "Value";

Constants in Perl are case sensitive. A common standard in Perl is to use all-uppercase letters, with underscores to separate words within the name.

Syntax Example:
use constant FULL_NAME => 'Mike Prestwood';
use constant AGE => 38;
  
print "Your name is " . FULL_NAME . ".<br>";
print "You are " . AGE . ".<br>";
Access VBA:   Const kPI = 3.1459

Scope can be Public, Global, or Private. The use of the newer Public keyword is preferred to the older Global. Private Const is the same as just specifying Const.

Syntax Example:
Const kPI = 3.1459
Const kName = "Mike"
 
//Public variable:
Public Const kFeetToMeter=3.28, kMeterToFeet=.3




End of Statement

[Other Languages] 

Languages Focus

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

Perl:   ;
Syntax Example:
print "Hello";
Access VBA:   Return

A return marks the end of a statement and you cannot combine statements on a single line of code. You can break a single statement into two or more code lines by using a space and underscore " _".

Syntax Example:
MsgBox "Hello1"
MsgBox "Hello2"
MsgBox "Hello3"

'The following commented code on a single line does not work...
'MsgBox "Hello4" MsgBox "Hello5"

'Two or more lines works too with a space+underscore:
MsgBox _
"Hello6";




Literals

[Other Languages] 

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

Perl:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
print "Hello";
print "Hello \"Mike\".";
  
#Does Perl evaluate this simple
#floating point math correctly? No! 
if ((.1 + .1 + .1) == .3) {
  print("Correct");
} else {
  print("Not correct");
}
Access VBA:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are optimized out and replaced with # if only integer values are used.

Syntax Example:
MsgBox ("Hello")
MsgBox ("Hello ""Mike"".")
 
'Does Access VBA evaluate this simple
'floating point math correctly? No! 
If (.1 + .1 + .1) = 0.3 Then
MsgBox "Correct"
Else
MsgBox "Not correct"
End If




Variables

[Other Languages] 

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

Perl:   $x = 0;

Perl is a loosely typed language with only three types of variables: scalars, arrays, and hashes. Use $ for a scalar variable, @ for an array, or % for a hash (an associative array).

The scalar variable type is used for any type of simple data such as strings, integers, and numbers. In Perl, you identify and use a variable with a $ even within strings.

Syntax Example:
#!/usr/local/bin/perl -w
 
print("Content-type: text/html\n\n");

$fullname = 'Mike Prestwood';
$Age = 38;
$Weight = 162.4;
 

print "Your name is $fullname.
";
print "You are $Age and weigh $Weight.
";
Access VBA:   Dim x as Integer

Access VBA is a loosely typed language. Declaring variables is optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim, Private, Public, or ReDim. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.

Undeclared variables are variants. To specifically declare a variant, use:

Dim x As Variant
Dim x 

Common data types include Byte (0..255), Boolean, Integer (2-byte integers), Long (4-byte integers), Currency, Single (32-bit number), Double (64-bit number), Date, String, and variant.

Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

Syntax Example:
Dim FullName As String
Dim Age As Integer
Dim Weight As Double
 
FullName = "Mike Prestwood"
Age = 32
Weight = 154.4
 
'Declaritive assignment not supported:
''Dim Married As String = "Y"   '>>>Not supported.




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]