CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
This over that (Misc. perl questions)?

 



modified
Deleted

Jan 29, 2001, 12:49 PM

Post #1 of 6 (624 views)
This over that (Misc. perl questions)? Can't Post

I'm currently coding a program, and I want it to have the best code that I can produce. So I have some very assorted questions.

Which is better,

Code
my

or

Code
local

. What's the difference between them?

I've heard it's always better to code with

Code
use strict;

in there. Everytime I put that in my programs I get a complication error. What limitations does strict put on your code, why is it better, and how to I get my scripts to function right with this on? Are there any documents explaining this?

When I open a file for just reading, should I always have it be

Code
"<filename.txt"

, or is it fine to just leave it as

Code
"filename.txt"

? Does the < do anything to it?

When I open a file, which is better, this:

Code
open (FILE, ">file.txt") || die $!;

, or this:

Code
open (FILE, ">file.txt") or die $!;

? What is the difference between using ||, or using 'or'.

When I'm opening a file just for reading, is it really neccessary to lock it? Also, I always skip unlocking files because when you close it, it gets unlocked anyway, so why add in the code and extra time to the user?

Any other general things you guys do when creating scripts?

Thanks guys, I appreciate it all. :)



cir
Novice

Jan 29, 2001, 2:06 PM

Post #2 of 6 (621 views)
Re: This over that (Misc. perl questions)? [In reply to] Can't Post

I'm no expert, but I'll go over what I know.

I believe that the my or local question was answered in the faq forum.

"When I open a file for just reading, should I always have it be

"<filename.txt"
, or is it fine to just leave it as

"filename.txt"
? Does the < do anything to it?"
< tells perl that you want to open it, it really doesn't do anything to the file.

"When I'm opening a file just for reading, is it really neccessary to lock it? Also, I always skip unlocking files because when you close it, it gets unlocked anyway, so why add in the code and extra time to the user?"
You don't need to lock a file when you're reading, but you should lock it whenever you write to a file, just so that if two people are accessing the same script, the file doesn't get corrupted.

That's pretty much all I know about your question, like I said I'm no expert.

http://


Jasmine
Administrator

Jan 29, 2001, 3:33 PM

Post #3 of 6 (616 views)
Re: This over that (Misc. perl questions)? [In reply to] Can't Post

"my" means that the variable is a private variable within the current block.

"local" means that the variable is semi-private in that it loses scope at the end of the current block -- the variable is available to any subroutine invoked from the block where the local variable is defined.

Perhaps this is better to explain in an example:


Code
#####  "my" example 

$variable = "global";
print $variable; #prints global]

sub mysample {
my $variable = "my";
print $variable; #prints my
subroutine();
}

sub subroutine {
print $variable; #prints global
}

print $variable; #prints global


##### "local" example

$variable = "global";
print $variable; #prints global

sub mysample {
local $variable = "local";
print $variable; #prints local
subroutine();
}

sub subroutine {
print $variable; #prints local
}

print $variable; #prints global




Quake
Deleted

Jan 29, 2001, 6:15 PM

Post #4 of 6 (611 views)
Re: This over that (Misc. perl questions)? [In reply to] Can't Post

I'm pretty sure 'or' is the exact same thing as '||', with no difference

require "quake.h";


japhy
Enthusiast

Jan 29, 2001, 7:46 PM

Post #5 of 6 (607 views)
Re: This over that (Misc. perl questions)? [In reply to] Can't Post

use strict requires, above all else, that you declare all your variables before they are used. This can be done in several ways:

1. declare them with my
2. declare them with use vars qw( ... )
3. declare them with our (Perl 5.6.0)
4. give them full package names ($main::foo)

As for or vs. ||, it's a matter of precedence. || binds more tightly than or. It's like using * and +. You have to put parentheses around things to have it act like you intend:


Code
3 + 4 * 5  is really  3 + (4 * 5) 
so do
(3 + 4) * 5 to force addition before multiplication

# likewise...

open FOO, $file || die "can't open $file: $!";
# is really
open FOO, ($file || die "can't open $file: $!");
# which is probably not what you meant
# so use
open(FOO, $file) || die "...";
# or
open FOO, $file or die "...";

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


modified
Deleted

Jan 30, 2001, 3:35 AM

Post #6 of 6 (603 views)
Re: This over that (Misc. perl questions)? [In reply to] Can't Post

Quake... nevermind. I'll talk to you on ICQ.

Thanks japhy, that helps a good deal :).


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives