473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Using Strick in PERL

43
Hi,

Why is it that if I use "strict" in Perl, I can no longer use any variable as I wish?

Let me explain with this simple script.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. print "Content-type: text/html\n\n";
  4.  
  5. $path = "/usr/www/users/yjulien/voile/cgi-bin/calcule_energie";
  6.  
  7. print $path
  8.  
  9. exit;
  10.  
  11.  
If I run the script I get this output:

/usr/www/users/yjulien/voile/cgi-bin/calcule_energie


But if I ad "use script;" like this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4.  
  5. print "Content-type: text/html\n\n";
  6.  
  7. $path = "/usr/www/users/yjulien/voile/cgi-bin/calcule_energie";
  8.  
  9. print $path
  10.  
  11. exit;
  12.  
  13.  
I get an "Internal Server Error" message.

Where is the conflict?

Thanks,

Yves
Dec 23 '13 #1
4 1589
RonB
589 Expert Mod 512MB
The strict pragma requires you to declare your vars before they are used. They can be declared as a global var by using the 'our' keyword or by using the 'my' keyword which creates lexical var and limits their scope to the enclosing block. 99.90% of the time you should use 'my' to create a lexical var.

Declaring your var limits its scope. You should limit your vars to the smallest scope that they require.
Dec 23 '13 #2
yjulien
43
Thanks Ron,

My problem is that I don't know how to use it properly.

For instance, I have this code that pull the current path of my directory. It works if I don't use strict:

Expand|Select|Wrap|Line Numbers
  1. unless ($path) {
  2.  
  3. $path = $0;
  4. $path =~ s/\\\\/\\/g;
  5. $path =~ s/\\/\//g;
  6. $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
  7.  
  8. unless ($path =~ /\//) {
  9. if ($os eq "unix") { $path = `pwd`; }
  10. }
  11.  
  12. unless ($path =~ /\//) {
  13. $path = $ENV{'SCRIPT_FILENAME'};
  14. $path =~ s/\\\\/\\/g;
  15. $path =~ s/\\/\//g;
  16. $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
  17. }
  18.  
  19. $path =~ s/ //g;
  20. $path =~ s/\n//g;
  21.  
  22. unless ($path =~ /\//) { &path_error; }
  23.  
  24. }
If I want to use strict, how do I use "my" or "our"?

I tried with starting the routine with a simple "my $path = ""; but it dod not worl :-(

Thanks for your help,

Yves
Dec 23 '13 #3
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. my $path;
  2.  
  3. unless ($path) {
  4.  
  5. $path = $0;
  6. $path =~ s/\\\\/\\/g;
  7. $path =~ s/\\/\//g;
  8. $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
  9.  
  10. unless ($path =~ /\//) {
  11. if ($os eq "unix") { $path = `pwd`; }
  12. }
  13.  
  14. unless ($path =~ /\//) {
  15. $path = $ENV{'SCRIPT_FILENAME'};
  16. $path =~ s/\\\\/\\/g;
  17. $path =~ s/\\/\//g;
  18. $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
  19. }
  20.  
  21. $path =~ s/ //g;
  22. $path =~ s/\n//g;
  23.  
  24. unless ($path =~ /\//) { &path_error; }
  25.  
  26. }

FYI, there are far easier ways to get the path.
http://search.cpan.org/~smueller/PathTools-3.40/Cwd.pm
http://search.cpan.org/~rjbs/perl-5....le/Basename.pm
Dec 23 '13 #4
yjulien
43
FYI, there are far easier ways to get the path.
http://search.cpan.org/~smueller/PathTools-3.40/Cwd.pm
http://search.cpan.org/~rjbs/perl-5....le/Basename.pm


Indeed thanks again !!!
Dec 23 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sergio | last post by:
I'm writing a cgi script that uses graphviz (the dot program) to generate a graph and I'm banging my head against the wall trying to get it to work properly. Currently, if I run the script directly...
1
by: JTrigger | last post by:
I need to call a .Net web service that takes an array or object as a parameter from a PERL client. I have been trying to use the PERL SOAP::Lite package to do this without success. I can call one...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
2
by: David Bear | last post by:
almost 41 million hits from google about using perl mod's in python was a bit discouraging...So, I thought about asking humans. There are some perl modules written to manage files, acls, etc in...
0
by: smritikapoor | last post by:
I am trying to connect to an https server using NNTP perl module. I have been able to connect to normal NNTP server using the NNTP perl module but cannot connect to an https server. Please help me...
1
by: Wade Ward | last post by:
I've used scripting languages before but never perl in particular. This screen shot shows where I'm stuck: http://www.billfordx.net/2007-04-28.htm My machine, even after installing komodo, has no...
1
by: jasper123 | last post by:
Hello, I am developing a cgi-perl script that takes some value from a html form as input and stores them in a data file. I have a program called "irr" in my server, it is executed just by typing irr...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
2
by: chanshaw | last post by:
Ok in linux the first line in your script is #!/usr/bin/perl -w What is it on windows if your perl directory is in c:\perl
1
by: Kamalendu | last post by:
1. I am using Padre perl IDE 2. I have DBD-Oracle-1.19 installed in my machine. 3. Oracle 10g Express Edition is Installed. Source code #!/usr/bin/perl -w use 5.006; use strict;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.