On Monday, September 2, 2013, John Aten wrote:
        my $valid_token = validate_tokens($token);


Too bad it doesn't work! Even if I put in valid tokens on the first shot,
there are errors:

You're passing the token as a parameter to the sub but the is using $_ for
the match. You need to assign the parameter in the sub. They arrive on the
global @_ array; one idiom is
sub validate_token {
  my ($test_token) = @_;

This assigns the first passed parameter to test_token. The advantage being
of you add more params you can just insert a new var. in the LHS list:
  my ($test_token, $empty_allowed, $cleanup) = @_;

Now use $test_token instead of $_



>
> I am writing a script to rename files. The script prompts the user to
> enter tokens which will comprise part of the file name. These are made of
> letters and numbers separated by a dot, ie: R.3. The letters can be R, I or
> C, upper or lowercase. The first number can be one through eight, and if it
> is a six there can be an additional dot followed by a one or two. To make
> sure that I don't make any mistakes when putting in these tokens, I have
> tried to write a function to make sure that they are valid:
>
> sub validate_tokens {
>         if ($_ !~ /^[ric]\.[1234578]$/i) {
>                 if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
>                         print "Enter valid tokens: ";
>                         my $new_token = <STDIN>;
>                         chomp $new_token;
>                         validate_tokens($new_token);
>                         }
>                 }
>         return $_;
> }
>
>
> I'm calling it like this:
>
>         print "Enter Tokens: ";
>         my $token = <STDIN>;
>         chomp $token;
>         my $valid_token = validate_tokens($token);
>
>
> Too bad it doesn't work! Even if I put in valid tokens on the first shot,
> there are errors:
>
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65,
> <STDIN> line 3.
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66,
> <STDIN> line 3.
>
> Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of
> the first code snippet above. Each time I run it, the <STDIN> line number
> in the error message increases by one. The script goes into an infinite
> loop, prompting for valid tokens, but even if valid ones are entered it
> continues. What am I missing?
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org <javascript:;>
> For additional commands, e-mail: beginners-h...@perl.org <javascript:;>
> http://learn.perl.org/
>
>
>

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk

Reply via email to