Variables in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables in perl
# 1  
Old 09-03-2012
Variables in perl

I don't fully understand variables in perl.

If we have a variable defined like this "my $number = 1" then this is called a lexical variable? But if you define this at the top of a script then why isn't it a global variable because it would be available throughout the file?

Sorry if this is a really stupid question to ask but I'm getting myself confused.
# 2  
Old 09-04-2012
Lexical and global refer to where the variables are stored. Global variables(symbols are another name for them) are stored in a symbol table which is maintained in memory accesible by all of the code, including a lot of packages you cannot see. Lexical variables are not in any table. Lexical variables can only refer to a scalar, an array, or a hash. Global variables can reference other things like external procedures.

Just because you write a program with "my $var" near the top does not promote the variable to being stored in a symbol table.

Perl is not C, although knowing C makes learning perl a lot easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. UNIX for Dummies Questions & Answers

Global variables in perl

hi all, i need a help for the following query. Thanks in advance for your valuable time. i have a main.pl file which has a global variable declared as below. our myVar=0; call first.pl script from the main.pl script. print the value of myVar (the value is still 0 and not 10.) i have a... (1 Reply)
Discussion started by: hemalathak10
1 Replies

3. Shell Programming and Scripting

using variables in perl not working

sdcprd@dotstoas110:$ echo $F2 1327332411 this works ---------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime (1327332411))' 012312 <<<< correct date this doesnt ----------- sdcprd@dotstoas110:$ perl -MPOSIX -le 'print strftime ("%m%d%y",localtime... (10 Replies)
Discussion started by: aliyesami
10 Replies

4. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

5. Shell Programming and Scripting

Loading variables - Perl

I'm trying to load a set of variables as defined by a local configuration file. Not too sure what I'm missing so I'll just post it as a whole. blub.pl #!/usr/bin/perl use strict; use warnings; my $config_file="blub.config"; Config_Loader(); sub Config_Loader {... (8 Replies)
Discussion started by: adelsin
8 Replies

6. Shell Programming and Scripting

Use the same perl script with several different variables

I'm new to perl and have to create these files for my job. I have a script that I can run that will create the file, but I have to manually pass the variables to it. It is dumb and it takes a lot of time. I want to be able to pass the existing perl script a set of variables (the same variables... (4 Replies)
Discussion started by: smartin114
4 Replies

7. Shell Programming and Scripting

System variables in Perl

Hi I'm new to Perl and the forum. I've done a quick search on my question but I didn't see an answer. I sincerely apologize if this question has been asked. I'm trying to have my perl scrip recognize system variable $USER such that: my $test_path = "/temp/\$USER/g03/Gau-11097.EIn"; open... (2 Replies)
Discussion started by: jhbamboo
2 Replies

8. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

9. Shell Programming and Scripting

Perl: parsing variables

I have the following script: #!/usr/bin/perl -w @files = <*.csv>; foreach $file (@files) { open(FH, $file); my @dt = split(/_|.csv/, $file); while (<FH>) { chomp; print $dt . $dt . ",$_\n"; } close(FH); } This script reads in all csv files in the current directory... (2 Replies)
Discussion started by: figaro
2 Replies

10. Shell Programming and Scripting

perl global variables

Can someone give me "the lecture" on why you shouldn't make all your varables global when programming in perl. I have been doing this but I have heard that it is not a good practice. (3 Replies)
Discussion started by: reggiej
3 Replies
Login or Register to Ask a Question