Perl before swine/script failure

Discussion in 'Mac Programming' started by mysterytramp, Apr 19, 2010.

  1. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #1
    This script doesn't do anything, but I'm trying to get it to do it well.

    It accepts arguments. If it has two arguments, it should say which is first and which is second. If it has just one argument, it should just print the first argument and print that the second is nonexistent. And if there are no arguments, it should say they are both nonexistent.

    Instead, I get these errors:

    Here's the script:

    Code:
    #!/usr/bin/perl
    # matchtest.plx
    use warnings;
    use strict;
    
    my $scalar1;
    my $firstFile;
    my $secondFile;
    my @array1;
    
    @array1 = @ARGV;
    
    $scalar1 = @array1;
    
    if ($scalar1 == 2) {
    
    	my $firstFile = shift @array1;
    	my $secondFile = shift @array1;
    
    	} else {
    
    		if ($scalar1 == 1) {
    
    			my $firstFile = shift @array1;
    			my $secondFile = "nonexistent";
    	
    	} else {
    
    	my $firstFile = "nonexistent";
    	my $secondFile = "nonexistent";
    
    	}
    }
    
    
    print "First file was $firstFile\n";
    print "Second file was $secondFile\n";
    print "The number of items was $scalar1\n";
    The errors are upset with the first two print statements. Why aren't they legit?

    mt
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    scope.

    my means local to this scope. You have a ton of things that are "too local".

    -Lee
     
  3. macsmurf macrumors 65816

    macsmurf

    Joined:
    Aug 3, 2007
    #3
    When you use my in the blocks you declare new scalars with the same name as the outermost scalars. That's the bug.

    Also, the indentation is weird, and you should really use elsif instead of if within an else. I would change it to

    Code:
    #!/usr/bin/perl
    # matchtest.plx
    use warnings;
    use strict;
    
    my $scalar1;
    my $firstFile;
    my $secondFile;
    my @array1;
    
    @array1 = @ARGV;
    
    $scalar1 = @array1;
    
    if ($scalar1 == 2) {
      $firstFile = shift @array1;
      $secondFile = shift @array1;
    } elsif ($scalar1 == 1) {
      $firstFile = shift @array1;
      $secondFile = "nonexistent";
    } else {
      $firstFile = "nonexistent";
      $secondFile = "nonexistent";
    }
    
    print "First file was $firstFile\n";
    print "Second file was $secondFile\n";
    print "The number of items was $scalar1\n";
    
    but I think I would do it like this:

    Code:
    #!/usr/bin/perl
    # matchtest.plx
    use warnings;
    use strict;
    
    my $length = @ARGV;
    my $firstFile = "nonexistent";
    my $secondFile = "nonexistent";
    
    $firstFile = shift @ARGV if @ARGV;
    $secondFile = shift @ARGV if @ARGV;
    
    print "First file was $firstFile\n";
    print "Second file was $secondFile\n";
    print "The number of items was $length\n";
    
    Bear in mind that I haven't coded Perl in years and I generally try to avoid it.
     
  4. mysterytramp thread starter macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    Thanks, Lee. Removing the extra "my"'s and my script worked.

    Thanks, macsmurf. Both your versions worked fine.

    Thanks for the lesson, guys.

    mt
     

Share This Page