Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Scoping Variables

by stuffy (Monk)
on May 17, 2001 at 13:52 UTC ( #81192=perlquestion: print w/replies, xml ) Need Help??

stuffy has asked for the wisdom of the Perl Monks concerning the following question:

I'm going to make some assumptions from things I've read in Programming perl, and in the monastary Please correct me if I'm wrong:

sub meat_and_potatoes_of_program { while (<my_file>){ /(\w{3}\s+\w{3}\s+\d{1,2}/; my $date = $1; print "date \n <br>";
this works as planned, every time the program runs across the date, it is output on the screen. I'm assuming that $date is scoped in the while loop
the next part:

if ($foo) { print "the date is... $date \n <br>"; }

this is where I don't think things are working correctly. the ouput is not exactly what I am expecting. I am getting "the date is..." and that's it. I am assuming that since $date is scoped within the while loop, it therefore should work in the if loop since the if loop is contained in the while. I am expecting to see "the date is Sun Oct 23".
I have also tried:

sub the_meat_and_potatoes_of_program{ my $pass_down_date = 1

just to scope it in the subroutine rather then in the loop, but that has the same results. I'm somewhat confused about what is going on here.

Stuffy

Replies are listed 'Best First'.
Re: Scoping Variables
by azatoth (Curate) on May 17, 2001 at 14:00 UTC
Re: Scoping Variables
by arturo (Vicar) on May 17, 2001 at 18:30 UTC

    It may have nothing to do with scoping at all. Basic rule is that a my variable is visible to everything in the block in which it's declared, so as long as your "if" is within the while loop, $date will be visible. I'd point a finger (tentatively) at

    /(\w{3}\s+\w{3}\s+\d{1,2}/; my $date = $1;

    For two reasons: one is that it's not well-formed. You should be getting an error on that (There's no closing paren). So if this is cut-n-paste, that's a problem right there. Second problem is that (assuming there is a closing paren in the actual code) if that regex doesn't match, $1 won't be set, and it's no surprise that you get nothing when you print out the value of $date.

    You *are* running with -w and strict on, aren't you? The present case is a VERY good example of why you should; if $date doesn't get set and you try to print it, -w will tell you it's uninitialized. And if $date has gone out of scope, strict will complain about it not being declared. So: use -w and strict : really.

    The fix, assuming you want to ignore lines that don't match your date-finding pattern:

    #!/usr/bin/perl -w use strict; # stuff # (in while loop) while (<my_file>) { next unless my ($date) =~ /(\w{3}\s+\w{3}\s+\d{1,2})/; # stuff if ($foo) { print "Date is $date\n"; } # more stuff }

    HTH!

      while (<NEW_HIST>){ /^(\w{3}\s+\w{3}\s+\d{1,2})/; my $pass_down_date = $1 if (/$tool/){ print "$pass_down_date"; $print_checker = 1; } if (/$stop/){ $print_checker = 0; } if ($print_checker){ print "$_ <br>\n"; } }

      this is the actual code in the loop. $pass_down_date is being matched, it will print outside of any of the if loops. If I try to print it out inside any of the if loops, it comes back with what I believe is an empty string.
      by the way, strict and -w are on. :)

      Stuffy

Re: Scoping Variables
by shotgunefx (Parson) on May 17, 2001 at 14:57 UTC
    What's going on between the assignment and the IF statement?

    I would guess something is clobbering the value of $date.

    Do you have a next or redo in there somewhere? Because you are blindly assigning $1 to $date regardless of a match, it sounds like maybe the loop is short circuiting and program flow comes back to the beginning of the loop and the match assigns undef to $date before hitting the IF statement.

    You're not using a my $date inside the IF statement are you? That would scope the original $date out as well.

    It's probably best to post the entire source for the loop it's entirety .

    9 out of 10 times when I have a "bug" like that, it's usually my fault. (typo, wrong context,etc)
Re: Scoping Variables
by mothra (Hermit) on May 17, 2001 at 20:10 UTC
    This, and posts like it, bother me.
    I'm going to make some assumptions from things I've read in Programming perl, and in the monastary Please correct me if I'm wrong:
    sub meat_and_potatoes_of_program { while (<my_file>){ /(\w{3}\s+\w{3}\s+\d{1,2}/;

    You're not memorizing anything here, because you haven't closed the parentheses. You must actually test code before you post it (unless you're merlyn or tilly or one of the several other clueful hackers among the Monks).

    my $date = $1; print "date \n <br>";
    Okay...and that print statement served what purpose in illustrating your problem?
    I have also tried:
    sub the_meat_and_potatoes_of_program{ my $pass_down_date = 1
    Which -- again -- serves what purpose?

    Please be more careful the next time you post a question here. Ensure that your code is exactly the same as that which you claim "doesn't work" (ie. you've tested it and are able to show precisely what the problem is via the errors perl gives you, or the output you're getting and how it differs from what you're expecting), and ideally that your spelling is correct, and that the layout of your post doesn't assault one's visual cortex (thanks to The Damian for that one, and for Text::Autoformat :).

    My intention isn't to flame, but rather to provide advice that will help you get the most helpful and accurate replies, and keep the content of The Monastery high.

      Ahem, er, am I missing something really simple here?
      I wouldn't expect this statement to work:-
      if ($foo) { print "the date is... $date \n <br>"; }
      at least not on my Perl version.

      Surely, it should be:-
      if ($foo) { print "The date is...", $date, "\n <br>"; }


      Never lick a gift horse in the mouth.
        Yes, you are missing something really simple here. It's called interpolation and it happens inside double quotes. Put this in a script and run it. $x=1; print 'single quoted dollar x is $x'; print "\n"; print "double quoted dollar x is $x"; print "\n"; perl -e '
Re: Scoping Variables
by ozzy (Sexton) on May 17, 2001 at 17:19 UTC
    I dont think the problem is the Scope of $date in your while loop.
    Check how
    • Your Opening the file
    • Your Regexp


    -= Ozzy =-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://81192]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2021-10-26 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?