Using Strick in PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yjulien
    New Member
    • Feb 2011
    • 43

    #1

    Using Strick in PERL

    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.

    Code:
    #!/usr/local/bin/perl
    
    print "Content-type: text/html\n\n";
    
    $path = "/usr/www/users/yjulien/voile/cgi-bin/calcule_energie";
    
    print $path
    
    exit;
    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:

    Code:
    #!/usr/local/bin/perl
    
    use strict;
    
    print "Content-type: text/html\n\n";
    
    $path = "/usr/www/users/yjulien/voile/cgi-bin/calcule_energie";
    
    print $path
    
    exit;
    I get an "Internal Server Error" message.

    Where is the conflict?

    Thanks,

    Yves
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    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.

    Comment

    • yjulien
      New Member
      • Feb 2011
      • 43

      #3
      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:

      Code:
      unless ($path) {
      
      $path = $0;
      $path =~ s/\\\\/\\/g;
      $path =~ s/\\/\//g;
      $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
      
      unless ($path =~ /\//) {
      if ($os eq "unix") { $path = `pwd`; }
      }
      
      unless ($path =~ /\//) {
      $path = $ENV{'SCRIPT_FILENAME'};
      $path =~ s/\\\\/\\/g;
      $path =~ s/\\/\//g;
      $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
      }
      
      $path =~ s/ //g;
      $path =~ s/\n//g;
      
      unless ($path =~ /\//) { &path_error; }
      
      }
      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

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Code:
        my $path;
        
        unless ($path) {
         
        $path = $0;
        $path =~ s/\\\\/\\/g;
        $path =~ s/\\/\//g;
        $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
         
        unless ($path =~ /\//) {
        if ($os eq "unix") { $path = `pwd`; }
        }
         
        unless ($path =~ /\//) {
        $path = $ENV{'SCRIPT_FILENAME'};
        $path =~ s/\\\\/\\/g;
        $path =~ s/\\/\//g;
        $path =~ s/(\/)(\w*)(\.*)(\w+)$//g;
        }
         
        $path =~ s/ //g;
        $path =~ s/\n//g;
         
        unless ($path =~ /\//) { &path_error; }
         
        }

        FYI, there are far easier ways to get the path.

        Comment

        • yjulien
          New Member
          • Feb 2011
          • 43

          #5
          FYI, there are far easier ways to get the path.

          http://search.cpan.org/~rjbs/perl-5....le/Basename.pm


          Indeed thanks again !!!

          Comment

          Working...