[Melbourne-pm] just my opinion

Jacinta Richardson jarich at perltraining.com.au
Wed Sep 17 19:22:51 PDT 2008


amuhrer wrote:

> for isn't only used for looping

What other uses are there with "for"?

for(my $i = 0; $i < 10; $i++) {
	# this is looping
}

for my $value (@array) {   # I prefer foreach
	# this is looping
}

for my $value (1..10) {    # I prefer foreach
	# so is this
}

print for @array;  # so is this.

I can't think of any use of "for" that isn't looping related.

> sigils change with the context, 

I agree that this is initially confusing.  We explain it by saying that $ means
singular ie "this", "the" or "that", while @ and % are multiple, ie "these",
"those".

my $fish;     # this fish.
my @fruits;   # those fruits
my %facts;    # those facts

$fruits[0];   # _that_ fruit in position 0
$fact{Jane};  # Jane's fact, or _the_ fact about Jane

Most students pick this up on the first day but need a reminder or two every now
and then for the rest of the course.  Especially when we get to references and
they suddenly start trying to treat regular hashes as hash references.  ;)

> some
> operators silently quote things and turn undef into 'undef'

Do you have an example?  I have never seen undef turned into 'undef'.  That
sounds like a bug.  undef does get turned into '' or 0 (as appropriate to the
operator) when used in some scalar contexts, but they are not true values
whereas 'undef' would be.  So

my $c;
if($c > 5) {    # this coerces $c into 0 for the comparison (with warning)
}

if($c lt 'a') {  # this coerces $c into '' for the comparison (with warning)
}

if($c) {         # $c is false, so this expression is false
}

print "$c\n";    # $c gets coerced into '' (with warning)
$d = $c + 10;    # $c gets coerced into 0 (with warning)

if(defined $c) {  # $c is still undefined
}

> hashes, arrays
> and lists can be shifted around from one to the other

I'm not really sure what you mean by this.  Are you talking about the fact that
you can do this?

# Create hash, by assigning it to a list
my %days_in_month = ( January => 31, February => 28, March => 31, April => 30);

# Copy the keys and values into to this array for no good reason
my @silly_stuff = %days_in_month;

# Create a new hash and assign the keys and values from an array instead of a
# list
my %new_days_in_month = @silly_stuff;


Or are you referring to something else?  One certainly can't treat a hash like
an array or visa versa:

# strict errors:
$days_in_month[0];
$silly_stuff{January}

Perhaps you would elaborate?

> perl is a great second or third language to learn, but its a scary first
> language

It probably is.  But then learning a new skill - how to program - (especially if
you're doing it on your own) is always going to be scary, no matter what the
language.  There are certainly easier choices than Perl though.

All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the Melbourne-pm mailing list