A couple of Perl subtleties
perl
Solution
shift @queue;
($item1, @rest) = split /,/;
If I understand you correctly, you seem to think that this shifts off an element from `@queue` to `$_`. That is not true.
The value that is shifted off of `@queue` simply disappears The following `split` operates on whatever is contained in `$_` (which is independent of the `shift` invocation).
while(<SOME_FILE>){
# Do something involving $_
}
Reading from a filehandle in a `while` statement is special: It is equivalent to
while ( defined( $_ = readline *SOME_FILE ) ) {
This way, you can process even colossal files line-by-line.
On the other hand,
for(<SOME_FILE>){
# Do something involving $_
}
will first load the entire file as a list of lines into memory. Try a 1GB file and see the difference.
Problem
I've been programming in Perl for a while, but I never have understood a couple of subtleties about Perl: The use and the setting/unsetting of the $_ variable confuses me. For instance, why does ``` # ... shift @queue; ($item1, @rest) = split /,/; ``` work, but (at least for me) ``` # ... shift @queue; /some_pattern.*/ or die(); ``` does not seem to work? Also, I don't understand the difference between iterating through a file using `foreach` versus `while`. For instance,I seem to be getting different results for ``` while(<SOME_FILE>){ # Do something involving $_ } ``` and ``` foreach (<SOME_FILE>){ # Do something involving $_ } ``` Can anyone explain these subtle differences?