Good advice, Hans and Timothy, I'll try to start using it.
One quick follow-on question (I promise only one!):
You mention larger projects, and I've heard about "reusable code"... Is
that generally done by copy/paste into the script you're working on? Or is
there a way to somehow "compile" like C does where it will gather chunks
from a library and stuff them together into a single script? I ask because
portability is huge for me, I have to produce single standalone scripts that
work on any of various unices.
Thanks!
- B
> The main reason is that it will catch errors in your code that vanilla
> Perl will not. As an example, if you accidentally add a typo into one
> of your variables, Perl won't care. It will just create the variable on
> the fly and assume you know what you're doing. Especially with larger
> projects, this can mean hours of scouring your code wondering why it
> doesn't "work", or why a variable doesn't have the expected value.
> Similarly, if you have a program wherein you define a variable $element
> for your loop ala 'foreach my $element(sort @array){', it avoids
> problems with uninitialized variables giving you unexpected data.
>
> Can you live without it? Sure. But the end result is by not letting
> you take shortcuts or write ambiguous code it will make your turnaround
> time faster and your troubleshooting easier.
>
>
> -----Original Message-----
> From: Bryan Harris [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 22, 2006 6:47 PM
> To: Beginners Perl
> Subject: Re: simple references question
>
>
>
> Thanks!
>
> Regarding your "note", out of curiosity, how will it help a lot in the
> end?
> I've been scripting for almost 5 years now, and have produced >100
> scripts
> that are used in data analysis work by ~15 people, and have never used
> "use
> strict", nor declared any variables with "my". Everybody says it's good
> coding practice, but I haven't yet figured out why... Just wondering.
>
> Thanks again for your response.
>
> - Bryan
>
>
>
>> Here's one way, but if you create @f like you want to, then you will
> end
>> up working with a copy of your array instead of the original array
>> passed. That may or may not be what you want to do.
>>
>> If you want to alter the contents of the original array, you will have
>> to use $f and dereference it. When working with the reference, if you
>> need an array (when using sort() for example), use @{$f} or @$f. When
>> you want to access an element of the array, use $f->[element].
>>
>> NOTE: Always use strict and warnings. It's much better to start now
>> than to have to change your habits later. It's like learning to type;
>> at first it makes things slower, but in the end it will help a lot.
>>
>>
>> ##################################
>>
>> use strict;
>> use warnings;
>>
>> my @a = (1,2,3,4);
>> my $b = 10;
>> my $c = 5;
>>
>> d($b,[EMAIL PROTECTED],$c);
>>
>> sub d {
>> my $e = shift;
>> my @f = @{shift()};
>> my $g = shift;
>>
>> print $f[2], "\n";
>> }
>>
>> ###################################
>>
>>
>>
>>
>> -----Original Message-----
>> From: Bryan R Harris [mailto:[EMAIL PROTECTED]
>> Sent: Wednesday, February 22, 2006 12:39 PM
>> To: Beginners Perl
>> Subject: simple references question
>>
>>
>>
>> I'm trying to pass an array to a subroutine. I'd like the subroutine
> to
>> create "@f" from the "[EMAIL PROTECTED]" that I send it, but I can't seem to
>> figure
>> out
>> the syntax. Is there a way to replace my "???f???" with something to
>> make
>> it work?
>>
>> **************************************
>>
>> @a = (1,2,3,4);
>> $b = 10;
>> $c = 5;
>>
>> d($b,[EMAIL PROTECTED],$c);
>>
>> sub d {
>> ($e, ???f??? ,$g) = @_;
>> print $f[2], "\n";
>> }
>>
>> **************************************
>>
>>
>> TIA.
>>
>> - Bryan
>>
>>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>